[go: up one dir, main page]

0% found this document useful (0 votes)
75 views13 pages

Work Program

The document describes programs for TCP socket applications including an echo client and server, a chat client and server, and a file transfer client and server. The echo client and server programs are implemented by creating sockets, sending and receiving data between the client and server, and closing the sockets. The chat client and server establish a connection, allow sending and receiving messages back and forth until "quit" is received, then close the sockets. The file transfer programs have a client that requests a file from the server or sends a file to the server, and a server that receives file requests, reads and sends the requested file, or receives and saves a transferred file.

Uploaded by

Clash Clan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views13 pages

Work Program

The document describes programs for TCP socket applications including an echo client and server, a chat client and server, and a file transfer client and server. The echo client and server programs are implemented by creating sockets, sending and receiving data between the client and server, and closing the sockets. The chat client and server establish a connection, allow sending and receiving messages back and forth until "quit" is received, then close the sockets. The file transfer programs have a client that requests a file from the server or sends a file to the server, and a server that receives file requests, reads and sends the requested file, or receives and saves a transferred file.

Uploaded by

Clash Clan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex.

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

3A)ECHO CLIENT AND ECHO SERVER

ALGORITHM

SERVER SIDE

1. Start the program.

2. Create a server socket to activate the port address.

3. Create a socket for the server socket which accepts the connection.

4. After establishing connection receive the data from client.

5. Print and send the same data to client.

6. Close the socket.

7. End the program.

ALGORITHM

CLIENT SIDE

1. Start the program.

2. Create a socket which binds the Ip address of server and the port address to acquire

service.

3. After establishing connection send a data to server.

4. Receive and print the same data from server.

5. Close the socket.

6. End the program.


PROGRAM:

eclient.java:

import java.io.*;

import java.net.*;

public class eclient

public static void main(String args[]){

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.*;

public class eserver

{public static void main(String args[])throws IOException

{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();

System.out.println("msg received and sent back to client");

ps.println(line);}}

catch(IOException e){System.out.println(e);}}}

OUTPUT

3B) CLIENT- SERVER APPLICATION FOR CHAT

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 communicates the client to send the end of the message.

8. Stop the program.

PROGRAM

TCPserver1.java

import java.net.*;

import java.io.*;

public class TCPserver1

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);

TCPclient1.java

import java.net.*;

import java.io.*;

public class TCPclient1

public static void main(String arg[])

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)

System.out.println("Socket Closed!Message Passing is over");

}}
OUTPUT:

3.C) FILE TRANSFER IN CLIENT & SERVER

ALGORITHM

SERVER SIDE

1. Start.

2. Implement a server socket that listens to a particular port number.

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.

5. Exit upon client’s request.

6. Stop.

CLIENT SIDE

1. Start.

2. Establish a connection between the Client and Server.

3. Socket ss=new Socket(InetAddress.getLocalHost(),1100);

4. Implement a client that can send two requests.

i) To get a file from the server.


ii) To put or send a file to the server.

5. After getting approval from the server, the client either get file from the server or send

6. Send file to the server.

PROGRAM

CLIENT SIDE (FileClient.java)

import java.net.*;

import java.io.*;

class FileClient {

public static void main(String srgs[])throws IOException

Socket s=null;

BufferedReader get=null;

PrintWriter put=null;

try

s=new Socket("127.0.0.1",8081);

get=new BufferedReader(new InputStreamReader(s.getInputStream()));

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:");

DataInputStream dis=new DataInputStream(System.in);

f=dis.readLine();

put.println(f);

File f1=new File("output");

FileOutputStream fs=new FileOutputStream(f1);

while((u=get.readLine())!=null)

byte jj[]=u.getBytes();

fs.write(jj);

fs.close();

System.out.println("File received");

s.close();

SERVER SIDE (FileServer.java)

import java.io.*;

import java.net.*;

public class FileServer {

public static void main(String args[])throws IOException

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);

PrintWriter put=new PrintWriter(cs.getOutputStream(),true);

BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));

String s=st.readLine();

System.out.println("The requested file is : "+s);

File f=new File(s);

if(f.exists())

BufferedReader d=new BufferedReader(new FileReader(s));

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.

You might also like