Explain socket program using tcp/ip in java


















Here, we are going to make one-way client and server communication. In this application, client sends a message to the server, server reads the message and prints it.

Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side. The accept method of ServerSocket class blocks the console until the client is connected.

After the successful connection of client, it returns the instance of Socket at server-side. A socket is simply an endpoint for communications between the machines.

The Socket class can be used to create a socket. The ServerSocket class can be used to create a server socket. This object is used to establish communication with the clients. To create the server application, we need to create the instance of ServerSocket class. Here, we are using port number for the communication between the client and server. You may also choose any other port number. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy.

By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. Popular Course in this category. Course Price View Course.

Free Software Development Course. Login details for this Free course will be emailed to you. Email ID. Contact No. This method is identical to the previous constructor, except that the host is denoted by an InetAddress object instead of a String. When the Socket constructor returns, it does not simply instantiate a Socket object but it actually attempts to connect to the specified server and port. Some methods of interest in the Socket class are listed here.

Notice that both the client and the server have a Socket object, so these methods can be invoked by both the client and the server. This method connects the socket to the specified host. This method is needed only when you instantiate the Socket using the no-argument constructor. Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.

Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket. Closes the socket, which makes this Socket object no longer capable of connecting again to any server. This class represents an Internet Protocol IP address. The following GreetingClient is a client program that connects to a server by using a socket and sends a greeting, and then waits for a response.

Malhar Lathkar. Anadi Sharma. Tushar Kale. Monica Mittal. Arnab Chakraborty. Save Article. Like Article. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?

Please use ide.



0コメント

  • 1000 / 1000