SOCKET PROBLEM: Server and client "deadlock"

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...
 
L

Lothar Kimmeringer

Ramon said:
while (true)

Why an endless loop if you describe a single write/read-step?
{
Socket clientSocket = s.accept();
ObjectOutputStream out
= new ObjectOutputStream( clientSocket.getOutputStream() ); [...]
while ( (size = f.read(buffer)) > 0 )
out.write(buffer, 0, size);

Why do you use an ObjectOutputStream and write the raw data
directly? The instantiation of an ObjectOutputStream already
leads to the write of some data, so if this data is also
present in the iso-file, you send this data twice.
while ( (size = f.read(buffer)) > 0 )
out.write(buffer, 0, size);
System.err.print("\t[DONE]\n");

Done without out.flush() is not done.
System.err.println("Downloading...");
int size;
while ( (size = in.read(buffer)) > 0 )
fo.write(buffer, 0, size);

This will never finish as long as you keep open the OutputStream
on the server-side. Only if you close it, the client will leave
this call and the read-function will return -1

If binary data being uploaded and a message being returned is
all you exchange between the two peers, you shouldn't use
ObjectOutputStream and ObjectInputStream. Both are caching
all objects that have been sent/received leading to Out-
OfMemoryErrors if this is a long-running process. The server
and the client can easily switch to a BufferedOutputStream
(don't forget the flush). The message can be sent by sending
the length of converted byte-array (or -1 if it's null) and
then the byte-array of the message that you retrieved by
message.getBytes("UTF8"). On the server-side you simply create
a String from that by doing a new String(bytes, "UTF8")


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top