How to Make Serversocket Wait Again
Welcome to Java Socket programming example. Every server is a programme that runs on a specific organisation and listens on a specific port. Sockets are bound to the port numbers and when nosotros run any server it just listens on the socket and waits for customer requests. For example, tomcat server running on port 8080 waits for client requests and in one case it gets any client request, it responds to them.
Java Socket Programming
A socket is i endpoint of a two-mode communication link between two programs running on the network. The socket is bound to a port number so that the TCP layer tin can identify the application that data is destined to be sent.
In java socket programming example tutorial, nosotros volition learn how to write java socket server and java socket client program. We will likewise acquire how server client program read and write information on the socket.
java.net.Socket and java.cyberspace.ServerSocket are the java classes that implements Socket and Socket server.
Coffee Socket Server Example
package com.journaldev.socket; import java.io.IOException; import coffee.io.ObjectInputStream; import java.io.ObjectOutputStream; import coffee.lang.ClassNotFoundException; import java.net.ServerSocket; import java.net.Socket; /** * This grade implements java Socket server * @author pankaj * */ public course SocketServerExample { //static ServerSocket variable private static ServerSocket server; //socket server port on which it will listen private static int port = 9876; public static void main(Cord args[]) throws IOException, ClassNotFoundException{ //create the socket server object server = new ServerSocket(port); //keep listens indefinitely until receives 'exit' telephone call or program terminates while(true){ System.out.println("Waiting for the client request"); //creating socket and waiting for client connexion Socket socket = server.take(); //read from socket to ObjectInputStream object ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); //convert ObjectInputStream object to String Cord bulletin = (String) ois.readObject(); System.out.println("Message Received: " + bulletin); //create ObjectOutputStream object ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); //write object to Socket oos.writeObject("Hello Client "+message); //close resources ois.shut(); oos.close(); socket.close(); //terminate the server if client sends exit request if(message.equalsIgnoreCase("go out")) break; } Organisation.out.println("Shutting downward Socket server!!"); //shut the ServerSocket object server.close(); } } Coffee Socket Client
package com.journaldev.socket; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import coffee.net.Socket; import coffee.internet.UnknownHostException; /** * This class implements java socket client * @author pankaj * */ public course SocketClientExample { public static void principal(Cord[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{ //get the localhost IP address, if server is running on another IP, you need to employ that InetAddress host = InetAddress.getLocalHost(); Socket socket = zippo; ObjectOutputStream oos = null; ObjectInputStream ois = null; for(int i=0; i<v;i++){ //establish socket connection to server socket = new Socket(host.getHostName(), 9876); //write to socket using ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); System.out.println("Sending request to Socket Server"); if(i==4)oos.writeObject("leave"); else oos.writeObject(""+i); //read the server response message ois = new ObjectInputStream(socket.getInputStream()); String message = (String) ois.readObject(); System.out.println("Message: " + message); //close resource ois.close(); oos.close(); Thread.sleep(100); } } } To test java socket programming of server-customer communication, first we need to run SocketServerExample class. When you will run socket server, information technology will just print "Waiting for client asking" and and so expect for the client request.
Now when you will run SocketClientExample class, it will send a request to java socket server and print the response message to console.
Here is the output of java socket server SocketServerExample programme.
Waiting for the client request Message Received: 0 Waiting for the client request Message Received: 1 Waiting for the client request Message Received: ii Waiting for the customer request Message Received: 3 Waiting for the client request Message Received: exit Shutting downwards Socket server!! Here is the output of Java socket customer SocketClientExample program.
Sending request to Socket Server Bulletin: Howdy Customer 0 Sending request to Socket Server Bulletin: Hullo Customer 1 Sending asking to Socket Server Message: Hi Client 2 Sending request to Socket Server Message: Howdy Customer three Sending request to Socket Server Bulletin: Hi Client go out That's all for a quick roundup of Socket programming in java. I hope you lot can get started with java socket server and java socket client programming.
Reference: Oracle Dr.
Source: https://www.journaldev.com/741/java-socket-programming-server-client
0 Response to "How to Make Serversocket Wait Again"
Post a Comment