Ah..great! it was this, thanks Scott.
I'm not sure why it wasn't working before, but I changed the listening
address to htonl(IADDR_ANY) and it worked fine...this doesn't help me do the
same to my C# service but never mind.
Another problem perhaps you can advise me on... now I've got it working,
data is being sent back and forward reliably and the number of bytes sent is
always right.
However I've got a problem that's a bit more of a logic issue - I'll
describe what happens: the client connects to the server, and sends data
first.
The server recv()s this data successfully.
However I would like it so that the server can receive all the data in
multiple chunks (i.e. in a loop with a call to recv on each iteration)...
but the problem is knowing how to stop this loop. The bit I can't get my
head round is... the call to recv never returns 0 - it returns positive
integer while there is still data (which fills the buffer correctly) - but
then blocks indefinitely when all the data has been received... *unless* -
it sends some data back on each iteration, as in an echo server. But what if
I want to receive all the data before formulating the response?
I could think of one solution which is to designate a zero-terminator for
the data. But this is slightly messy as it requires extra code, possibly
with a loop, to detect this terminator byte. I don't want to send dummy data
over the network as this is causing unnecessary traffic.
The code I've got for the server is:
// std::string data_to_consider("");
char buf[RECVBUFLEN + 1];
do
{
int bytesrecvd = recv(Socket, buf, RECVBUFLEN, 0);
Sleep(0);
if(bytesrecvd == SOCKET_ERROR) throw(5);
if(bytesrecvd > 0)
{
buf[min(bytesrecvd, RECVBUFLEN)] = '\0';
printf("%s", buf); //in the non-demo application this would be probably
appended to a std::string or something instead...
printf("Sent %d bytes back\n", send(Socket, buf, bytesrecvd, 0));
//don't want to do this every time!
//instead I want to do this..
//data_to_consider += buf;
}
else break; //this break never occurs
} while(TRUE);
//here is where I want to send some data back, once and only, e.g.
send(Socket, ProcessAllData(data_to_consider), data_to_consider.length,
0);
printf("\n");
_tprintf(_T("\nClient has disconnected\n"));
does that make sense what I'm trying to get at?
Thanks!
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news

YKdnSN4OKf2VILfRVn-(E-Mail Removed)...
> Bonj wrote:
>
>> Hello
>> I wonder if anybody can help me..
>> I have got two linux machines, both running fedora core 2, and a windows
>> machine (running XP) - I have built a TCP client and server for linux
>> (and one for windows), these all work fine when connecting to localhost -
>> however they can't seem to connect to each other - the connet() call
>> always errors with "connection refused" - *both* when trying to do it
>> from windows to linux (linux running the server), *and* linux to linux
>> (linux running the server and the client).
>>
>> Everything else network related works - the windows machine can see the
>> linux files via the samba server and vice versa, and the windows web
>> browser can see the linux box's web server's apache home page, and the
>> linux machines can both see each other's files, and browse to each
>> other's web servers.
>> It's just my own TCP programs that always seem to refuse to connect. It's
>> always "Connection refused". I'm stumped as to what I can do, because the
>> programs work fine when connecting to localhost / 127.0.0.1 - just not to
>> each other.
>>
>> Any ideas?
>
> This sounds like what would happen if the port number is not specified in
> network byte order at one end.
>
> --
> Scott McPhillips [VC++ MVP]
>