473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sockets: c# server with c++ client

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
4 7343

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr.Kadzija | last post by:
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the problem is that: both client and server will send...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
14
by: jack | last post by:
At this link I have two c# projects, one is a client, the other is a server. Just point the ip address of the client at the server http://www.slip-angle.com/hosted/bug/ The server does...
3
by: Adam Honek | last post by:
Hello, Can someone pinpoint me to a reliable link relating to sockets and how they are implemented to allow client/server communication? I'm finding quite a few but very few actually explain...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.