From 858317f74f092e9ac598c33a1a2741fa4c0dc98d Mon Sep 17 00:00:00 2001 From: anasM0hammad Date: Sat, 6 Oct 2018 12:03:35 +0530 Subject: [PATCH 1/3] Socket Server code added --- socket/Server.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 socket/Server.java diff --git a/socket/Server.java b/socket/Server.java new file mode 100644 index 0000000..ee156b8 --- /dev/null +++ b/socket/Server.java @@ -0,0 +1,62 @@ + +import java.net.*; +import java.io.*; + +public class Server +{ + //initialize socket and input stream + private Socket socket = null; + private ServerSocket server = null; + private DataInputStream in = null; + + // constructor with port + public Server(int port) + { + // starts server and waits for a connection + try + { + server = new ServerSocket(port); + System.out.println("Server started"); + + System.out.println("Waiting for a client ...!!"); + + socket = server.accept(); + System.out.println("Client accepted Succesfully"); + + // takes input from the client socket + in = new DataInputStream( + new BufferedInputStream(socket.getInputStream())); + + String line = ""; + + // reads message from client until "Over" is sent + while (!line.equals("Over")) + { + try + { + line = in.readUTF(); + System.out.println(line); + + } + catch(IOException i) + { + System.out.println(i); + } + } + System.out.println("Closing connection"); + + // close connection + socket.close(); + in.close(); + } + catch(IOException i) + { + System.out.println(i); + } + } + + public static void main(String args[]) + { + Server server = new Server(5000); + } +} \ No newline at end of file From ea40c3cd56d2ea1ab2742f8c822fbf5434fe432b Mon Sep 17 00:00:00 2001 From: anasM0hammad Date: Sat, 6 Oct 2018 12:10:27 +0530 Subject: [PATCH 2/3] Client coded is added --- socket/Client.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 socket/Client.java diff --git a/socket/Client.java b/socket/Client.java new file mode 100644 index 0000000..92d95d1 --- /dev/null +++ b/socket/Client.java @@ -0,0 +1,70 @@ + +import java.net.*; +import java.io.*; + +public class Client +{ + // initialize socket and input output streams + private Socket socket = null; + private DataInputStream input = null; + private DataOutputStream out = null; + + // constructor to put ip address and port + public Client(String address, int port) + { + // establish a connection + try + { + socket = new Socket(address, port); + System.out.println("Connected Succesfully"); + + // takes input from terminal + input = new DataInputStream(System.in); + + // sends output to the socket + out = new DataOutputStream(socket.getOutputStream()); + } + catch(UnknownHostException u) + { + System.out.println(u); + } + catch(IOException i) + { + System.out.println(i); + } + + // string to read message from input + String line = ""; + + // keep reading until "Over" is input + while (!line.equals("Over")) + { + try + { + line = input.readLine(); + out.writeUTF(line); + } + catch(IOException i) + { + System.out.println(i); + } + } + + // close the connection + try + { + input.close(); + out.close(); + socket.close(); + } + catch(IOException i) + { + System.out.println(i); + } + } + + public static void main(String args[]) + { + Client client = new Client("127.0.0.1", 5000); + } +} \ No newline at end of file From 3f003e7c450800034518c46208c8a886ba1c0b27 Mon Sep 17 00:00:00 2001 From: wasim963 Date: Sun, 14 Oct 2018 17:45:46 +0530 Subject: [PATCH 3/3] palindrome --- palindrome | 24 ++++++++++++++++++++++++ palindrome.java | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 palindrome create mode 100644 palindrome.java diff --git a/palindrome b/palindrome new file mode 100644 index 0000000..14d8e16 --- /dev/null +++ b/palindrome @@ -0,0 +1,24 @@ +import java.util.*; + +class Palindrome +{ + public static void main(String args[]) + { + String original, reverse = ""; // Objects of String class + Scanner in = new Scanner(System.in); + + System.out.println("Enter a string to check if it is a palindrome"); + original = in.nextLine(); + + int length = original.length(); + + for (int i = length - 1; i >= 0; i--) + reverse = reverse + original.charAt(i); + + if (original.equals(reverse)) + System.out.println("The string is a palindrome."); + else + System.out.println("The string isn't a palindrome."); + + } +} \ No newline at end of file diff --git a/palindrome.java b/palindrome.java new file mode 100644 index 0000000..14d8e16 --- /dev/null +++ b/palindrome.java @@ -0,0 +1,24 @@ +import java.util.*; + +class Palindrome +{ + public static void main(String args[]) + { + String original, reverse = ""; // Objects of String class + Scanner in = new Scanner(System.in); + + System.out.println("Enter a string to check if it is a palindrome"); + original = in.nextLine(); + + int length = original.length(); + + for (int i = length - 1; i >= 0; i--) + reverse = reverse + original.charAt(i); + + if (original.equals(reverse)) + System.out.println("The string is a palindrome."); + else + System.out.println("The string isn't a palindrome."); + + } +} \ No newline at end of file