<< Chapter < Page Chapter >> Page >

Assuming that you select a computer that supports echo processing on port 7, the output from this program should be as shown in Figure 2 .

Figure 2 - Output for a successful echo test.
echo: This is an echo test

Beginning of the program named Java4660a

I will discuss this program in fragments. (I will ignore exception handling code.) A complete listing of the program is provided in Listing 12 . The beginning of the program is shown in Listing 1 .

Listing 1 - Beginning of the program named Java4660a.
import java.net.*; import java.io.*;import java.util.*; class Java4660a{ public static void main(String[]args){ String server = "localhost";int port = 7; //echo port

Listing 1 declares and initializes two local variables to specify the server and the port. We will use these variables later.

Instantiate a Socket object

Listing 2 shows the key statement in this program insofar as learning new material is concerned.

Listing 2 - Instantiate a Socket object.
Socket socket = new Socket(server,port);

The statement in Listing 2 establishes a connection with the specified port on the specified server by instantiating a new object of type Socket .

Once this object exists, it is possible to use it to communicate with the server on the specified port using the protocol prescribed for the service beingdelivered on that port.

The constructor for this class throws two different types of exceptions so you will need to wrap this statement in a try/catch block. The two types ofexceptions are:

  • UnknownHostException
  • IOException

Get I/O stream objects

Once you have a Socket object, you can use that object to open input and output streams that allow you to transfer data between the clientand the server using the code shown in Listing 3 .

Listing 3 - Get I/O stream objects.
BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter outputStream =new PrintWriter(new OutputStreamWriter( socket.getOutputStream()),true);

Note that the true parameter in the last line of Listing 3 causes the output stream to flush automatically. Proper flushing is an importantaspect of socket programming.

Send a line of text to the server and display the echo

The code in Listing 4 uses the outputStream created above to send a line of text to the server, and then uses the inputStream created above to capture and display the echo that is returned from the server.

Listing 4 - Send a line of text to the server and display the echo.
//Send line of text to the server outputStream.println("This is an echo test");//Get echoed line back from server and display it System.out.println("echo: "+inputStream.readLine());//Close the socketsocket.close();

Then Listing 4 closes the socket.

You can view the remainder of the program in Listing 12 .

The essence of socket programming

That's really about all there is to socket programming from the client viewpoint.

Beyond this, the programming complexity associated with socket programming results from the requirement to implement an application protocol that willsuccessfully communicate with the server.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask