0% found this document useful (0 votes)
49 views6 pages

19P220 Lab - 7

The document describes a program for client-server chat using TCP and UDP. It outlines the algorithm for the server and client. The server will create a socket, bind an address, accept connections, and send/receive data. The client will create a socket, connect to the server, and send/receive data. Code is provided for both the client and server classes to implement this algorithm. The output shows the program executes successfully.

Uploaded by

Maheshwaran
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)
49 views6 pages

19P220 Lab - 7

The document describes a program for client-server chat using TCP and UDP. It outlines the algorithm for the server and client. The server will create a socket, bind an address, accept connections, and send/receive data. The client will create a socket, connect to the server, and send/receive data. Code is provided for both the client and server classes to implement this algorithm. The output shows the program executes successfully.

Uploaded by

Maheshwaran
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/ 6

Exp No: 7

IMPLENTATION OF TRANSMISSION
Date: CONTROL PROTOCOL AND USER DATAGRAM
PROTOCOL

Aim:
To write a program for client - server chat using TCP.

Algorithm:
Server:

Step1: Start the program.


Step2: Include all the necessary header files and declare necessary variables.
Step3: Create a stream socket “sersock ” using socket() and check if the socket is created
successfully or not.
Step 4: Initialize the server socket structure variable ser_ip(family, port & address).
Step 5: Use bind() to assign the local socket address ser_ip to the socket identified by sersock.
Step 6: Accept the request to connect from the client by using the accept() and store the socket
information in n sersock.
Step7: Send and receive data to and from the client by using send () &recv() respectively,
print the received data.
Step 8: Goto step 7 until client sends “exit”. Step
9: Close all open sockets.
Step 10: Stop the program.
Client:
Step 1: Include all the necessary header files and declare necessary variables.
Step 2:Create a stream socket “clisock” using socket() and check if the socket is created
successfully of not.
Step 3: Initialize the client socket structure variable ip(family, port & address).
Step 4: Send a request to connect with the server using connect().
Step 5: Send and receive data to and from server using send() &recv() respectively.
Step 6: Repeat step 6 until either of the users wants to quit by typing “exit”. Step
7: Close all open sockets.
Step 8: Stop the program.

19p220
Program:
Client:
package dccnlab;
import java.net.*;
import java.io.*;
public class tcpclient
{
private Socket socket= null;
private DataInputStream input = null;
private DataOutputStream out = null;
public tcpclient(String address, int port)
{
try
{
socket = new Socket(address, port);
System.out.println("Connected");
input = new DataInputStream(System.in);
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
String line = "";
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)

19P113
{
System.out.println(i);
}
}
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
tcpclient client = new tcpclient("127.0.0.1", 5000);
}
}

Server:
package dccnlab;
import java.net.*;
import java.io.*;
public class tcpserver{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public tcpserver(int port){
try{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");

19P113
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
while (!line.equals("Over")){
try{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i){
System.out.println(i);
}
}
System.out.println("Closing connection");
socket.close();
in.close();
}
catch(IOException i){
System.out.println(i);
}
}
public static void main(String args[])
{
tcpserver server = new tcpserver(5000);
}
}

19P113
Output :

Result:
Thus to write a program for client - server chat using TCP has been executed successfully
and the output is verified

19P113

You might also like