Work Program
Work Program
No: 3 Applications using TCP sockets like: Echo client and echo server,
Chat and File Transfer
AIM
To write a java program for application using TCP Sockets Links
ALGORITHM
SERVER SIDE
3. Create a socket for the server socket which accepts the connection.
ALGORITHM
CLIENT SIDE
2. Create a socket which binds the Ip address of server and the port address to acquire
service.
eclient.java:
import java.io.*;
import java.net.*;
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try{
c=new Socket("localhost",8080);
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);
if(!line.equals("exit"))
System.out.println("server:"+is1.readLine());
}while(!line.equals("exit"));}
catch(IOException e){
System.out.println("socket closed");
}}}
eserver.java:
import java.io.*;
import java.net.*;
{ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try{
s=new ServerSocket(8080);}
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();
ps.println(line);}}
catch(IOException e){System.out.println(e);}}}
OUTPUT
ALGORITHM
CLIENT
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
SERVER
5. The server accept the connection and to send the data from server to client and
6. vice versa
7. The server communicates the client to send the end of the message.
PROGRAM
TCPserver1.java
import java.net.*;
import java.io.*;
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);
TCPclient1.java
import java.net.*;
import java.io.*;
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
c=new Socket("localhost",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)
}}
OUTPUT:
ALGORITHM
SERVER SIDE
1. Start.
3. Server reads the filename and sends the data stored in the file for the‘get’ request.
4. It reads the data from the input stream and writes it to a file in theserver for the ‘put’
instruction.
6. Stop.
CLIENT SIDE
1. Start.
5. After getting approval from the server, the client either get file from the server or send
PROGRAM
import java.net.*;
import java.io.*;
class FileClient {
Socket s=null;
BufferedReader get=null;
PrintWriter put=null;
try
s=new Socket("127.0.0.1",8081);
put=new PrintWriter(s.getOutputStream(),true);
catch(Exception e)
System.exit(0);
String u,f;
System.out.println("Enter the file name to transfer from server:");
f=dis.readLine();
put.println(f);
while((u=get.readLine())!=null)
byte jj[]=u.getBytes();
fs.write(jj);
fs.close();
System.out.println("File received");
s.close();
import java.io.*;
import java.net.*;
ServerSocket ss=null;
try {
ss=new ServerSocket(8081);
}
catch(IOException e) {
System.out.println("couldn't listen");
System.exit(0);
Socket cs=null;
try {
cs=ss.accept();
System.out.println("Connection established"+cs);
catch(Exception e) {
System.out.println("Accept failed");
System.exit(1);
String s=st.readLine();
if(f.exists())
String line;
while((line=d.readLine())!=null)
{ put.write(line);
put.flush(); }
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
} } }
OUTPUT
RESULT:
Thus the socket program for Echo, Chat and File Transfer was developed and Executed
successfully.