Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple socket conumdrum

Reply
Thread Tools

Simple socket conumdrum

 
 
Will Hartung
Guest
Posts: n/a
 
      01-19-2005
I have a brain dead Client and equally unsophisticated Server program.

The Client connects to the Server, server pops a thread, and then they have
a simple conversation.

The basic goal of the C/S system is simply to Move Data in order to evaluate
timings and what not for a project.

I don't care WHAT the data is, I just want to move some.

Here's the meat of the Server:

I get the InputStream using:

InputStream is = socket.getInputStream();

My blocksize is 8192.

Then, I run this little loop:
while(totalRcvd != totalCnt) {
int amtToRead = blocksize;
if (amtToRead > (totalCnt - totalRcvd)) {
amtToRead = totalCnt - totalRcvd;
}
int amtRcvd = is.read(buffer, 0, amtToRead);
totalRcvd = totalRcvd + amtRcvd;
}

For a small amount of data, 10240 bytes, this works fine.

When I bump up to 102400 bytes, it hangs about 70% of the way into the total
amount, meanwhile the client thinks that it has completed sending the data
and is awaiting acknowledgment from the server.

If I kill the client at this point, then the sockets are closed and I get an
appropriate exception on the server.

Finally, running the client and server on my windows machine, it works
(though it is not consistent). It consistently fails with the server on a
Solaris 8 machine.

Anyone have any hints why this is hanging up?

Thanx!

Regards,

Will Hartung
()


 
Reply With Quote
 
 
 
 
dar7yl
Guest
Posts: n/a
 
      01-20-2005
> When I bump up to 102400 bytes, it hangs about 70% of the way into the
> total
> amount, meanwhile the client thinks that it has completed sending the data
> and is awaiting acknowledgment from the server.


It sounds like the http socket is timing out.
Try turning off buffering for the output, and write a block at a time.
(btw, I didn't see anywhere you are actually writing the data to the
client)

as a matter of style, recode your loop as follows:
(the brace style is your own preference, I abhor K&R style)
<code>
int remainder = totalCnt;
while (remainder > 0)
{
int amtToRead = remainder ;
if (amtToRead > BLOCKSIZE)
{
amtToRead = BLOCKSIZE;
}
int amtRcvd = is.read(buffer, 0, amtToRead);
remainder -= amtRcvd;

// now, what are you doing with your buffer here?
} // while
</code>

regards,
Dar7yl

"Will Hartung" <> wrote in message
news:...
>I have a brain dead Client and equally unsophisticated Server program.
>
> The Client connects to the Server, server pops a thread, and then they
> have
> a simple conversation.
>
> The basic goal of the C/S system is simply to Move Data in order to
> evaluate
> timings and what not for a project.
>
> I don't care WHAT the data is, I just want to move some.
>
> Here's the meat of the Server:
>
> I get the InputStream using:
>
> InputStream is = socket.getInputStream();
>
> My blocksize is 8192.
>
> Then, I run this little loop:
> while(totalRcvd != totalCnt) {
> int amtToRead = blocksize;
> if (amtToRead > (totalCnt - totalRcvd)) {
> amtToRead = totalCnt - totalRcvd;
> }
> int amtRcvd = is.read(buffer, 0, amtToRead);
> totalRcvd = totalRcvd + amtRcvd;
> }
>
> For a small amount of data, 10240 bytes, this works fine.
>
> When I bump up to 102400 bytes, it hangs about 70% of the way into the
> total
> amount, meanwhile the client thinks that it has completed sending the data
> and is awaiting acknowledgment from the server.
>
> If I kill the client at this point, then the sockets are closed and I get
> an
> appropriate exception on the server.
>
> Finally, running the client and server on my windows machine, it works
> (though it is not consistent). It consistently fails with the server on a
> Solaris 8 machine.
>
> Anyone have any hints why this is hanging up?
>
> Thanx!
>
> Regards,
>
> Will Hartung
> ()
>
>



 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      01-20-2005

"Will Hartung" <> wrote in message
news:...
> I have a brain dead Client and equally unsophisticated Server program.
>
> The Client connects to the Server, server pops a thread, and then they

have
> a simple conversation.
>
> The basic goal of the C/S system is simply to Move Data in order to

evaluate
> timings and what not for a project.
>
> I don't care WHAT the data is, I just want to move some.
>
> Here's the meat of the Server:
>
> I get the InputStream using:
>
> InputStream is = socket.getInputStream();
>
> My blocksize is 8192.
>
> Then, I run this little loop:
> while(totalRcvd != totalCnt) {
> int amtToRead = blocksize;
> if (amtToRead > (totalCnt - totalRcvd)) {
> amtToRead = totalCnt - totalRcvd;
> }
> int amtRcvd = is.read(buffer, 0, amtToRead);
> totalRcvd = totalRcvd + amtRcvd;
> }


Just for completeness sake, are you sure you set totalRcvd to 0 after each
message? Also, how is totalCnt count assigned? Are you sure it's the same
for both? (e.g. is it sent as a 2-byte prefix or a 4-byte prefix?) Is the
data being sent as bytes (not characters) down a corresponding Output
Stream?

Cheers,
Matt Humphrey http://www.iviz.com/


 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      01-20-2005
Will Hartung wrote:
> I have a brain dead Client and equally unsophisticated Server program.
>
> The Client connects to the Server, server pops a thread, and then they have
> a simple conversation.
>
> The basic goal of the C/S system is simply to Move Data in order to evaluate
> timings and what not for a project.
>
> I don't care WHAT the data is, I just want to move some.
>
> Here's the meat of the Server:
>
> I get the InputStream using:
>
> InputStream is = socket.getInputStream();
>
> My blocksize is 8192.
>
> Then, I run this little loop:
> while(totalRcvd != totalCnt) {
> int amtToRead = blocksize;
> if (amtToRead > (totalCnt - totalRcvd)) {
> amtToRead = totalCnt - totalRcvd;
> }
> int amtRcvd = is.read(buffer, 0, amtToRead);
> totalRcvd = totalRcvd + amtRcvd;
> }
>
> For a small amount of data, 10240 bytes, this works fine.
>
> When I bump up to 102400 bytes, it hangs about 70% of the way into the total
> amount, meanwhile the client thinks that it has completed sending the data
> and is awaiting acknowledgment from the server.
>
> If I kill the client at this point, then the sockets are closed and I get an
> appropriate exception on the server.
>
> Finally, running the client and server on my windows machine, it works
> (though it is not consistent). It consistently fails with the server on a
> Solaris 8 machine.
>
> Anyone have any hints why this is hanging up?


Nothing jumps out at me. Try printing totalRcvd and amtToRead just before
you call is.read(), and printing totalRcvd just after it returns, and
see if they make sense.

Also, changing while(totalRecvd != totalCnt) to
while(totalRecvd < totalCnt) would feel more robust to me.

Steve
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      01-21-2005
Will Hartung wrote:

> while(totalRcvd != totalCnt) {
> int amtToRead = blocksize;
> if (amtToRead > (totalCnt - totalRcvd)) {
> amtToRead = totalCnt - totalRcvd;
> }
> int amtRcvd = is.read(buffer, 0, amtToRead);
> totalRcvd = totalRcvd + amtRcvd;
> }


Consider what happens if you get a premature EOF. You aren't checking
for this condition, you're assuming that all the data will arrive. Very
optimistic ...

 
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 1 02-03-2009 06:20 AM
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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57