Client to Server Example Java

Client to Server Example Java

Before going into the details of client-to-server communication, it is advised to go through Networking – Introduction and Communication with TCP/IP Protocol to know the terms and basics of networking and the way Java supports.

This is the first application in one-way communication. In one-way communication, here, client sends to server but server does not send back to client. In two-way communication, client sends to server and also server sends back to client.

Total 4 applications are given in TCP/IP communication.

Application Number Functionality
1st application Client to server communication (one-way)
2nd application Server to client communication (one-way)
3rd application Server sends file contents to client (two-way, non-continuous)
4th application Chat program (two-way, continuous)
1st application, Client to Server Example Java: Client to server communication

The application comprises of two program. A client program running on client-side and a server program running on server-side. The client-side program, sends best wishes to the server and the server-side program, receives the message and prints on its display terminal (monitor).

Client Program –

Responsibility: Able to send wishes as a string to the server. As it is sending, it uses output streams.

Socket sock = new Socket("", 5000);

The Socket class constructor takes two parameters – a string, the IP address of the server and an integer, the port number on the server which the client would like to connect. is the default address of the local system in computer networks.

OutputStream ostream = sock.getOutputStream();

The getOutputStream() method of Socket class returns an object of OutputStream, here the object is ostream. This is the starting point of the whole communication (program). Here, the socket gets linked to streams. Streams are instrumental to pass the data.

DataOutputStream dos = new DataOutputStream(ostream);

As OutputStream is an abstract class; it cannot be used directly. In the above code, it is chained to a concrete class DataOutputStream. writeBytes() method of DataOutputStream takes the string message and passes to the Socket. Now the client socket sends to the other socket on the server. When the job is over close the streams and socket. It releases the handles (links) connected to the system resources.

Following are the exceptions, in the above program, thrown by the constructor and different methods.

  1. Socket("", 5000) throws UnknownHostException
  2. getOutputStream() throws IOException
  3. writeBytes(message1) throws IOException
  4. All close() methods throw IOException

Server Program –

Responsibility: Able to receive the message send by the client, read from its socket and print. As it is receiving, it uses input streams.

Client to Server Example Java
Screenshot on Client to Server Example Java

ServerSocket sersock = new ServerSocket(5000);

The server has two jobs – one is, as expected, to communicate and the other is binding the connection on the port number 5000. For communication, it uses Socket and for binding, it uses ServerSocket. Binding is nothing but dedicating the port number to the client as long as it would like; meantime, if some other client asks for 5000 port number, it should not be alloted by the server. When client disconnects, the port is freed and can be given to another client by the server.

Socket sock = sersock.accept();

accept() is a method of ServerSocket class used, used by the server, to bind the connection on the port number 5000, requested by the client.

InputStream istream = sock.getInputStream();

getInputStream() method of Socket returns an object of InputStream and this is the starting point on the server program. The server uses input stream as it is receiving the message.

DataInputStream dstream = new DataInputStream(istream);

As InputStream is an abstract class, it cannot be used directly. It is chained to a concrete class DataInputStream.

String message2 = dstream.readLine();

readLine() method of DataInputStream reads the message string from the socket and returns. This message is printed at the console.

Note: When you compile this program, you get a deprecated warning due to readLine() method of DataInutStream; but the program executes. To avoid this warnig, in the next program, BufferedReader is used.

Executing the client and server programs

On a single system, to act as client and server, open two DOS prompts and treat one as client and the other server. From one DOS prompt, first, run the server program and from the other DOS prompt run the client program. You get output at server DOS prompt.

This application and the next one are one-way communication only either sending or receiving. But the second set (after the next) of applications are two-way where client and server are capable of sending and receiving (both).

Java Networking in a Nutshell

Some questions for you from java.lang package.

1. How many ways you can copy one object properties to another?
Ans: 3 ways – Shallow copying, Deep copying and Cloning.

2. How many types of inner classes exist?
Ans: 4 types.

3. What are JAR files?
Ans: JAR file is a zipped file zipped by JVM.

4. How to convert a string to data type form?
Ans: String to data type conversions – byte, short, int, long, float, double, char and boolean.

5. How to convert an object to string?
Ans: Object to String – toString()

6. Do you know the functionality and methods of following java.lang package classes?
Ans: 1. Root class – class Object 2. class Character 3. class System

7. What are wrapper classes?
Ans: Wrapper classes

8. What is the garbage collection mechanism in Java?
Ans: Garbage Collection – gc() and exit(0)

9. How to use the C++ destructor functionality where Java does not support destructors?
Ans: Java Destructor – finalize()

10. How to compare two objects?
Ans: Object comparison – hashCode() & equals()

10 thoughts on “Client to Server Example Java

    1. S. Nageswara Rao, Corporate Trainer Post author

      Nothing but OutputStream is the super class of DataOutputStream. As OutputStream is an abstract class and we cannot use directly, we are linking OutputStream with DataOutputStream which is concrete class. Moreover, getOutputStream() method returns an object of OutputStream and not an object of DataOutputStream.

  1. essien Address already in use: JVM_Bind
    at Method)
    at com.networking.Server.main(

    1. Rats On Preromasu Address already in use: JVM_Bind
      at Method)
      at com.networking.Server.main(

      How can i solve it?
      i use different port number but that problem occur every time.

Leave a Reply

Your email address will not be published. Required fields are marked *