Reputation: 51
I have to write java project for TCP Client/Server. The goal of the code is the Client asks server about a file. if the file is available it will be saved into client directory and show message the file is found and saved otherwise, the file is not found.
the code works when the file is in the Server directory and it will be saved in the client directory but if the file is not available it will show message in server side the file not available but in Client side it will save a new empty file with the name of the file that not available and message the File received
Can you help please
I have Server code
import java.io.*;
import java.net.*;
import java.util.*;
//********************
public class Server
{
static ServerSocket ServSock;
static int QLen = 6;
//***
public static void main(String args[])
{
try
{
//*** establish server socket
ServSock = new ServerSocket(Integer.parseInt(args[0]), QLen);
while (true)
{
//*** accept a client
Socket Sock = ServSock.accept();
//*** pass the client to a new thread
new Worker(Sock).start();
}
}
catch(IOException e)
{System.out.println(e);}
}
}
//*************************
class Worker extends Thread
{
Socket Sock;
PrintWriter PW;
BufferedReader DIS;
String Question = "Enter the file name to transfer from server:";
//**************
Worker(Socket S)
{Sock=S;}
//**************
public void run()
{
try
{
//*** thread identifies itself
System.out.println("Thread: " + getName());
//*** auto-flush
//*** set up socket I/O streams
PW = new PrintWriter (new BufferedWriter (new OutputStreamWriter(Sock.getOutputStream())),true);
DIS = new BufferedReader (new InputStreamReader (Sock.getInputStream()));
//*** send server question
PW.println(Question);
//*** wait for client response
String R = DIS.readLine();
Scanner in = null;
try{
BufferedOutputStream put=new BufferedOutputStream(Sock.getOutputStream());
BufferedReader st=new BufferedReader(new InputStreamReader(Sock.getInputStream()));
String str = "/Users/Batool/Desktop/HW/Server/";
String path = str + R;
System.out.println("The requested file is path: "+path);
System.out.println("The requested file is : "+R);
File f=new File(path);
System.out.println(f.isFile());
if(f.isFile())
{
//System.out.println("inside if");
FileInputStream fis=new FileInputStream(f);
System.out.println("File transfered");
System.out.println("———————————————————————————————");
byte []buf=new byte[1024];
int read;
while((read=fis.read(buf,0,1024))!=-1)
{
put.write(buf,0,read);
put.flush();
}
}//end if
else{ System.out.println("File not found");
System.out.println("———————————————————————————————");}//end else
}//end try
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
//System.exit(0);
}//end catch
//*** close this socket connection
Sock.close();
}
catch(IOException e)
{System.out.println(e);}
}
}
and I have Client code
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
static Socket sock;
static PrintWriter pw;
static BufferedReader br;
static String response = "";
//************************************
public static void main(String args[]) throws IOException
{
//*** establish connection to remote server
sock = new Socket(args[0],
Integer.parseInt(args[1])); //*** provide server name & port
//*** set up socket I/O streams
pw = new PrintWriter (new BufferedWriter (new OutputStreamWriter(sock.getOutputStream())),true);
br = new BufferedReader (new InputStreamReader (sock.getInputStream()));
//*** wait for server question
String r = br.readLine(); //*** THIS IS A BLOCKING CALL
System.out.println("Server asks: " + r);
//*** respond to server
/*Scanner in = new Scanner(System.in);
String s = in.nextLine();
pw.println(s);
pw.flush();
*/
Socket s=null;
BufferedInputStream get=null;
PrintWriter put=null;
try
{
get=new BufferedInputStream(sock.getInputStream());
put=new PrintWriter(sock.getOutputStream(),true);
String f;
int u;
//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(f);
String str = "/Users/Batool/Desktop/HW/Client";
FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
System.out.println(new File(str,f1.toString()));
System.out.println(f1.isFile());
byte jj[]=new byte[1024];
while((u=get.read(jj,0,1024))!=-1)
{
fs.write(jj,0,u);
}
fs.close();
System.out.println("File received");
System.out.println("———————————————————————————————");
sock.close();
}catch(Exception e)
{
System.out.println("File not found");
System.out.println("———————————————————————————————");
e.printStackTrace();
System.exit(0);
}
//*** close this socket connection
//sock.close();
}
}
Upvotes: 1
Views: 116
Reputation: 140525
Basically you need to implement a protocol.
Client asks server: do you have file X?
a) Servers says: yes, I got xyz bytes for you, and the md5sum is zyx
Then your client asks for the file; and it can verify it got the correct amount of bytes and that no transmission errors happened. (of course you don't need that verification part; but depending on your context, those are things that you should consider to have in place, too)
b) Server says: no, I don't have that file
Then your client gives an error message to its user.
In other words: the server just reading files and pushing bytes out will not do!
You want to turn your server into something that offers services. And a service has a contract, such as "input" and "output". And services need ways to "negotiate" what should happen next. In your current solution, everything that happen is hardcoded - meaning there is already a protocol in place, but that protocol is defined by the instructions that you put into your code!
Upvotes: 1