R
Ramon
Hello again,
I'm writing a simple experimental file transfer server and client. The
file can be a binary file.
The problem is when:
* Server finishes transferring a file and waits for the client's input.
* Client, for some reason, "hangs" and waits for more data to be
transfered, even though the file is fully transfered.
* Thus, a deadlock occurs.
Is the a way to solve this problem?
/*
* SERVER CODE -- 1st if sends a file to the client,
* then it waits for a message from the client.
*/
ServerSocket s = new ServerSocket(60001);
String file = "/home/ramon/image.iso";
while (true)
{
Socket clientSocket = s.accept();
ObjectOutputStream out
= new ObjectOutputStream( clientSocket.getOutputStream() );
FileInputStream f = new FileInputStream(file);
byte buffer[] = new byte[1024];
System.err.println("Uploading file...");
int size;
while ( (size = f.read(buffer)) > 0 )
out.write(buffer, 0, size);
System.err.print("\t[DONE]\n");
// waits for the client
ObjectInputStream in = new ObjectInputStream(
clientSocket.getInputStream() );
String x = (String) in.readObject();
System.out.println("Msg from client: >>" + x + "<<");
clientSocket.close();
f.close();
}
/*
* CLIENT CODE: 1st it receives a file, then it
* sends a message to the server.
*/
Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 60001);
ObjectOutputStream outX = new ObjectOutputStream( s.getOutputStream() );
ObjectInputStream in = new ObjectInputStream( s.getInputStream() );
FileOutputStream fo = new FileOutputStream("/home/ramon/zdown/myfile.iso");
byte buffer[] = new byte[1024];
// downloads file
System.err.println("Downloading...");
int size;
while ( (size = in.read(buffer)) > 0 )
fo.write(buffer, 0, size);
fo.close();
System.err.println("File Downloaded.");
// sends a message
System.err.print("Type a message: ");
Scanner scan = new Scanner(System.in);
outX.writeObject((String) scan.nextLine());
System.err.println("Data send");
s.close();
/*
* OUTPUT FROM SERVER:
*/
Uploading file...
[DONE]
/*
* OUTPUT FROM CLIENT:
*/
Downloading...
I'm writing a simple experimental file transfer server and client. The
file can be a binary file.
The problem is when:
* Server finishes transferring a file and waits for the client's input.
* Client, for some reason, "hangs" and waits for more data to be
transfered, even though the file is fully transfered.
* Thus, a deadlock occurs.
Is the a way to solve this problem?
/*
* SERVER CODE -- 1st if sends a file to the client,
* then it waits for a message from the client.
*/
ServerSocket s = new ServerSocket(60001);
String file = "/home/ramon/image.iso";
while (true)
{
Socket clientSocket = s.accept();
ObjectOutputStream out
= new ObjectOutputStream( clientSocket.getOutputStream() );
FileInputStream f = new FileInputStream(file);
byte buffer[] = new byte[1024];
System.err.println("Uploading file...");
int size;
while ( (size = f.read(buffer)) > 0 )
out.write(buffer, 0, size);
System.err.print("\t[DONE]\n");
// waits for the client
ObjectInputStream in = new ObjectInputStream(
clientSocket.getInputStream() );
String x = (String) in.readObject();
System.out.println("Msg from client: >>" + x + "<<");
clientSocket.close();
f.close();
}
/*
* CLIENT CODE: 1st it receives a file, then it
* sends a message to the server.
*/
Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 60001);
ObjectOutputStream outX = new ObjectOutputStream( s.getOutputStream() );
ObjectInputStream in = new ObjectInputStream( s.getInputStream() );
FileOutputStream fo = new FileOutputStream("/home/ramon/zdown/myfile.iso");
byte buffer[] = new byte[1024];
// downloads file
System.err.println("Downloading...");
int size;
while ( (size = in.read(buffer)) > 0 )
fo.write(buffer, 0, size);
fo.close();
System.err.println("File Downloaded.");
// sends a message
System.err.print("Type a message: ");
Scanner scan = new Scanner(System.in);
outX.writeObject((String) scan.nextLine());
System.err.println("Data send");
s.close();
/*
* OUTPUT FROM SERVER:
*/
Uploading file...
[DONE]
/*
* OUTPUT FROM CLIENT:
*/
Downloading...