06 CN
06 CN
Experiment No 6
Sockets:
A socket is one endpoint of a two way communication link between two programs running on the
network. The socket mechanism provides a means of inter-process communication (IPC) by
establishing named contact points between which the communication takes place.
Socket are generally employed in client server applications. The server creates a socket, attaches it
to a network port addresses then waits for the client to contact it. The client creates a socket and
then attempts to connect to the server socket.
Types of Sockets :
There are two primary and common types of Sockets: the datagram socket and the stream socket.
1.Datagram Socket :
This is a type of network which has connection less point for sending and receiving packets. It is
similar to mailbox. The letters (data) posted into the box are collected and delivered (transmitted) to
a letterbox (receiving socket).
2.Stream Socket:
Program:
java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.IOException;
import import java.io.InputStreamReader
import java.util.HashSet;
BufferedReade
r
reader = new
BufferedReader(new
InputStreamReader(System.in));
PrintWriter out =
System.out.println("New client
System.out.println("Connected to connected: " +
chat server"); clientSocket.getInetAddress());
new
ClientHandler(clientSocket).start(
// Thread to listen );
for messages from the server
}
new Thread(() ->
} catch (IOException e) {
{ try {
e.printStackTrace();
String }
serverMessage;
}
while
((serverMessage = in.readLine()) !
= null) {
private static class
ClientHandler extends Thread {
System.out.println("Server: " +
serverMessage); private Socket socket;
}
private PrintWriter out;
} catch
private BufferedReader in;
(IOException e) {
e.printStackTrace(); public
while ((userMessage =
@Override public
reader.readLine()) != null) {
void run() { try {
out.println(userMessage);
in = new
} BufferedReader(new
InputStreamReader(socket.getInputS
tream()));
} synchronized
(clientWriters) {
}
clientWriters.add(out);
String message;
while ((message =
in.readLine()) != null) {
System.out.println("Received: " +
message);
synchronized
(clientWriters) {
for
(PrintWriter writer :
clientWriters) {
writer.println(message);
} catch (IOException
e) {
e.printStackTrace(); }
finally
{ try {
socket.close();
} catch
(IOException e) {
e.printStackTrace();
synchronized
(clientWriters) {
clientWriters.remove(out);
}
This code implements a simple client-server chat application using Java's networking features.
● Client-Side (ChatClient):
The client connects to a chat server (localhost on port 12345) using a socket. It listens for
incoming messages from the server in a separate thread while allowing the user to send
messages via the command line. Both incoming and outgoing messages are handled
through BufferedReader and PrintWriter.
● Server-Side (ChatServer):
The server listens for incoming client connections on port 12345. Each client connection
is managed by a new thread (ClientHandler). When a message is received from a
client, it broadcasts the message to all connected clients by using a synchronized set of
PrintWriter objects for communication.
Output:
Server Side:
Client Side:
Conclusion:
We have successfully completed the Socket programming using TCP or UDP.