Simple client Assignment Help

Assignment Help: >> A simple client–server example >> Simple client

Simple client:

We define a class, called HelloClient, for the client. As for the server, this has a number of instance variables relating to the sockets and streams that are needed to enable the client to communicate with the server. In this simple client we do not define a constructor, as there is nothing for it to do. The main work of the class, as in the server, is in a method called run.

The code for the first part of the class, including the run method, is as follows:

public class HelloClient
{
// streams used for communicating with server private InputStream is;
private BufferedReader fromServer;
private Socket socket; // socket to server

// use local host address for server
static final String SERVER_ADDRESS = "127.0.0.1";
static final int SERVER_PORT_NUMBER = 3000;

public void run ( )
{
// set up connection to the server try
{
socket = new Socket(SERVER_ADDRESS, SERVER_PORT_NUMBER);
openStreams( );

String messageFromServer = fromServer.readLine( ); System.out.println("Server said: " + messageFromServer);

closeStreams( );
socket.close( );
}
catch (IOException e)
{
System.out.println("Trouble contacting the server " + e);
}
}

The code in the run method carries out all the functions of this simple client with the aid of two helper methods, openStreams and closeStreams. First, it creates a Socket object that connects to a specified port at a specified server address. In this example, we use port 3000 again, as this is the port number expected by the simple server. The above code uses the special IP address "127.0.0.1", known as the loopback address or the local host because it indicates that we want to communicate with a server on the same computer as the client. To communicate with any other computer you can simply change the value of the constant called SERVER_ADDRESS to either the symbolic name or the IP address of the other computer.

Once the socket has been created and the connection established (this is the job of the accept method of the ServerSocket object in the server software above) we can go on to open the streams that are needed for communicating with the server. If anything goes wrong at this stage (for example, if the server software is not running) this will normally give rise to an IOException, which will be caught and handled by the catch block.

The openStreams method creates a BufferedReader object, referenced by the fromServer instance variable, for text input from the server. We then use the readLine method to obtain a line of message text from the server, and this message is displayed on the screen. Finally, we close the streams using the private method closeStreams and also close the socket, to terminate the connection to the server.

As with the server, the private helper methods are quite straightforward. We start with
openStreams, as follows:

// open streams for communicating with the server private void openStreams ( ) throws IOException
{
is = socket.getInputStream( );
fromServer = new BufferedReader(new InputStreamReader(is));
}

We use the getInputStream method of the Socket object to enable us to get input from the server. We use the InputStream object referenced by the instance variable is to create a BufferedReader object referenced by fromServer. Again, the reason for this layering or wrapping of streams is that a BufferedReader has more convenient ways to read data than the lower-level InputStream. For example, we can use the readLine method to read text input.

When we have finished with these input streams, it is important to close them and this is the role of the closeStreams method, whose code is as follows:

// close streams to server
private void closeStreams ( ) throws IOException
{
fromServer.close( );
is.close( );
}

As with the corresponding methods in the server class both of these methods, openStreams and closeStreams, can give rise to an IOException. This is declared in the method header and is handled by the code in the run method.

The client code is run by a main method located in the separate class,
TestHelloClient, as follows:

public class TestHelloClient
{
public static void main (String [] args)
{
HelloClient client1 = new HelloClient( );
client1.run( );
}
}

Before running the client, you need to start the corresponding server. Otherwise, an exception will occur when the client tries to create a socket linked to the server. When the client and server above run successfully, the client should produce the following output:

Server said: Hello Walrus

After this, the client and server both close their respective ends of the connection and terminate.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Simple client homework and assignments? Live Simple client experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Simple client homework help, java assignment help and Simple client projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd