0% found this document useful (0 votes)
10 views10 pages

06 CN

Uploaded by

bramhatechnocrat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

06 CN

Uploaded by

bramhatechnocrat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Vidyavardhini’s College of Engineering & Technology

Department of Computer Engineering


Academic Year : 2024-25

Experiment No 6

Aim: Socket programming using TCP or UDP.


Theory:

Sockets:

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

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.

When the connection is established, transfer of data takes place.

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:

In Computer operating system, a stream socket is type of interprocess communications socket or


network socket which provides a connection-oriented, sequenced, and unique flow of data without
record boundaries with well defined mechanisms for creating and destroying connections and for
detecting errors. It is similar to phone.
A connection is established between the phones (two ends) and a conversation (transfer of data)
takes place.

Program:

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

Client Side Program: import Server S ide Program:

java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.IOException;
import import java.io.InputStreamReader

java.io.InputStreamReader; import ; java.io.PrintWriter;

import java.io.PrintWriter; import java.net.ServerSocket;

import java.net.Socket; import java.net.Socket;

import java.util.HashSet;

public class ChatClient { import java.util.Set;

private static final


String public pri class ChatServer {
SERVER_ADDRESS =
"localhost"; // new vate static
Server IP address
Set<PrintWriter> clientWriters =
HashSet<>();
private static final int
SERVER_PORT = 12345; // public
Server port number main(String[]
static void
server args) {

public static void System.out.println("Chat


main(String[] args) { number started...");

try (Socket socket = new int port = 12345; //


Socket(SERVER_ADDRESS, Port for the server
SERVER_PORT);

BufferedReade
r
reader = new
BufferedReader(new

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

InputStreamReader(System.in));

PrintWriter out =

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

new try (ServerSocket


PrintWriter(socket.getOutputStream serverSocket = new
(), true); ServerSocket(port)) {

BufferedReader in while (true) {


= new BufferedReader(new
InputStreamReader(socket.getInputS Socket
tream()))) { clientSocket =
serverSocket.accept();

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

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

}).start(); ClientHandler(Socket socket) {

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

// Sending messages to this.socket = socket;


the server
}
String userMessage;

while ((userMessage =
@Override public
reader.readLine()) != null) {
void run() { try {
out.println(userMessage);
in = new
} BufferedReader(new
InputStreamReader(socket.getInputS
tream()));

} catch (IOException e) { out = new


PrintWriter(socket.getOutputStream
e.printStackTrace(); (), true);

} synchronized
(clientWriters) {
}

clientWriters.add(out);

String message;

while ((message =
in.readLine()) != null) {

System.out.println("Received: " +
message);

synchronized
(clientWriters) {

for
(PrintWriter writer :

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

clientWriters) {

writer.println(message);

} catch (IOException
e) {

e.printStackTrace(); }

finally

{ try {

socket.close();

} catch
(IOException e) {

e.printStackTrace();

synchronized
(clientWriters) {

clientWriters.remove(out);

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

}
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.

This setup enables multiple clients to connect and chat simultaneously.

CSL502:Computer Network Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year : 2024-25

Output:

Server Side:

Client Side:

Conclusion:
We have successfully completed the Socket programming using TCP or UDP.

CSL502:Computer Network Lab

You might also like