Send File Contents two way communication Java

Before going into the details of this server-to-client 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 (second one being chat application) in two-way communication. In one-way communication, either client sends to server or server sends to client. But one will not reciprocate (reverse communication) with the other. In two-way communication, client sends to server and also server sends back to client.

The 4 programs available on the TCP/IP protocol are listed below.

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)

Two-way Communication

In this category of two-way applications, two sets are given where both sending and receiving exists. In the first application, client asks the server to send the file contents. The server sends. This is not a continuous communication. The second application is a chat program where it is a continuous communication.

3rd application, Send File Contents two way communication Java: – Server returns the file contents requested by the client

In this application, the client requests the server to send the file contents by supplying the file name. The file exists on the server. The server opens the file, reads the contents line-by-line and sends each line separately one-by-one.

Client program:

The client program has three responsibilities which must be fulfilled in the code. To make the code simple, instead of try-catch blocks, Exception is thrown in the main() method signature.

Following are the three responsibilities of the client.

  1. To take input for file name from keyboard. Remember, this file should exist on server. For this, it uses input stream.
  2. The file read from keyboard should be sent to the server. For this client uses output stream.
  3. The file contents sent by the server, the client should receive and print on the console.
public class ContentsClient   
  public static void main( String args[ ] ) throws Exception
     Socket sock = new Socket( "", 4000);

                   // reading the file name from keyboard. Uses input stream
     System.out.print("Enter the file name");
     BufferedReader keyRead = new BufferedReader(new InputStreamReader(;
     String fname = keyRead.readLine();
	        // sending the file name to server. Uses PrintWriter
     OutputStream  ostream = sock.getOutputStream( );
     PrintWriter pwrite = new PrintWriter(ostream, true);
          	          // receiving the contents from server.  Uses input stream
     InputStream istream = sock.getInputStream();
     BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream));

     String str;
     while((str = socketRead.readLine())  !=  null) // reading line-by-line 
     pwrite.close(); socketRead.close(); keyRead.close();

To take input from the keyboard, a BufferedReader object, keyRead, is created. To send the file name to the server, pwrite of PrintWriter and to receive the file contents from the server, socketRead of BufferedReader are created.

PrintWriter pwrite = new PrintWriter(ostream, true);

The speciality of PrintWriter is it has got a provision for auto flushing. The second parameter of the constructor true indicates auto flushing.

Server program:

The server program has three responsibilities which must be fulfilled in the code.

  1. First job is to read the file name coming from client. For this, it uses input stream.
  2. Second one is to open the file, using some input stream, and read the contents.
  3. Third one is, as the reading is going on, to send the contents each line separately.
public class ContentsServer   
  public static void main(String args[]) throws Exception
  {                           // establishing the connection with the server
     ServerSocket sersock = new ServerSocket(4000);
     System.out.println("Server ready for connection");
     Socket sock = sersock.accept();            // binding with port: 4000
     System.out.println("Connection is successful and wating for chatting");
                               // reading the file name from client
     InputStream istream = sock.getInputStream( );
     BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
     String fname = fileRead.readLine( );
                             // reading file contents
     BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
                           // keeping output stream ready to send the contents
     OutputStream ostream = sock.getOutputStream( );
     PrintWriter pwrite = new PrintWriter(ostream, true);
     String str;
     while((str = contentRead.readLine()) !=  null) // reading line-by-line from file
	pwrite.println(str);         // sending each line to client

     sock.close();  sersock.close();       // closing network sockets
     pwrite.close();  fileRead.close(); contentRead.close();

Send File Contents two way communication Java
Screenshot of Send File Contents two way communication Java

Observe, the client and server use the same streams as both have got the same responsibility of sending and receiving the data. For reading the data sent by other side, they use BufferedReader and to send data they use PrintWriter.

4 thoughts on “Send File Contents two way communication Java”

  1. Hai

    Thanks for this article..

    I am working with client server communication…Clients sends a request, server reads that request, and sends a response, lastly client reads the response.I tried to implement with the help of many examples.But the client sends a request successfully, and the server reads it and responses successfully but the client then cannot get the response.Please help me….

Leave a Comment

Your email address will not be published.