By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

sockets: c# server with c++ client

P: n/a
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;
}
-------------------------------------------------
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OB**************@TK2MSFTNGP09.phx.gbl...
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.
Nov 17 '05 #2

P: n/a
Andy Walldorff wrote:
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OB**************@TK2MSFTNGP09.phx.gbl...
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 ...

Nov 17 '05 #3

P: n/a
Herbert VON GRÜNENWALD wrote:
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

Nov 17 '05 #4

P: n/a
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).

--
Adam Clauss
ca*****@tamu.edu

"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OB**************@TK2MSFTNGP09.phx.gbl...
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;
}
-------------------------------------------------

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.