Program in Java to execute a simple connectionless communication between a server and client.
There are two types of protocol in the transport layer for communication:
1) TCP
2) UDP
In UDP (User Datagram Protocol), a connection is not formed online in TCP where the client sends a request to the server for a connection. The message is sent in the form of a datagram packet. Datagrams upon arrival contain the address of the sender which the server uses to send data to the correct client.
The algorithm for the whole process is:
SERVER:
1) Create a UDP socket-
serverSocket=new DatagramSocket();
2) Read request-
serverSocket.receive(requestPacket);
3) Write reply-
serverSocket.sent(reply packet, host, port);
4) Continue to read request.
CLIENT:
1) Create Datagram Socket for client-
clientSocket=new DatagramSocket();
2) Send request-
clientSocket.send(requestPacket,host,port);
3) Read reply-
clientSocket.receive(reply Packet);
4) Close-
clientSocket.close();
UDP advantages and disadvantages:
1) It is faster than TCP
2) Not reliable is a connection is not formed
3) No acknowledgment when the message is received.
Classes used:
1) DatagramPacket
2) DatagramSocket
Methods used:
1) receive()
2) send()
3) close()
DatagramPacket Objects:
1) DatagramPacket(byte[] buff, int length)
2) DatagramPacket(byte[] buff, int length, InetAddress address, int port)
## byte format is used to send or receive.
Inet Address:
Java representation of address-
1) InetAddress address = InetAddressgetByName("nameofthesite.com");
2) InetAddress address = InetAddress.getLocalHost();
Simplex Communication is one-way communication. It means that messages can be sent from one side and received from the other.
This is the implementation of simplex communication.
To run the program, first, run the server code then the client code.
Submitted by Purnima Agarwal (Purnima)
Download packets of source code on Coders Packet
Comments