0% found this document useful (0 votes)
24 views2 pages

Krishna - Chavda - I077 - 60003220149 - CN - Experiment 5

The document describes a TCP socket programming experiment. It includes code for a TCP client and server that can exchange messages. The client connects to the server, sends a message, and receives a response back. The server accepts the client connection, receives the message, and sends a response. The experiment establishes a TCP connection between the client and server to demonstrate socket programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Krishna - Chavda - I077 - 60003220149 - CN - Experiment 5

The document describes a TCP socket programming experiment. It includes code for a TCP client and server that can exchange messages. The client connects to the server, sends a message, and receives a response back. The server accepts the client connection, receives the message, and sends a response. The experiment establishes a TCP connection between the client and server to demonstrate socket programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Department of Information Technology

COURSE CODE: DJS22ITL404 DATE: 13-03-2024


COURSE NAME: Computer Networks Laboratory CLASS: S. Y. B.Tech
NAME: Krishna Rajendra Chavda SAPID: 60003220149
ROLLNO: I077 BATCH: I2 – 1
Experiment No. 5
TCP Socket Programming

Code:
TCP Client:
import java.io.*;
import java.net.*;

public class TCPClient {


public static void main(String[] args) {
try {
// Connect to the server running on localhost at port 12345
Socket socket = new Socket("localhost", 49674);

// Create input and output streams for the socket


BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Send a message to the server


out.println("Hello, server!");

// Receive the response from the server


String response = in.readLine();
System.out.println("Server response: " + response);

// Close the connections


in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

TCP Server:
import java.io.*;
import java.net.*;

public class TCPServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(49674);
System.out.println("Server started. Waiting for client connection...");

Socket clientSocket = serverSocket.accept();


System.out.println("Client connected: " + clientSocket);

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Client: " + inputLine);
out.println("Server: Received - " + inputLine);
}

// Close the connections


in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:

CONCLUSION: In this experiment, We have performed and analyzed the Client side
program and Server side program using a TCP connection.

You might also like