0% found this document useful (0 votes)
159 views9 pages

CN LAB Ex3

Tcp 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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views9 pages

CN LAB Ex3

Tcp 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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex.

No:3a Applications using TCP sockets : Echo client

AIM
To write a java program for Echo client application using TCP Sockets Links

PRE LAB DISCUSSION:


1. In the TCP Echo client a socket is created. Using the socket a connection is made to
the server using the connect() function. After a connection is established, we send
messages input from the user and display the data received from the server using
send() and read() functions.
2. In the TCP Echo server, we create a socket and bind to a advertised port number.
After binding the process listens for incoming connections. Then an infinite loop is
started to process the client requests for connections. After a connection is requested,
it accepts the connection from the client machine and forks a new process.
3. The new process receives data from the client using recv() function and echoes the
same data using the send() function. Please note hat this server is capable of handling
multiple clients as it forks a new process for every client trying to connect to the
server. TCP socket routines enable reliable IP communication using the transmission
control protocol(TCP).
4. The implementation of the Transmission Control Protocol (TCP) in the Network
Component. TCP runs on top of the Internet Protocol (IP). TCP is a connection-
oriented and reliable, full duplex protocol supporting a pair of byte streams, one for
each direction.
5. A TCP connection must be established before exchanging data. TCP retransmits data
that do not reach the final destination due to errors or data corruption. Data is
delivered in the sequence of its transmission

ALGORITHM
Client
1. Start
2. Create the TCP socket
3. Establish connection with the server
4. Get the message to be echoed from the user
5. Send the message to the server
6. Receive the message echoed by the server
7. Display the message received from the server
8. Terminate the connection
9. Stop

Server
1. Start
2. Create TCP socket, make it a listening socket
3. Accept the connection request sent by the client for connection establishment
4. Receive the message sent by the client
5. Send the received message to the client from which it receives
6. Close the connection when client initiates termination and server becomes a
listening server, waiting for clients.
7. Stop.
PROGRAM:
Serv
er:

import java.net.*;
import java.io.*;
public class ES
{
public static void main(String args[])
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
{
line=is.readLine();
if ((line).equals("END"))
{
System.out.println("Client Closed");
break;
}
ps.println(line);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Client:
import java.net.*;
import java.io.*;
public class EC
{
public static void main(String args[])
{
String line;
try
{
InetAddress ia=InetAddress.getLocalHost();
Socket c=new Socket(ia,9000);

PrintStream os=new PrintStream(c.getOutputStream());


DataInputStream is=new DataInputStream(System.in);
DataInputStream is1=new DataInputStream(c.getInputStream());
while(true)
{
System.out.println("Client:");
line=is.readLine();
if ((line).equals("END"))
{
System.out.println("Client Closed");
break;
}
os.println(line);
System.out.println("Server:"+is1.readLine());
}
}
catch(IOException e)
{
System.out.println("Socket Closed!");
}
}
}
OUTPUT
Server

Client:
RESULT:
Thus the java application program using TCP Sockets was developed and executed
successfully.

Ex.No:3b Applications using TCP sockets : Chat


AIM:
To write a java program for Echo client application using TCP Sockets Links

ALGORITHM:
CLIENT
1. Start the program
2. Include necessary package in java
3. To create a socket in client to server.
4. The client establishes a connection to the server.
5. The client accept the connection and to send the data from client to server.
6. The client communicates the server to send the end of the message
7. Stop the program.
SERVER
1. Start the program
2. Include necessary package in java
3. To create a socket in server to client
4. The server establishes a connection to the client.
5. The server accept the connection and to send the data from server to client and
6. vice versa
7. The server communicate the client to send the end of the message.
8. Stop the program.
PROGRAM:
SERVER:
import java.net.*;
import java.io.*;
public class Chatserver
{
public static void main(String arg[])
{
ServerSocket s=null;
String line;
DataInputStream is=null,is1=null;
PrintStream os=null;
Socket c=null;
try
{
s=new ServerSocket(9999);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
is1=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
do
{
line=is.readLine();
System.out.println("Client:"+line);
System.out.println("Server:");
line=is1.readLine();
os.println(line);
}
while(line.equalsIgnoreCase("quit")==false);
is.close();
os.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}

CLIENT:
import java.net.*;
import java.io.*;
public class Chatclient
{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("172.28.208.1",9999);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
do
{
System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine());
}
while(line.equalsIgnoreCase("quit")==false);
is1.close();
os.close();
}
catch(IOException e)
{
System.out.println("Socket Closed!Message Passing is over");
}
}
}

OUTPUT:

Viva Questions and Answers:


1.Define chat.
A real-time communication via keyboard between two or more users on a local
network (LAN) or over the Internet
2.Define TCP.
TCP/IP stands for Transmission Control Protocol/Internet Protocol, which is a set of
networking protocols that allows two or more computers to communicate. The Defense
Data Network, part of the Department of Defense, developed TCP/IP, and it has been
widely adopted as a networking standard.
3.what is socket API?
A socket API is an application programming interface (API), usually provided by the
operating system, that allows application programs to control and use network sockets.
Internet socket APIs are usually based on the Berkeley sockets standard.
RESULT:
Thus the above program a client-server application for chat using TCP / IP was
executed and successfully

Ex.No:3C Applications using TCP sockets : FILE TRANSFER

AIM
To write a java program for File transfer application using TCP Sockets Links
Algorithm
Server
1. Import java packages and create class fileserver.
2. Create a new server socket and bind it to theport.
3. Accept the clientconnection
4. Get the file name and stored into theBufferedReader.
5. Create a new object class file andrealine.
6. If file is exists then FileReader read the content until EOF isreached.
7. Stop theprogram.

Client
1. Import java packages and create class fileserver.
2. Create a new server socket and bind it to the port.
3. Now connection is established.
4. The object of a BufferReader class is used for storing data content which
has been retrieved from socketobject.
5. The connection is closed.
6. Stop the program.

PROGRAM
File Server :
import java.net.*;
import java.io.*;
public class Fileserver
{
public static void main (String [] args ) throws IOException
{
ServerSocket servsock = new ServerSocket(13267);
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
File myFile = new File("C:/Java/jdk-17.0.1/bin/a.html");
byte[] mybytearray;
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
long length = myFile.length();
System.out.println("FIle Length is="+length);
long current=0;
while(current!=length)
{
int size=100;
if(length - current >= size)
{
current += size;
}
else
{
size = (int)(length - current);
current = length;
}

mybytearray=new byte [size];


System.out.println("\nTime required in ms is="+System.currentTimeMillis());
bis.read(mybytearray,0,size);
os.write(mybytearray,0,size);
System.out.print("\nSending file ... "+size+"=="+(current*100)/length+"%
complete!");
}

os.flush();
sock.close();
}
}

File Client:
import java.net.*;
import java.io.*;
public class Fileclient
{
public static void main (String [] args ) throws IOException
{
int bytesRead=0;
byte[] mybytearray=new byte[100000];

Socket sock = new Socket("172.28.208.1",13267);


System.out.println("Connecting...");
// receive file
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("C:/Java/jdk-17.0.1/bin/a1.html");
BufferedOutputStream bos = new BufferedOutputStream(fos);
while((bytesRead=is.read(mybytearray))!=-1)
{
bos.write(mybytearray, 0, bytesRead);
System.out.println("\nBytes read is="+bytesRead);
System.out.println("\nTime required in ms is="+System.currentTimeMillis());
}

bos.flush();
bos.close();
sock.close();
}
}
a.html (saved in C:\java\jdk\bin)
<html>
<body style="background-color:pink">
Hai All! Click here to download the notes for Java Streams<br>
<br><a
href="https://drive.google.com/file/d/1Dw8IYSSByQ9kPu1A39r25UnLtWzP6HCV/view?
pli=1"> I/O STREAMS</a>
</body>
</html>

OUTPUT

RESULT:
Thus the java application program using TCP Sockets was developed and
executed successfully.

You might also like