The server code Assignment Help

Assignment Help: >> Datagram communication >> The server code

The server code:

Of course, there is no point in running the client unless we have a server already waiting to receive its messages. So next we must look at how to program a server for UDP communication. All that the server does is to receive a datagram repeatedly, extract the text message and display it on System.out. The code for the server is as follows:

import java.net. *;
import java.io. *;

public class DatagramServer
{
// number of bytes in a UDP packet
static final int UDP_PACKET_SIZE = 512;

// port number on which datagram is expected

static final int PORT_NUMBER = 4000;

// message which indicates that the client is quitting

static final String CLIENT_QUITTING = "Client quitting";

DatagramSocket datagramSocket; // the datagram socket

public DatagramServer ( )
{
System.out.println("...Server starting");
try
{
// set up datagram socket on port
datagramSocket = new DatagramSocket(PORT_NUMBER);
}
catch (IOException e)
{
System.out.println("Problem setting up DatagramSocket:" + e);
}
}

public void receiveData ( )
{
String message; // message from client
DatagramPacket packet; // the packet of data received
// buffer for receiving a fixed-size UDP packet, byte [] buffer = new byte [UDP_PACKET_SIZE ];


try
{

// continue reading packets until exit message

boolean morePackets = true;
while (morePackets)
{

// create an empty packet to receive the data
packet = new DatagramPacket(buffer, buffer.length);

// receive a packet from the client

datagramSocket.receive(packet);

message = new String(packet.getData( ), 0, packet.getLength( ));
System.out.println("Message received is '" +
message + "'");
morePackets = (!message.equals(CLIENT_QUITTING));
}
System.out.println("...Server Terminating");
}
catch (IOException e)
{
System.out.println("Problem receiving packet:" + e);
}
}
}

The constructor for the DatagramServer class sets up a datagram socket on the same port number used by the client when sending the datagrams. Note the need to specify a port number in this case, unlike when setting up the datagram socket in the client code. This is because the server needs to know on which port to listen for incoming packets.

The main work of the server is carried out in the method receiveData. As long as it is expecting more packets, it creates an empty datagram packet and waits for a datagram to arrive on the specified port. It then stores that datagram in the empty packet and uses the getData and getLength methods of the DatagramPacket object to extract the message text from the datagram. It displays the message string on System.out before returning to wait for the next message. The server terminates when it receives the special message text from the client telling it that the client is terminating.

To run this class we use the main method of class TestDatagramServer, which is as follows:

public class TestDatagramServer
{
public static void main (String [] args)
{
DatagramServer server = new DatagramServer( );
server.receiveData( );
}
}

This simply creates a DatagramServer object to set up the socket and invokes its receiveData method to enable it to accept datagram packets from the client. In this case, as we have seen, the server program terminates if the client sends its termination
message. Alternatively, we could change the server code so that the server continues running in order to receive messages from other clients. 

 

Java Assignment Help - Java Homework Help

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