sockets: c# server with c++ client

  • Thread starter =?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=
  • Start date
?

=?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=

hi everyone,

i've made a client/server class in C++ and some other to do the same
thing in c#.

clients and server works fine, in the two langages, but there is one
problem when a c++ client disconnect from the c# server:
- the connection is OK
- when i send a message from the client to the server, no problems (it
is received by the server)
- but when i shutdown and close the client (c++) (code below, there is
no error at run time), the server receive some data:
-------------------------------------------------
byte[] nb = new byte[4];

received = socket.Receive(nb, 0, 4, System.Net.Sockets.SocketFlags.None);

or

received = socket.Receive(nb);
-------------------------------------------------
the result is the same:
received = 0

and this function receive 0 bytes, 10000-100000 times !!

so i doesn't understand very well what's happenning !

have you any ideas ?

thanks

there the code when closing the client
-------------------------------------------------
int CManuWinSock::Close()
{
int iret = 0;

if (m_socket != INVALID_SOCKET)
{
iret = shutdown(m_socket, SD_BOTH);
if (iret)
{
erreur(WSAGetLastError(), "CManuWinSock::Close shutdown");
}
iret = closesocket(m_socket);
if (iret)
{
erreur(WSAGetLastError(), "CManuWinSock::Close closesocket");
return iret;
}
else
m_socket = INVALID_SOCKET;
}

return iret;
}
-------------------------------------------------
 
A

Andy Walldorff

Herbert VON GRÜNENWALD said:
hi everyone,

i've made a client/server class in C++ and some other to do the same thing
in c#.

clients and server works fine, in the two langages, but there is one
problem when a c++ client disconnect from the c# server:
- the connection is OK
- when i send a message from the client to the server, no problems (it is
received by the server)
- but when i shutdown and close the client (c++) (code below, there is no
error at run time), the server receive some data:
[snip]

While I have not used the .NET sockets (yet), you typically use the select
function to determine if a socket is ready to read, detect errors, .... I
would suggest using the poll or select method and checking for an error when
receive returns 0 or prior to calling receive to determine if it is ready to
be read.
 
?

=?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=

Andy said:
hi everyone,

i've made a client/server class in C++ and some other to do the same thing
in c#.

clients and server works fine, in the two langages, but there is one
problem when a c++ client disconnect from the c# server:
- the connection is OK
- when i send a message from the client to the server, no problems (it is
received by the server)
- but when i shutdown and close the client (c++) (code below, there is no
error at run time), the server receive some data:

[snip]

While I have not used the .NET sockets (yet), you typically use the select
function to determine if a socket is ready to read, detect errors, .... I
would suggest using the poll or select method and checking for an error when
receive returns 0 or prior to calling receive to determine if it is ready to
be read.

Yes, my server and clients pool each other.

But during two pool, when the socket disconnect, the Socket.Receive
function return immediatly, so my process use 100% of the CPU...

I've found the trick, when Receive return 0, the server send a message
pool to the client.
The SocketException raise after 3 of this message !!
I don't understand why, but that works ...
 
M

Markus Stoeger

Herbert said:
the result is the same:
received = 0

and this function receive 0 bytes, 10000-100000 times !!

so i doesn't understand very well what's happenning !

have you any ideas ?

The Receive function returns 0 bytes when the connection has been closed by
the remote host. It's always been like that :) Just call .Close() on the
socket too and exit.

Max
 
A

Adam Clauss

That's perfectly normal. Getting a Receive triggered with a length of 0 is
..NET's way of telling you the socket was closed. (I wish it wasn't - it is
not intuitive, but it is, and once you know it, it works).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top