Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > File over socket problem

Reply
Thread Tools

File over socket problem

 
 
jwarzech@gmail.com
Guest
Posts: n/a
 
      04-25-2007
I am trying to send a zip file from a client to a server using
sockets. I can get the file to transfer all right but the server
"halts" after the send. Using trace debugging I can see that the while
loop is exiting but any statement afterwards is not being processed.

Client Code

output = new DataOutputStream(client.getOutputStream());
byte[] buffer = new byte[1024];
int r;
InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
while((r = in.read(buffer)) > 0)
{
output.write(buffer,0,r);
}
output.flush();
input.close();
output.close();



Server Code

input = new DataInputStream(connection.getInputStream());
OutputStream out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int r;
while((r = input.read(buffer)) > 0)
{
out.write(buffer,0,r);
}
out.flush();
out.close();
input.close();

//This doesn't display, any idea??
System.out.println("Completed");

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am trying to send a zip file from a client to a server using
> sockets. I can get the file to transfer all right but the server
> "halts" after the send. Using trace debugging I can see that the while
> loop is exiting but any statement afterwards is not being processed.
>
> Client Code
>
> output = new DataOutputStream(client.getOutputStream());
> byte[] buffer = new byte[1024];
> int r;
> InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
> while((r = in.read(buffer)) > 0)
> {
> output.write(buffer,0,r);
> }
> output.flush();
> input.close();
> output.close();
>
>
>
> Server Code
>
> input = new DataInputStream(connection.getInputStream());
> OutputStream out = new FileOutputStream(outFile);
> byte[] buffer = new byte[1024];
> int r;
> while((r = input.read(buffer)) > 0)
> {
> out.write(buffer,0,r);
> }
> out.flush();
> out.close();
> input.close();
>
> //This doesn't display, any idea??
> System.out.println("Completed");
>


Without seeing all of the code it is going to be difficult to diagnose.
Are you checking for Exceptions?

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Doezel
Guest
Posts: n/a
 
      04-26-2007
>I am trying to send a zip file from a client to a server using
> sockets. I can get the file to transfer all right but the server
> "halts" after the send. Using trace debugging I can see that the while
> loop is exiting but any statement afterwards is not being processed.
>
> Client Code
>
> output = new DataOutputStream(client.getOutputStream());
> byte[] buffer = new byte[1024];
> int r;
> InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
> while((r = in.read(buffer)) > 0)
> {
> output.write(buffer,0,r);
> }
> output.flush();
> input.close();
> output.close();
>
>
>
> Server Code
>
> input = new DataInputStream(connection.getInputStream());
> OutputStream out = new FileOutputStream(outFile);
> byte[] buffer = new byte[1024];
> int r;
> while((r = input.read(buffer)) > 0)
> {
> out.write(buffer,0,r);
> }
> out.flush();
> out.close();
> input.close();
>
> //This doesn't display, any idea??
> System.out.println("Completed");


I'm not that experienced with java. But I think the server keeps on waiting
for input. How would the server know when the client doesn't want to send
anymore? I would at first send the filesize to the server and let the server
read that number of bytes. That way the server can close itself after it has
received enough bytes.

 
Reply With Quote
 
Doezel
Guest
Posts: n/a
 
      04-26-2007
> while((r = input.read(buffer)) > 0)
> {
> out.write(buffer,0,r);
> }


You should check if r>-1 instead of r>0.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      04-26-2007
Doezel wrote:
>> while((r = input.read(buffer)) > 0)
>> {
>> out.write(buffer,0,r);
>> }

>
> You should check if r>-1 instead of r>0.


Doezel has a point there. That could be what is hanging your program.
The read returns 0 bytes sometimes before it returns -1 to signal end of
stream. Anyway 0 bytes is possible.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Z.
Guest
Posts: n/a
 
      04-26-2007
Doezel wrote:
>> while((r = input.read(buffer)) > 0)
>> {
>> out.write(buffer,0,r);
>> }


> You should check if r>-1 instead of r>0.


NO! Check for != -1.

It's possible to read 0 bytes before "End Of Stream" (-1).
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      04-30-2007
Knute Johnson wrote:
> Doezel wrote:
>
>>> while((r = input.read(buffer)) > 0)
>>> {
>>> out.write(buffer,0,r);
>>> }

>>
>>
>> You should check if r>-1 instead of r>0.

>
>
> Doezel has a point there. That could be what is hanging your program.
> The read returns 0 bytes sometimes before it returns -1 to signal end of
> stream. Anyway 0 bytes is possible.


No he doesn't. InputStream.read() is specified to return either -1
meaning end of stream or a value between 1 and the implicit or explicit
buffer size. Reading zero bytes is only possible if you supply a length
of zero, or in channel I/O in non-blocking mode.
 
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') Steve Holden Python 0 02-01-2009 12:45 PM
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
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM



Advertisments