Simple server Assignment Help

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

Simple server:

We define a class for the server called HelloServer. This has a number of instance variables concerned with the sockets and streams needed for the client and server to communicate. The first part of the class, including the constructor, is shown below. For clarity, we will defer explanations of some of the instance variables at this stage - these will be discussed when we look at the methods of this class later in this section.

public class HelloServer
{
private ServerSocket serverSocket;
private Socket socket; // socket to link to the client

// streams for communication to client
private OutputStream os;
private PrintWriter toClient;

// use a high numbered non-dedicated port
static final int PORT_NUMBER = 3000;
static final String MESSAGE_TO_CLIENT = "Hello Walrus";

// constructor
public HelloServer ( )
{

try
{

 

}

// create a ServerSocket object to listen on the port serverSocket = new ServerSocket(PORT_NUMBER);

catch (IOException e)
{
System.out.println("Trouble on port " +
PORT_NUMBER + ": " + e);
}
} // end constructor
...

The constructor does only one thing - it creates a ServerSocket object, which will permit the server to listen on the specified port waiting for a client to attempt to make a connection. In this case we use port 3000, but there is nothing special about this number except that it is out of the range 0 -1023 dedicated to standard services. The constructor for a ServerSocket object can generate an IOException, so we provide code to catch and report this.

The main work of the server is carried out by the run method, as follows:

public void run ( )
{
try
{
// wait for a connection request socket = serverSocket.accept( );

openStreams( );

toClient.println(MESSAGE_TO_CLIENT);

closeStreams( );
socket.close( );
}
catch (IOException e)
{
System.out.println("Trouble with a connection " + e);
}
}

Here, the accept method of the ServerSocket object causes the server to wait until a client attempts to make a connection. This attempt occurs when the client software creates a socket on the client computer. When a client does attempt to make a connection, a socket is created at the server and a reference to this new socket is stored in the instance variable socket. Next, we must use the private method openStreams to open the streams that allow the server to send a message to the client. 

We then send a message to the client using the PrintWriter object referenced by toClient. Finally, we close the streams using the private method closeStreams and also close the socket to terminate the connection to the client.

We can take a closer look at the private methods, starting with openStreams, as follows:

// set up streams for communicating with the client
private void openStreams( ) throws IOException
{
final boolean AUTO_FLUSH = true;
os = socket.getOutputStream( );
toClient = new PrintWriter(os, AUTO_FLUSH);
}

We use the getOutputStream method of the Socket object to obtain the OutputStream that will enable us to send output to the client. We use the OutputStream object referenced by the instance variable os to create a PrintWriter object referenced by toClient. Essentially, the PrintWriter object wraps around the OutputStream object, and offers a more convenient way to send data using the println method. This PrintWriter object is defined to be autofiushed: that is, the output will be sent immediately after execution of a println method, rather than being held in a buffer.

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

// close output streams to client
private void closeStreams ( ) throws IOException
{
toClient.close( );
os.close( );
}

Both of these methods, openStreams and closeStreams, can give rise to an IOException. Since they do not handle the exception within the method, it must be declared in the method header. The exception will then be handled by the code in the try-catch statement of the run method that invokes these two helper methods.

This completes the code for the simple server, except that in order to run the server we must have a main method and this is located in a separate class, TestHelloServer, as follows:

public class TestHelloServer
{
// create a server and have it greet the client public static void main (String [] args)
{
HelloServer server1 = new HelloServer( );
server1.run( );
}
}

The main method creates a HelloServer object and invokes its run method. Until we have a client to interact with this server, this causes the server to wait when it reaches the accept method invocation on the ServerSocket object. 

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Simple server homework and assignments? Live Simple server experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Simple server homework help, java assignment help and Simple server 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