0% found this document useful (0 votes)
23 views

Using TCP

Uploaded by

Varsha
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)
23 views

Using TCP

Uploaded by

Varsha
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/ 4

Using TCP/IP sockets, write a client – server program to make the client send the file

name and to make the server send back the contents of the requested file if present.

TCP Client
import java.net.*;
import java.io.*;
public class ReceiveFile {
public static void main(String[] args) throws IoException{
//create client Socket
Socket s=new Socket("localhost",2000);
//accept file name from keyboard
BufferedReader k=new BufferedReader(new InputStreamReader(System.in)
);
System.out.print("Enter file name: ");
String filename=k.readLine();
//send file name to server using DataOutputStream
DataOutputStream d=new DataOutputStream(s.getOutputStream());
d.writeBytes(filename +"\n");
//to read data sent from server
BufferedReader i=new BufferedReader(new InputStreamReader(s.getInputStream()))
;
String st;
//read first line from server into st
st=i.readLine();
//if file is found on server side, then send "Yes" else "No"
if(st.equals("Yes"))
{
//read and display the file contents coming from Server
while((st=i.readLine())!=null)
System.out.println(st);
//close all connections
i.close();
d.close();k.close();
s.close();
}
else
System.out.println("File not found");
}
}

TCP Server
//create a server that send file contents to client
import java.net.*;
import java.io.*;
public class SendFile {
public static void main(String[] args) throws IoException {
//create a server socket
ServerSocket se=new ServerSocket(2000);
//let the server wait until the connection is accepted by client
Socket q=se.accept();
System.out.println("Connection established successfully");
//to receive file name from client
BufferedReader v=new BufferedReader(new InputStreamReader(q.getInputStream(
)));
//to transfer file contents to client
DataOutputStream dr=new DataOutputStream(q.getOutputStream());
//read file name from client
String g=v.readLine();
FileReader f=null;
BufferedReader ff=null;
boolean b;
//create file class object with file name
File r=new File(g);
//test if file exists or not
if(r.exists())
b=true;
else
b=false;
//if file exists, send 'yes' to client else send 'no'
if(b==true) dr.writeBytes("Yes"+ "\n");
else dr.writeBytes("No"+"\n");
if(b==true)
{
//attach file to fileReader to read data
f=new FileReader(g);
//attach FileReader to BufferedReader
ff=new BufferedReader(f);
String qq;
//read from BufferedReader and write to DataOutputStream
while((qq=ff.readLine())!=null)
{
dr.writeBytes(qq+"\n");
}
dr.close();
ff.close();
v.close();
se.close();
q.close();
f.close();
}
}
}

Write a program on datagram socket for client/server to display the messages on


client side, typed at the server side.

Source Code:
UDP Client
import java.io.*;
import java.net.*;
public class UDPC
{
public static void main(String[] args)
{
DatagramSocket skt;
String str;
try
{
skt=new DatagramSocket(3000);
byte[] b = new byte[1000];
while(true){
DatagramPacket reply = new DatagramPacket(b,b.length);
skt.receive(reply);
str = new String(reply.getData(),0,reply.getLength());
System.out.println(“client received:”+str);
}

}
catch(Exception ex)
{
System.out.println(ex);

}
}
}

UDP Server
import java.io.*;
import java.net.*;
public class UDPS
{
public static void main(String[] args)
{
DatagramSocket skt=null;
int ch =0;
try
{
skt=new DatagramSocket();
InetAddress ip = InetAddress.getByName(“localhost”);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println(“Enter the message:”);

String msg = br.readLine();

DatagramPacket dp = new DatagramPacket(msg.getBytes(),msg.length(),ip,3000);


skt.send(dp);
System.out.println(“Do you wish to continue: 1 for no 0 for yes”);
ch = Integer.parseInt(br.readLine());

}while(ch==0);
} catch(Exception ex)
{
System.out.println(ex):
}
}
}

You might also like