CN LAB Ex3
CN LAB Ex3
AIM
To write a java program for Echo client application using TCP Sockets Links
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);
Client:
RESULT:
Thus the java application program using TCP Sockets was developed and executed
successfully.
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:
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;
}
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];
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.