Send File Contents two way communication Java

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: ContentsClient.java

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.

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: ContentsServer.java

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.


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. saranya

    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 Reply

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