Java Tutorial
ServerSocket(int port)Creates a server socket on the specified port and queue length of 50.
ServerSocket(int port, int maxQueue)Creates a server socket on the specified port with a maximum queue length (maxQueue).
ServerSocket(int port, int maxQueue, InetAddress address)Creates a server socket on the specified port with a maximum queue length on a specified host address.
// TCP/IP Server Program import java.io.*; import java.net.*; class Server { public static void main(String args[])throws Exception { try { String line, newLine; ServerSocket ss=new ServerSocket(6789); // Communication Endpoint for the client and server. while(true) { // Waiting for socket connection Socket s=ss.accept(); System.out.println("Server Started..."); // DataInputStream to read data from input stream DataInputStream inp=new DataInputStream(s.getInputStream()); // DataOutputStream to write data on outut stream DataOutputStream out = new DataOutputStream(s.getOutputStream()); DataInputStream in=new DataInputStream(System.in); // Rads a line text while(true) { System.out.println("Press 'q' if you want to exit server"); line = inp.readLine(); // Writes in output stream as bytes //out.writeBytes(line +'\n'); System.out.println("Received from client: " + line); newLine = in.readLine(); if (newLine.equals("q")) { out.writeBytes("Server is down..." +'\n'); return; } else { out.writeBytes(newLine + '\n'); } } } } catch(Exception e) { } } }
// TCP/IP Client program import java.io.*; import java.net.*; class Client { public static void main(String args[])throws Exception { String line, newLine; try { DataInputStream in=new DataInputStream(System.in); // Communication Endpoint for client and server Socket cs=new Socket("LocalHost",6789); System.out.println("Client Started..."); // DataInputStream to read data from input stream DataInputStream inp=new DataInputStream (cs.getInputStream()); // DataOutputStream to write data on outut stream DataOutputStream out=new DataOutputStream(cs.getOutputStream()); while(true) { newLine = in.readLine(); if (newLine.equals("q")) { out.writeBytes("Client is down..." +'\n'); return; } else { out.writeBytes(newLine + '\n'); } line = inp.readLine(); System.out.println("Received from server: "+line); } } catch(Exception e) { } } }Server Output:
// Compile and Run server program first $ javac Server.java Note: Server.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. $ java Server Server Started... Press 'q' if you want to exit server Received from client: hi how are you? Press 'q' if you want to exit server Received from client: i am fine, how is going qClient Output:
// Compile and Run client program when server program is running $ javac Client.java Note: Client.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. $ java Client Client Started... hi Received from server: how are you? i am fine, how is going Received from server: Server is down...
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page