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 [Link].*;
import [Link].*;
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([Link])
);
[Link]("Enter file name: ");
String filename=[Link]();
//send file name to server using DataOutputStream
DataOutputStream d=new DataOutputStream([Link]());
[Link](filename +"\n");
//to read data sent from server
BufferedReader i=new BufferedReader(new InputStreamReader([Link]()))
;
String st;
//read first line from server into st
st=[Link]();
//if file is found on server side, then send "Yes" else "No"
if([Link]("Yes"))
{
//read and display the file contents coming from Server
while((st=[Link]())!=null)
[Link](st);
//close all connections
[Link]();
[Link]();[Link]();
[Link]();
}
else
[Link]("File not found");
}
}
TCP Server
//create a server that send file contents to client
import [Link].*;
import [Link].*;
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=[Link]();
[Link]("Connection established successfully");
//to receive file name from client
BufferedReader v=new BufferedReader(new InputStreamReader([Link](
)));
//to transfer file contents to client
DataOutputStream dr=new DataOutputStream([Link]());
//read file name from client
String g=[Link]();
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([Link]())
b=true;
else
b=false;
//if file exists, send 'yes' to client else send 'no'
if(b==true) [Link]("Yes"+ "\n");
else [Link]("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=[Link]())!=null)
{
[Link](qq+"\n");
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}
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 [Link].*;
import [Link].*;
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,[Link]);
[Link](reply);
str = new String([Link](),0,[Link]());
[Link](“client received:”+str);
}
}
catch(Exception ex)
{
[Link](ex);
}
}
}
UDP Server
import [Link].*;
import [Link].*;
public class UDPS
{
public static void main(String[] args)
{
DatagramSocket skt=null;
int ch =0;
try
{
skt=new DatagramSocket();
InetAddress ip = [Link](“localhost”);
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
do
{
[Link](“Enter the message:”);
String msg = [Link]();
DatagramPacket dp = new DatagramPacket([Link](),[Link](),ip,3000);
[Link](dp);
[Link](“Do you wish to continue: 1 for no 0 for yes”);
ch = [Link]([Link]());
}while(ch==0);
} catch(Exception ex)
{
[Link](ex):
}
}
}