From c5c8374f7e0f418a7e9e4044532ce1b0d737947a Mon Sep 17 00:00:00 2001 From: Nilesh Halge Date: Sun, 6 Oct 2019 00:32:07 +0530 Subject: [PATCH] Create server.java --- TCP-basics/server.java | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 TCP-basics/server.java diff --git a/TCP-basics/server.java b/TCP-basics/server.java new file mode 100644 index 0000000..7e1a946 --- /dev/null +++ b/TCP-basics/server.java @@ -0,0 +1,70 @@ +// A Java program for a Client +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"); + + // 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); + } +}