Socket Programming refers to writing programs that execute across multiple computers in which the devices are all connected to each other using a network. A Server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request. The client and server can communicate by writing to or reading from their socket.
A Socket is simply an endpoint for communication between machines.
There are two communication protocol that one can use for Socket Programming:
1) Transmission control protocol (TCP)
2) User Datagram Protocol (UDP)
Transmission Control Protocol.
TCP is a connection oriented protocol. In order to do communication over the TCP Protocol, a connection must be established between the pair of sockets. While on of the socket listen for connection request (Server), the other ask for connection (client). Once two socket have been connected, they can be used to transmit data in both direction.
Fig: Socket-based client and server programming.
Classes used for Socket programming:
1)Socket
Socket class represents the socket that both the client and the server use to communicate with each other.
2)ServerSocket
The ServerSocket class used by server application to obtain a port and listen for client requests.
Steps for creating a simple Server Program:
Step 1) Open the Server Socket.
ServerSocket ss=new ServerSocket(PORT);
Step 2) Wait for the client request.
Socket client=ss.accept();
Step 3) Create I/O Streams for the communicating to the client.
Receive data from server: => String str=dis.readUTF(); Send data to the server: => dos.writeUTF(“Hello”);
Step 4) Close the socket.
s.close();
User Datagram Protocol.
UDP is a connection less protocol that allows for packets of data to be transmitted. Datagram Packets are used to implement a connection less packet delivery service supported by the UDP Protocol. Each message is transferred from source machine to destination based on information contained within that packet.
The format of Datagram Packet is:
The class DatagramPacket contain several constructors that can be used for creating packet object. For Example: DatagramPacket(byte[] buff, int length, InetAddress address, int port);
The class datagramSocket support various methods that can be used for transmitting or receiving data over the network, The two key methods are:
void send(DatagramPacket p) => send a datagram packet from this socket.
void receive(DatagramPacket p) => receive a datagram packet from this socket.
Program 1.1: Develop a program for one way client and server communication using java Socket, where client sends a message to the server, then the server reads the message and print it.
Source Code:
Filename: DemoClient.java
import java.net.*;
import java.io.*;
public class DemoClient
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket(“localhost”,1234);
DataOutputStream dis=new DataOutputStream(s.getOutputStream()); dis.writeUTF(“Hello!! How are you”);
s.close();
}
}
Filename: DemoServer.java:
import java.net.*;
import java.io.*;
public class DemoServer
{
public static void main(String args[]) throws Exception