473,839 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sock ets.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::C lose()
{
int iret = 0;

if (m_socket != INVALID_SOCKET)
{
iret = shutdown(m_sock et, SD_BOTH);
if (iret)
{
erreur(WSAGetLa stError(), "CManuWinSock:: Close shutdown");
}
iret = closesocket(m_s ocket);
if (iret)
{
erreur(WSAGetLa stError(), "CManuWinSock:: Close closesocket");
return iret;
}
else
m_socket = INVALID_SOCKET;
}

return iret;
}
-------------------------------------------------
Nov 17 '05 #1
4 7370

"Herbert VON GRÜNENWALD" <he************ *******@microso ft.com> wrote in
message news:OB******** ******@TK2MSFTN GP09.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************ *******@microso ft.com> wrote in
message news:OB******** ******@TK2MSFTN GP09.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.ed u

"Herbert VON GRÜNENWALD" <he************ *******@microso ft.com> wrote in
message news:OB******** ******@TK2MSFTN GP09.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.Sock ets.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::C lose()
{
int iret = 0;

if (m_socket != INVALID_SOCKET)
{
iret = shutdown(m_sock et, SD_BOTH);
if (iret)
{
erreur(WSAGetLa stError(), "CManuWinSock:: Close shutdown");
}
iret = closesocket(m_s ocket);
if (iret)
{
erreur(WSAGetLa stError(), "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
15632
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 each other multiple lines (using PrintWriter.println()) at a time, and i don't know how many lines each of them will send. this wouldn't be a problem if a sender could end his turn with a pre-defined character or string (e.g. "\n", "bye" or...
1
3801
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 pending connections every 500ms. I also have a vb6 application that uses the WinSock control at the other end of the communication tunel. I have to work with vb6 here because it uses less memory than .NET.
4
4440
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 strings' instead of 'one liners' I check the streams for terminators. I'm having problems with the connection, I've been looking and debugging for 2 weeks now (debugging with an emulator is terribly slow..) but I'm not getting it...
14
1877
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 little more than the examples in the c# documentation for how to set up an asynchronous server. It just accepts connections, reads data into a buffer continously, doing nothing with it.
3
284
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 it step by step and a lot of the source code examples I see is not commented or commented poorly. P.S Is Sockets and TCPClient the same thing?
2
7134
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 short while now using a server program a former coworker started. The program listens on a port for incomming connections. When a valid connection is made (we send this init string into the socket from the clients) the server closes the socket so...
1
15649
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 CONNECTION //IMPORTANT: the logic is NOT complete, the program works only once and disconnects or freeze //One needs to modify or rewrite the program so it is fully functional for LIST and RETR
14
11935
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 doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call NetworkStream ns = client.GetStream(); to get a stream stream.Read(buffer, 0, buffer.Length);
0
9855
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10647
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10293
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.