{"id":191,"date":"2018-07-26T12:19:22","date_gmt":"2018-07-26T12:19:22","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=191"},"modified":"2018-08-02T07:02:29","modified_gmt":"2018-08-02T07:02:29","slug":"java-socket-programming","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-socket-programming\/","title":{"rendered":"Java Socket Programming"},"content":{"raw":"<div>\r\n\r\n<strong>TCP and UDP<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are two types of Internet Protocols (IP) called <strong>TCP<\/strong> or <strong>Transmission Control Protocol<\/strong> and <strong>UDP<\/strong> or <strong>User Datagram Protocol<\/strong>. TCP (Transmission Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers whereas UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another without guarantee about arrival. Hence, UDP is not connection-based like TCP.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">TCP is suited for applications that require high reliability, and transmission time is relatively less critical. UDP is suitable for applications that need fast, efficient transmission, such as games. UDP's stateless nature is also useful for servers that answer small queries from huge numbers of clients.<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0TCP\/IP Server\/Client Sockets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">TCP\/IP sockets are used to implement reliable, bidirectional, persistent, point-to-point, stream-based connections between hosts on the Internet. A socket can be used to connect Java\u2019s I\/O system to other programs that may reside either on the local machine or on any other machine on the Internet.<\/p>\r\n&nbsp;\r\n\r\n<strong>Kinds of TCP Sockets<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are Two kinds of TCP sockets in Java. One is for servers, and the other is for clients.\r\n<ol>\r\n \t<li style=\"text-align: justify\">The <strong>ServerSocket<\/strong> class is designed to be a \u201clistener,\u201d which waits for clients to connect before doing anything. Hence, ServerSocket is for servers.<\/li>\r\n \t<li style=\"text-align: justify\">The <strong>Socket<\/strong> class is designed to connect to server sockets and to initiate protocol exchanges. The Socket class is for clients. The creation of a Socket object implicitly establishes a connection between the client and server.<\/li>\r\n<\/ol>\r\n<strong>\u00a0 \u00a0 \u00a0Create Client Sockets<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are two constructors used to create client sockets as shown in Table 22.1.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109.png\" alt=\"\" width=\"622\" height=\"174\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Instance Methods for Sockets<\/strong>\r\n\r\n&nbsp;\r\n\r\nSocket defines several instance methods as shown in Table 22.2.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-193 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110.png\" alt=\"\" width=\"599\" height=\"259\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Input and Output Stream<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The input and output streams associated with a Socket constructor by the use of the getInputStream( ) and getOuptutStream( ) methods are shown in Table 22.3.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-194 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111.png\" alt=\"\" width=\"648\" height=\"170\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Sockets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">java.net.Socket is an abstraction of a bi-directional communication channel between hosts.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-195 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112.png\" alt=\"\" width=\"462\" height=\"196\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\">Through sockets, data can be sent and received via Input stream and Output Stream of sockets as shown in Figure 22.1.<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Socket Communication<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A server (program) runs on a specific computer and has a socket that is bound to a specific port.The server waits and listens to the socket for a client to make a connection request. This is shown in Figure 22.1.<\/p>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-196 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113.png\" alt=\"\" width=\"493\" height=\"505\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0Implementing Server<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe steps involved in implementing Server socket communication is shown below.\r\n\r\n&nbsp;\r\n\r\n1<strong>.<\/strong> <strong>Open the Server Socket<\/strong>\r\n\r\n&nbsp;\r\n\r\nServerSocket server;\r\n\r\nDataOutputStream os;\r\n\r\nDataInputStream is;\r\n\r\nserver = new ServerSocket(PORT);\r\n\r\n&nbsp;\r\n\r\n2.\u00a0 <strong>Wait for the Client Request<\/strong>\r\n\r\n&nbsp;\r\n\r\nSocket client = server. accept();\r\n\r\n&nbsp;\r\n\r\n3. <strong>Create I\/O streams for communicating to the client<\/strong>\r\n\r\n&nbsp;\r\n\r\nis = new DataInputStream(client.getInputStream());\r\n\r\nos = new DataOutputStream(client.getOutputStream());\r\n\r\n&nbsp;\r\n\r\n4. <strong>Perform communication with client<\/strong>\r\n\r\n&nbsp;\r\n\r\nReceive from client:\r\n\r\nString line = is.readLine();\r\n\r\nSend to client:\r\n\r\nos.writeBytes(\"Hello\\n\");\r\n\r\n&nbsp;\r\n\r\n5. <strong>Close sockets<\/strong>\r\n\r\n&nbsp;\r\n\r\nclient.close();\r\n\r\n&nbsp;\r\n\r\n<strong>Implementing Client<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe steps involved in implementing Client socket communication is shown below.\r\n\r\n&nbsp;\r\n\r\n<strong>1. Create a Socket Object:<\/strong>\r\n\r\n&nbsp;\r\n\r\nclient = new Socket(server, port_id );\r\n\r\n&nbsp;\r\n\r\n2<strong>. Create I\/O streams for communicating with the server.<\/strong>\r\n\r\n&nbsp;\r\n\r\nis = new DataInputStream(client.getInputStream() );\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">os = new DataOutputStream( client.getOutputStream() );<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a03<strong>. Perform I\/O or communication with the server:<\/strong>\r\n\r\n&nbsp;\r\n\r\nReceive data from the server:\r\n\r\nString line = is.readLine();\r\n\r\nSend data to the server:\r\n\r\nos.writeBytes(\"Hello\\n\");\r\n\r\n&nbsp;\r\n\r\n4<strong>. Close the socket when done:<\/strong>\r\n\r\n&nbsp;\r\n\r\nclient.close();\r\n\r\n&nbsp;\r\n\r\n<strong>Client\/Server Communication<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The server must be running when a client starts. the Client\/Server communication with sockets is\u00a0<span style=\"text-align: initial;font-size: 1em\">explained pictorialy in Figure 22.4.<\/span><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114.png\" alt=\"\" width=\"584\" height=\"426\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Server creates a server socket, bind to a port and waits for client connections. The Client requests for a connection and when the connection is accepted by the server, a socket is created. Then communication takes place using Input Output streams through sockets.<\/p>\r\n&nbsp;\r\n\r\n<strong>Data Transmission Through Sockets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 22.5 illustrates the code used to create sockets and to establish connection between server and client. Once the connection request is accepted, data transmission takes place via streams through sockets between the client and the server.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115.png\" alt=\"\" width=\"561\" height=\"437\" \/><\/p>\r\n\r\n<div>\r\n\r\nThe methods used for creating Input and Ouput streams are given below.\r\n\r\n&nbsp;\r\n\r\nInputStream input = socket.getInputStream();\r\n\r\nOutputStream output = socket.getOutputStream\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong> A Simple Server Code<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 SimpleServer.java: a simple server program import java.net.*;\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\npublic class SimpleServer {\r\n\r\npublic static void main(String args[]) throws IOException {\r\n\r\n\/\/\u00a0 Register service on port 1234\r\n\r\n\/\/\u00a0 Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out);\r\n\r\n\/\/\u00a0 Send a string!\r\n\r\ndos.writeUTF(\"Hi there\");\r\n\r\n\/\/\u00a0 Close the connection, but not the server socket dos.close();\r\n\r\ns1out.close(); s1.close();\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A simple server code is given above. A server socket is created and registered its service on port 1234. The server then listens for clients connection and accept it. Once connection is established, the server gets a communication stream associated with the socket and sends a string to the client. Finally it closes the connection.<\/p>\r\n&nbsp;\r\n\r\n<strong>A Simple Client Code<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 SimpleClient.java: a simple client program import java.net.*;\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">import java.io.*;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public class SimpleClient {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public static void main(String args[]) throws IOException { \/\/ Open your connection to a server, at port 1234 Socket s1 = new Socket(\u201cvijim.ist.au\",1234);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String st = new String (dis.readUTF()); System.out.println(st);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 When done, just close the connection and exit<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">dis.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s1In.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s1.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">\u00a0A simple client code is given above. The client requests for a connection at port 1234 registered by the server. Once connection is accepted a socket is created. The client then gets an input stream associated with the socket and reads the string sent by the server. When this is done, it closes the connection.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example of Server<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A working example of Server and Client is given here.Both the server and the client are running in the local host.<\/p>\r\n<p style=\"text-align: justify\">A simple server code is given below. A server socket is created and registered its service on port 6014. The server then waits for clients connection and accept it. Once connection is established, the server gets an input stream associated with the socket represented as <strong>fromsock<\/strong> to read a string from the client. The string is then displayed on the server screen. Again the server gets an output stream represented as <strong>toSock<\/strong> to write an 'OK' message to the client. Finally it closes the connection.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0import java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass Server2\r\n\r\n{\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\nServerSocket Server;\r\n\r\nSocket s;\r\n\r\nBufferedReader fromKbd,fromSock;\r\n\r\nPrintStream toSock;\r\n\r\nString line;\u00a0\u00a0\u00a0\u00a0 int i ;\r\n\r\ntry\r\n\r\n{\r\n\r\nServer = new ServerSocket(6014);\r\n\r\nSystem.out.println(\"Waiting\");\r\n\r\ns = Server.accept();\r\n\r\nSystem.out.println(\"Connection accepted\");\r\n\r\nfromSock = new BufferedReader( new InputStreamReader(s.getInputStream()));\r\n\r\nline = fromSock.readLine();\r\n\r\nSystem.out.println(line);\r\n\r\ntoSock = new PrintStream(s.getOutputStream()); toSock.println(\"Ok\");\r\n\r\nServer.close();\r\n\r\ns.close();\r\n\r\n}\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">catch(Exce ption e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Exception\"+e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Example of Client<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A simple client code is given here. The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads a string from the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock<\/strong> to write a string to the server. Again the client gets an input stream associated with the socket <strong>fromSock<\/strong> to read the message sent by the server. When this is done, it closes the connection.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass Client2\r\n\r\n{\r\n\r\npublic static void main(String a[])\r\n\r\n{\r\n\r\nSocket s;\r\n\r\nBufferedReader fromKbd;\r\n\r\nPrintStream toSock;\r\n\r\nBufferedReader fromSock;\r\n\r\nString line;\r\n\r\ntry\r\n\r\n{\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s = new Socket(InetAddress.getByName(\"localhost\"),6014);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fromKbd = new BufferedReader( new InputStreamReader(System.in)); System.out.println(\"Enter a String\"); line = fromKbd.readLine();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">toSock = new PrintStream(s.getOutputStream()); toSock.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">f<\/span><span style=\"text-align: initial;font-size: 1em\">romSock = new BufferedReader( new InputStreamReader(s.getInputStream()));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">line = fromSock.readLine();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{ }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong>Output of Client &amp; Server<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe output of the above example program is shown in FIgure 22.6.\r\n\r\n<\/div>\r\n<p style=\"text-align: center\">\u00a0<img class=\"alignnone size-full wp-image-200\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117.png\" alt=\"\" width=\"565\" height=\"290\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0Example of Chat TCP Server<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example is to illustrate about chat server and chat client using TCP sockets.A server socket is created and registered its service on port 6014. The server then waits for client connection and accept it. Once connection is established, the server gets a Buffered stream <strong>fromKbd<\/strong> to read strings from the keyboard. The server then gets an output stream represented as <strong>toSock<\/strong> to write an strings to the client. This runs in a loop and finally it closes the connection.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass TCPServerChat\r\n\r\n{\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\nServerSocket Server;\r\n\r\nSocket s;\r\n\r\nBufferedReader fromKbd,fromSock;\r\n\r\nPrintWriter out; String line;\r\n\r\ntry\r\n\r\n{\r\n\r\nServer = new ServerSocket(6014);\r\n\r\nSystem.out.println(\"Waiting\");\r\n\r\ns = Server.accept();\r\n\r\nSystem.out.println(\"Connection accepted\");\r\n\r\nout = new PrintWriter(s.getOutputStream(), true);\r\n\r\nfromSock = new BufferedReader(new InputStreamReader(s.getInputStream()));\r\n\r\nfromKbd = new BufferedReader(new InputStreamReader(System.in));\r\n\r\nSystem.out.println(\"I am ready now\");\r\n\r\nwhile((line = fromKbd.readLine()) != null)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Exception\"+e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Example of Chat TCP Client<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads strings from the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock <\/strong>to write strings to the server. This runs in a loop and and it closes the connection when done.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass TCPClientChat\r\n\r\n{\r\n\r\npublic static void main(String a[])\r\n\r\n{\r\n\r\nSocket s;\r\n\r\nBufferedReader fromKbd, fromSock;\r\n\r\nPrintWriter out;\r\n\r\nString line;\r\n\r\ntry\r\n\r\n{\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s = new Socket(InetAddress.getLocalHost(),6014);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out = new PrintWriter(s.getOutputStream(), true);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fromSock=newBufferedReader(new InputStreamReader(s.getInputStream()));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fromKbd=newBufferedReader(new InputStreamReader(System.in));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"I am ready now\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while((line = fromKbd.readLine()) != null)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Exception\"+e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<strong>Output of TCP Chat<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe output of Chat server and Chat client using TCP sockets is shown in Figure 22.7 and Figure 22.8.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118.png\" alt=\"\" width=\"433\" height=\"472\" \/><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Example of FTP TCP Server<\/strong>\r\n\r\n&nbsp;\r\n\r\nThis is another example for file transfer from server to client using TCP sockets.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A simple server code is given below. A server socket is created and registered its service on port 6014. The server then waits for clients connection and accept it. Once connection is established, the server gets an input stream associated with the socket represented as <strong>fromsock<\/strong> to read the filename\u00a0<span style=\"text-align: initial;font-size: 1em\">requested by the client. The filename is then displayed on the server screen. The server then gets an file input stream represented as <\/span><strong style=\"text-align: initial;font-size: 1em\">fromFile<\/strong><span style=\"text-align: initial;font-size: 1em\"> to read from the file line by line. It then writes to the socket using an output stream <\/span><strong style=\"text-align: initial;font-size: 1em\">toSock<\/strong><span style=\"text-align: initial;font-size: 1em\"> to the client. Finally it closes the connection.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass FTPTCPServer\r\n\r\n{\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\nServerSocket ftpServer;\r\n\r\nSocket s4ftp;\r\n\r\nBufferedReader fromFile,fromKbd,fromSock;\r\n\r\nFileInputStream fis;\r\n\r\nPrintStream toSock;\r\n\r\nString fileName,line;\r\n\r\ntry\r\n\r\n{\r\n\r\nftpServer = new ServerSocket(6014);\r\n\r\nSystem.out.println(\"Waiting\");\r\n\r\ns4ftp = ftpServer.accept();\r\n\r\nSystem.out.println(\"Connection accepted\");\r\n\r\nfromSock = new BufferedReader(new InputStreamReader(s4ftp.getInputStream()));\r\n\r\nfileName = fromSock.readLine();\r\n\r\nSystem.out.println(fileName);\r\n\r\nfis = new FileInputStream(fileName);\r\n\r\n\/\/System.out.println(fileName);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fromFile = new BufferedReader( new InputStreamReader(fis)); toSock = new PrintStream(s4ftp.getOutputStream()); while( (line=fromFile.readLine()) != null )<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/System.out.println(fileName);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">toSock.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(line);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">toSock.println(\"EOF\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ftpServer.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fis.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s4ftp.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Exception\"+e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Example of FTP TCP Client<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A simple client code is given here. The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads the filename requested by the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock<\/strong> to send the filename to the server. Again the client gets an input stream associated with the socket <strong>fromSock<\/strong> to read the file content sent by the server and write\u00a0<span style=\"text-align: initial;font-size: 1em\">it to a file using an file ouput stream <\/span><strong style=\"text-align: initial;font-size: 1em\">toFile<\/strong><span style=\"text-align: initial;font-size: 1em\">. It also displays in the client screen. When this is done, it closes the connection.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\nclass FTPTCPClient\r\n\r\n{\r\n\r\npublic static void main(String a[])\r\n\r\n{\r\n\r\nSocket s4file;\r\n\r\nFileOutputStream fos;\r\n\r\nBufferedReader fromKbd,fromSock;\r\n\r\nPrintStream toSock,toFile;\r\n\r\nString fileName,line;\r\n\r\ntry\r\n\r\n{\r\n\r\ns4file = new Socket(InetAddress.getByName(\"localhost\"),6014); fromKbd = new BufferedReader( new InputStreamReader(System.in)); System.out.println(\"Enter filename\"); fileName = fromKbd.readLine();\r\n\r\ntoSock = new PrintStream(s4file.getOutputStream()); toSock.println(fileName);\r\n\r\nfromSock = new BufferedReader( new InputStreamReader(s4file.getInputStream()));\r\n\r\nfos = new FileOutputStream(\"copy.txt\");\r\n\r\ntoFile = new PrintStream(fos);\r\n\r\nwhile( !(line=fromSock.readLine()).equals(\"EOF\") )\r\n\r\n{\r\n\r\ntoFile.println(line);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(line );<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fos.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s4file.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Output of FTP TCP server &amp; Client<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe output of FTP server and FTP client using TCP sockets is shown in Figure 22.9.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119.png\" alt=\"\" width=\"574\" height=\"351\" \/><\/p>\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module have discussed about the basics of Socket Programming and also discussed about the client server communication with Socket programming. Moreover, this module has explored about Socket and ServerSocket classes in Java and to build TCP communication between two hosts.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>References<\/strong>\r\n<ol>\r\n \t<li>Herbert Schildt, <strong>Java: The Complete Reference,<\/strong> 7th Edition, McGraw-Hill, 2010.<\/li>\r\n \t<li><em>www.diffen.com\/difference\/TCP_vs_UDP<\/em><\/li>\r\n<\/ol>\r\n&nbsp;","rendered":"<div>\n<p><strong>TCP and UDP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are two types of Internet Protocols (IP) called <strong>TCP<\/strong> or <strong>Transmission Control Protocol<\/strong> and <strong>UDP<\/strong> or <strong>User Datagram Protocol<\/strong>. TCP (Transmission Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers whereas UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another without guarantee about arrival. Hence, UDP is not connection-based like TCP.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">TCP is suited for applications that require high reliability, and transmission time is relatively less critical. UDP is suitable for applications that need fast, efficient transmission, such as games. UDP&#8217;s stateless nature is also useful for servers that answer small queries from huge numbers of clients.<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0TCP\/IP Server\/Client Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">TCP\/IP sockets are used to implement reliable, bidirectional, persistent, point-to-point, stream-based connections between hosts on the Internet. A socket can be used to connect Java\u2019s I\/O system to other programs that may reside either on the local machine or on any other machine on the Internet.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Kinds of TCP Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are Two kinds of TCP sockets in Java. One is for servers, and the other is for clients.<\/p>\n<ol>\n<li style=\"text-align: justify\">The <strong>ServerSocket<\/strong> class is designed to be a \u201clistener,\u201d which waits for clients to connect before doing anything. Hence, ServerSocket is for servers.<\/li>\n<li style=\"text-align: justify\">The <strong>Socket<\/strong> class is designed to connect to server sockets and to initiate protocol exchanges. The Socket class is for clients. The creation of a Socket object implicitly establishes a connection between the client and server.<\/li>\n<\/ol>\n<p><strong>\u00a0 \u00a0 \u00a0Create Client Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are two constructors used to create client sockets as shown in Table 22.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109.png\" alt=\"\" width=\"622\" height=\"174\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109.png 622w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109-300x84.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109-225x63.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-109-350x98.png 350w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Instance Methods for Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Socket defines several instance methods as shown in Table 22.2.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-193 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110.png\" alt=\"\" width=\"599\" height=\"259\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110.png 599w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110-300x130.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110-225x97.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-110-350x151.png 350w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Input and Output Stream<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The input and output streams associated with a Socket constructor by the use of the getInputStream( ) and getOuptutStream( ) methods are shown in Table 22.3.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-194 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111.png\" alt=\"\" width=\"648\" height=\"170\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111.png 648w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111-300x79.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111-65x17.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111-225x59.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-111-350x92.png 350w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">java.net.Socket is an abstraction of a bi-directional communication channel between hosts.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-195 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112.png\" alt=\"\" width=\"462\" height=\"196\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112.png 462w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112-300x127.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112-225x95.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-112-350x148.png 350w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\">Through sockets, data can be sent and received via Input stream and Output Stream of sockets as shown in Figure 22.1.<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Socket Communication<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A server (program) runs on a specific computer and has a socket that is bound to a specific port.The server waits and listens to the socket for a client to make a connection request. This is shown in Figure 22.1.<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-196 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113.png\" alt=\"\" width=\"493\" height=\"505\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113.png 493w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113-293x300.png 293w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113-65x67.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113-225x230.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-113-350x359.png 350w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0Implementing Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The steps involved in implementing Server socket communication is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p>1<strong>.<\/strong> <strong>Open the Server Socket<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>ServerSocket server;<\/p>\n<p>DataOutputStream os;<\/p>\n<p>DataInputStream is;<\/p>\n<p>server = new ServerSocket(PORT);<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0 <strong>Wait for the Client Request<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Socket client = server. accept();<\/p>\n<p>&nbsp;<\/p>\n<p>3. <strong>Create I\/O streams for communicating to the client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>is = new DataInputStream(client.getInputStream());<\/p>\n<p>os = new DataOutputStream(client.getOutputStream());<\/p>\n<p>&nbsp;<\/p>\n<p>4. <strong>Perform communication with client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Receive from client:<\/p>\n<p>String line = is.readLine();<\/p>\n<p>Send to client:<\/p>\n<p>os.writeBytes(&#8220;Hello\\n&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p>5. <strong>Close sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>client.close();<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Implementing Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The steps involved in implementing Client socket communication is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. Create a Socket Object:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>client = new Socket(server, port_id );<\/p>\n<p>&nbsp;<\/p>\n<p>2<strong>. Create I\/O streams for communicating with the server.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>is = new DataInputStream(client.getInputStream() );<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">os = new DataOutputStream( client.getOutputStream() );<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a03<strong>. Perform I\/O or communication with the server:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Receive data from the server:<\/p>\n<p>String line = is.readLine();<\/p>\n<p>Send data to the server:<\/p>\n<p>os.writeBytes(&#8220;Hello\\n&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p>4<strong>. Close the socket when done:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>client.close();<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Client\/Server Communication<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The server must be running when a client starts. the Client\/Server communication with sockets is\u00a0<span style=\"text-align: initial;font-size: 1em\">explained pictorialy in Figure 22.4.<\/span><\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114.png\" alt=\"\" width=\"584\" height=\"426\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114.png 584w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114-300x219.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114-225x164.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-114-350x255.png 350w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Server creates a server socket, bind to a port and waits for client connections. The Client requests for a connection and when the connection is accepted by the server, a socket is created. Then communication takes place using Input Output streams through sockets.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Data Transmission Through Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 22.5 illustrates the code used to create sockets and to establish connection between server and client. Once the connection request is accepted, data transmission takes place via streams through sockets between the client and the server.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115.png\" alt=\"\" width=\"561\" height=\"437\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115.png 561w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115-300x234.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115-225x175.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-115-350x273.png 350w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/p>\n<div>\n<p>The methods used for creating Input and Ouput streams are given below.<\/p>\n<p>&nbsp;<\/p>\n<p>InputStream input = socket.getInputStream();<\/p>\n<p>OutputStream output = socket.getOutputStream<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong> A Simple Server Code<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 SimpleServer.java: a simple server program import java.net.*;<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class SimpleServer {<\/p>\n<p>public static void main(String args[]) throws IOException {<\/p>\n<p>\/\/\u00a0 Register service on port 1234<\/p>\n<p>\/\/\u00a0 Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out);<\/p>\n<p>\/\/\u00a0 Send a string!<\/p>\n<p>dos.writeUTF(&#8220;Hi there&#8221;);<\/p>\n<p>\/\/\u00a0 Close the connection, but not the server socket dos.close();<\/p>\n<p>s1out.close(); s1.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A simple server code is given above. A server socket is created and registered its service on port 1234. The server then listens for clients connection and accept it. Once connection is established, the server gets a communication stream associated with the socket and sends a string to the client. Finally it closes the connection.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>A Simple Client Code<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 SimpleClient.java: a simple client program import java.net.*;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">import java.io.*;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public class SimpleClient {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public static void main(String args[]) throws IOException { \/\/ Open your connection to a server, at port 1234 Socket s1 = new Socket(\u201cvijim.ist.au&#8221;,1234);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String st = new String (dis.readUTF()); System.out.println(st);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 When done, just close the connection and exit<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">dis.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s1In.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s1.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\u00a0A simple client code is given above. The client requests for a connection at port 1234 registered by the server. Once connection is accepted a socket is created. The client then gets an input stream associated with the socket and reads the string sent by the server. When this is done, it closes the connection.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example of Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A working example of Server and Client is given here.Both the server and the client are running in the local host.<\/p>\n<p style=\"text-align: justify\">A simple server code is given below. A server socket is created and registered its service on port 6014. The server then waits for clients connection and accept it. Once connection is established, the server gets an input stream associated with the socket represented as <strong>fromsock<\/strong> to read a string from the client. The string is then displayed on the server screen. Again the server gets an output stream represented as <strong>toSock<\/strong> to write an &#8216;OK&#8217; message to the client. Finally it closes the connection.<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class Server2<\/p>\n<p>{<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>ServerSocket Server;<\/p>\n<p>Socket s;<\/p>\n<p>BufferedReader fromKbd,fromSock;<\/p>\n<p>PrintStream toSock;<\/p>\n<p>String line;\u00a0\u00a0\u00a0\u00a0 int i ;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>Server = new ServerSocket(6014);<\/p>\n<p>System.out.println(&#8220;Waiting&#8221;);<\/p>\n<p>s = Server.accept();<\/p>\n<p>System.out.println(&#8220;Connection accepted&#8221;);<\/p>\n<p>fromSock = new BufferedReader( new InputStreamReader(s.getInputStream()));<\/p>\n<p>line = fromSock.readLine();<\/p>\n<p>System.out.println(line);<\/p>\n<p>toSock = new PrintStream(s.getOutputStream()); toSock.println(&#8220;Ok&#8221;);<\/p>\n<p>Server.close();<\/p>\n<p>s.close();<\/p>\n<p>}<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">catch(Exce ption e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Exception&#8221;+e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 Example of Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A simple client code is given here. The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads a string from the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock<\/strong> to write a string to the server. Again the client gets an input stream associated with the socket <strong>fromSock<\/strong> to read the message sent by the server. When this is done, it closes the connection.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class Client2<\/p>\n<p>{<\/p>\n<p>public static void main(String a[])<\/p>\n<p>{<\/p>\n<p>Socket s;<\/p>\n<p>BufferedReader fromKbd;<\/p>\n<p>PrintStream toSock;<\/p>\n<p>BufferedReader fromSock;<\/p>\n<p>String line;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s = new Socket(InetAddress.getByName(&#8220;localhost&#8221;),6014);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fromKbd = new BufferedReader( new InputStreamReader(System.in)); System.out.println(&#8220;Enter a String&#8221;); line = fromKbd.readLine();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">toSock = new PrintStream(s.getOutputStream()); toSock.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">f<\/span><span style=\"text-align: initial;font-size: 1em\">romSock = new BufferedReader( new InputStreamReader(s.getInputStream()));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">line = fromSock.readLine();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{ }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output of Client &amp; Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above example program is shown in FIgure 22.6.<\/p>\n<\/div>\n<p style=\"text-align: center\">\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-200\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117.png\" alt=\"\" width=\"565\" height=\"290\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117.png 565w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117-300x154.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117-225x115.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-117-350x180.png 350w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0Example of Chat TCP Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example is to illustrate about chat server and chat client using TCP sockets.A server socket is created and registered its service on port 6014. The server then waits for client connection and accept it. Once connection is established, the server gets a Buffered stream <strong>fromKbd<\/strong> to read strings from the keyboard. The server then gets an output stream represented as <strong>toSock<\/strong> to write an strings to the client. This runs in a loop and finally it closes the connection.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class TCPServerChat<\/p>\n<p>{<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>ServerSocket Server;<\/p>\n<p>Socket s;<\/p>\n<p>BufferedReader fromKbd,fromSock;<\/p>\n<p>PrintWriter out; String line;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>Server = new ServerSocket(6014);<\/p>\n<p>System.out.println(&#8220;Waiting&#8221;);<\/p>\n<p>s = Server.accept();<\/p>\n<p>System.out.println(&#8220;Connection accepted&#8221;);<\/p>\n<p>out = new PrintWriter(s.getOutputStream(), true);<\/p>\n<p>fromSock = new BufferedReader(new InputStreamReader(s.getInputStream()));<\/p>\n<p>fromKbd = new BufferedReader(new InputStreamReader(System.in));<\/p>\n<p>System.out.println(&#8220;I am ready now&#8221;);<\/p>\n<p>while((line = fromKbd.readLine()) != null)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Exception&#8221;+e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Example of Chat TCP Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads strings from the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock <\/strong>to write strings to the server. This runs in a loop and and it closes the connection when done.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class TCPClientChat<\/p>\n<p>{<\/p>\n<p>public static void main(String a[])<\/p>\n<p>{<\/p>\n<p>Socket s;<\/p>\n<p>BufferedReader fromKbd, fromSock;<\/p>\n<p>PrintWriter out;<\/p>\n<p>String line;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s = new Socket(InetAddress.getLocalHost(),6014);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out = new PrintWriter(s.getOutputStream(), true);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fromSock=newBufferedReader(new InputStreamReader(s.getInputStream()));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fromKbd=newBufferedReader(new InputStreamReader(System.in));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;I am ready now&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while((line = fromKbd.readLine()) != null)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Exception&#8221;+e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p><strong>Output of TCP Chat<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The output of Chat server and Chat client using TCP sockets is shown in Figure 22.7 and Figure 22.8.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118.png\" alt=\"\" width=\"433\" height=\"472\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118.png 433w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118-275x300.png 275w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118-65x71.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118-225x245.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-118-350x382.png 350w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Example of FTP TCP Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This is another example for file transfer from server to client using TCP sockets.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A simple server code is given below. A server socket is created and registered its service on port 6014. The server then waits for clients connection and accept it. Once connection is established, the server gets an input stream associated with the socket represented as <strong>fromsock<\/strong> to read the filename\u00a0<span style=\"text-align: initial;font-size: 1em\">requested by the client. The filename is then displayed on the server screen. The server then gets an file input stream represented as <\/span><strong style=\"text-align: initial;font-size: 1em\">fromFile<\/strong><span style=\"text-align: initial;font-size: 1em\"> to read from the file line by line. It then writes to the socket using an output stream <\/span><strong style=\"text-align: initial;font-size: 1em\">toSock<\/strong><span style=\"text-align: initial;font-size: 1em\"> to the client. Finally it closes the connection.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class FTPTCPServer<\/p>\n<p>{<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>ServerSocket ftpServer;<\/p>\n<p>Socket s4ftp;<\/p>\n<p>BufferedReader fromFile,fromKbd,fromSock;<\/p>\n<p>FileInputStream fis;<\/p>\n<p>PrintStream toSock;<\/p>\n<p>String fileName,line;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>ftpServer = new ServerSocket(6014);<\/p>\n<p>System.out.println(&#8220;Waiting&#8221;);<\/p>\n<p>s4ftp = ftpServer.accept();<\/p>\n<p>System.out.println(&#8220;Connection accepted&#8221;);<\/p>\n<p>fromSock = new BufferedReader(new InputStreamReader(s4ftp.getInputStream()));<\/p>\n<p>fileName = fromSock.readLine();<\/p>\n<p>System.out.println(fileName);<\/p>\n<p>fis = new FileInputStream(fileName);<\/p>\n<p>\/\/System.out.println(fileName);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fromFile = new BufferedReader( new InputStreamReader(fis)); toSock = new PrintStream(s4ftp.getOutputStream()); while( (line=fromFile.readLine()) != null )<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/System.out.println(fileName);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">toSock.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(line);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">toSock.println(&#8220;EOF&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ftpServer.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fis.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s4ftp.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Exception&#8221;+e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Example of FTP TCP Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A simple client code is given here. The client requests for a connection at port 6014 registered by the server in the local host. Once connection is accepted a socket is created. The client reads the filename requested by the user using a Buffered stream represented as <strong>fromKbd<\/strong>. The client then gets an output stream associated with the socket <strong>toSock<\/strong> to send the filename to the server. Again the client gets an input stream associated with the socket <strong>fromSock<\/strong> to read the file content sent by the server and write\u00a0<span style=\"text-align: initial;font-size: 1em\">it to a file using an file ouput stream <\/span><strong style=\"text-align: initial;font-size: 1em\">toFile<\/strong><span style=\"text-align: initial;font-size: 1em\">. It also displays in the client screen. When this is done, it closes the connection.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>class FTPTCPClient<\/p>\n<p>{<\/p>\n<p>public static void main(String a[])<\/p>\n<p>{<\/p>\n<p>Socket s4file;<\/p>\n<p>FileOutputStream fos;<\/p>\n<p>BufferedReader fromKbd,fromSock;<\/p>\n<p>PrintStream toSock,toFile;<\/p>\n<p>String fileName,line;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>s4file = new Socket(InetAddress.getByName(&#8220;localhost&#8221;),6014); fromKbd = new BufferedReader( new InputStreamReader(System.in)); System.out.println(&#8220;Enter filename&#8221;); fileName = fromKbd.readLine();<\/p>\n<p>toSock = new PrintStream(s4file.getOutputStream()); toSock.println(fileName);<\/p>\n<p>fromSock = new BufferedReader( new InputStreamReader(s4file.getInputStream()));<\/p>\n<p>fos = new FileOutputStream(&#8220;copy.txt&#8221;);<\/p>\n<p>toFile = new PrintStream(fos);<\/p>\n<p>while( !(line=fromSock.readLine()).equals(&#8220;EOF&#8221;) )<\/p>\n<p>{<\/p>\n<p>toFile.println(line);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(line );<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fos.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s4file.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Output of FTP TCP server &amp; Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The output of FTP server and FTP client using TCP sockets is shown in Figure 22.9.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119.png\" alt=\"\" width=\"574\" height=\"351\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119.png 574w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119-300x183.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-119-350x214.png 350w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module have discussed about the basics of Socket Programming and also discussed about the client server communication with Socket programming. Moreover, this module has explored about Socket and ServerSocket classes in Java and to build TCP communication between two hosts.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>References<\/strong><\/p>\n<ol>\n<li>Herbert Schildt, <strong>Java: The Complete Reference,<\/strong> 7th Edition, McGraw-Hill, 2010.<\/li>\n<li><em>www.diffen.com\/difference\/TCP_vs_UDP<\/em><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":20,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-191","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/191\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/191\/revisions\/560"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/191\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=191"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=191"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}