Download SOCKET PROGRAMMING experiment and more Study Guides, Projects, Research Computer Networks in PDF only on Docsity!
EXPERIMENT โ 8
SOCKET PROGRAMMING
Aim โ To implement Socket programming in Java using UDP
Theory โ
Socket
Sockets allow communication between two different processes on the same or different
machines. To be more precise, it's a way to talk to other computers using standard Unix file
descriptors.
Socket Types โ
There are four types of sockets available to the users. The first two are most commonly used
and the last two are rarely used. Processes are presumed to communicate only between
sockets of the same type but there is no restriction that prevents communication between
sockets of different types.
1. Stream Sockets โ Delivery in a networked environment is guaranteed. If you send
through the stream socket three items "A, B, C", they will arrive in the same order โ
"A, B, C". These sockets use TCP (Transmission Control Protocol) for data
transmission. If delivery is impossible, the sender receives an error indicator. Data
records do not have any boundaries.
2. Datagram Sockets โ Delivery in a networked environment is not guaranteed. They're
connectionless because you don't need to have an open connection as in Stream
Sockets โ you build a packet with the destination information and send it out. They
use UDP (User Datagram Protocol).
3. Raw Sockets โ These provide users access to the underlying communication
protocols, which support socket abstractions. These sockets are normally datagram
oriented, though their exact characteristics are dependent on the interface provided
by the protocol.
4. Sequenced Packet Sockets โ They are similar to a stream socket, with the exception
that record boundaries are preserved. This interface is provided only as a part of the
Network Systems (NS) socket abstraction, and is very important in most serious NS
applications. Sequenced-packet sockets allow the user to manipulate the Sequence
Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a
group of packets, either by writing a prototype header along with whatever data is
to be sent, or by specifying a default header to be used with all outgoing data, and
allows the user to receive the headers on incoming packets.
6. Acknowledgment mechanism: -
The UDP does have any acknowledgment mechanism, i.e., there is no handshaking
between the UDP sender and UDP receiver. If the message is sent in TCP, then the
receiver acknowledges that I am ready, then the sender sends the data. In the case
of TCP, the handshaking occurs between the sender and the receiver, whereas in
UDP, there is no handshaking between the sender and the receiver.
7. Segments are handled independently: -
Each UDP segment is handled individually of others as each segment takes different
path to reach the destination. The UDP segments can be lost or delivered out of
order to reach the destination as there is no connection setup between the sender
and the receiver.
8. Stateless: -
It is a stateless protocol that means that the sender does not get the
acknowledgement for the packet which has been sent.
Java Socket programming can be connection-oriented (TCP) or connectionless (UDP). But for
this experiment we are considering Java socket programming using UDP protocol.
๏ท Socket and ServerSocket classes are used for connection-oriented socket
programming
๏ท DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
The client in socket programming must know two parameters โ
๏ท IP Address of Server, and
๏ท Port number.
UDPServer Code โ
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPServer { public final static int SERVICE_PORT = 50001 ; public static void main(String[] args) throws IOException { try { DatagramSocket serverSocket = new DatagramSocket(SERVICE_PORT); byte[] receivingDataBuffer = new byte[ 1024 ]; byte[] sendingDataBuffer = new byte[ 1024 ]; DatagramPacket inputPacket = new DatagramPacket(receivingDataBuffer, receivingDataBuffer.length); System.out.println("Waiting for a client to connect..."); serverSocket.receive(inputPacket); String receivedData = new String(inputPacket.getData()); System.out.println("Sent from the client: " + receivedData); sendingDataBuffer = receivedData.toUpperCase().getBytes(); InetAddress senderAddress = inputPacket.getAddress(); int senderPort = inputPacket.getPort(); DatagramPacket outputPacket = new DatagramPacket(sendingDataBuffer, sendingDataBuffer.length, senderAddress, senderPort); serverSocket.send(outputPacket); serverSocket.close(); } catch (SocketException e) { e.printStackTrace(); } } }
DatagramPacket sendingPacket = new DatagramPacket(sendingDataBuffer, sendingDataBuffer.length, IPAddress, SERVICE_PORT); clientSocket.send(sendingPacket); DatagramPacket receivingPacket = new DatagramPacket(receivingDataBuffer, receivingDataBuffer.length); clientSocket.receive(receivingPacket); String receivedData = new String(receivingPacket.getData()); System.out.println("Sent from the server: " + receivedData); clientSocket.close(); } catch (SocketException e) { e.printStackTrace(); } } }
Output โ
Conclusion โ Thus we have studied about UDP protocol and implemented java socket
programming using UDP.