Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > SOCKET PROBLEM: Server and client "deadlock"

Reply
Thread Tools

SOCKET PROBLEM: Server and client "deadlock"

 
 
Ramon
Guest
Posts: n/a
 
      02-25-2009
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...
 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      02-25-2009
Ramon wrote:

> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
writing data form server socket to client socket greatestmclarenfan Java 2 02-16-2006 08:05 PM
Client socket disconnection event not received on Server socket java .nio Avizz Java 3 09-29-2003 10:47 AM



Advertisments