Unix Programming - problem reading from socket data sent by a Java client

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2006 > problem reading from socket data sent by a Java client





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author problem reading from socket data sent by a Java client
msosno01@gmail.com

2006-07-21, 7:24 pm

I am trying to send an integer from Java Client to C++ server. I
figured out how to do the conversion. However, I have a problem reading
from the socket.
This is my code:

int DataInputStream::readInt()//reads one integer from the client
{
int32_t var1;
soc->recv((void*)&var1, 4);
var1 = ntohl(var1);
return var1;
}

I am assuming that I need a loop, but I cannot figure out which one. I
tried all cases that I could imagine, but none of them worked.
I MUST use recv function. Here is how it is defined:

int recv(void *buffer, int bufferLen)
throw(SocketException) {
int rtn;
if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0)
{
throw SocketException("Received failed (recv())", true);
}

return rtn;
}

Any help is appreciated.

James Carlson

2006-07-24, 7:29 am

msosno01@gmail.com writes:
> int recv(void *buffer, int bufferLen)
> throw(SocketException) {
> int rtn;
> if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0)
> {
> throw SocketException("Received failed (recv())", true);
> }
>
> return rtn;
> }


You didn't say what sort of problems you're seeing (it's always good
to include some hints about the problem), but you'd probably be in
better shape if you did something like this:

char *bp = buffer;

while (bufferLen > 0) {
rtn = ::recv(sockDesc, bp, bufferLen, 0);
if (rtn < 0)
throw SocketException("Received failed (recv())", true);
if (rtn == 0)
throw SocketException("end of file (recv())", true);
bufferLen -= rtn;
}

.... but without knowing the structure and design of the rest of the
program, it's hard to say.

--
James Carlson, KISS Network <james.d.carlson@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
Maxim Yegorushkin

2006-07-26, 7:28 pm


James Carlson wrote:

> You didn't say what sort of problems you're seeing (it's always good
> to include some hints about the problem), but you'd probably be in
> better shape if you did something like this:
>
> char *bp = buffer;
>
> while (bufferLen > 0) {
> rtn = ::recv(sockDesc, bp, bufferLen, 0);
> if (rtn < 0)
> throw SocketException("Received failed (recv())", true);
> if (rtn == 0)
> throw SocketException("end of file (recv())", true);
> bufferLen -= rtn;
> }


The loop misses bp += rtn; to work properly on the second iteration.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com