473,698 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another socket programming question

Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand.

Programming environment. VS.NET 2003, C#, Windows XP.

About the architecture:
I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has a custom developed collection class that is used to track all sockets that connect to the server(server functionality) as well as any sockets that the server must create to communicate with peers(client functionality). The collection class contains a custom developed socketTrackerIt em class that contains a reference to the socket as well as two custom developed classes. One for listening that can be started and stopped and one for sending data over the socket. All socket handling is Async with ManualResetEven ts handling synchronization where appropriate.

Usage scenario:
The usage in which I am have a problem is this. A peer lets say Peer A connects to another peer, Peer B, on a specific IPAddress and Port to send a one time only information packet. So Peer B's server class has been started and is listening for incoming connection attempts. Peer A creates a new socket to Peer B because one does not already exist and connects. Peer B is setting waiting for a connection. Peer B get the connection request from Peer A which is handled by BeginConnectCal lback. This callback checks to see if Peer A should be allowed to connect, i.e. not on block list, and if it is allowed creates a new SocketTrackerIt em, adds a new SocketListener and SocketSender to the SocketTrackerIt em, starts the SocketListener to listen for any data sent on the connected socket, then adds the SocketTrackerIt em to the collection based upon connected socket's RemoteEndPoint hash. The ConnectDone manual reset event is fired and the server goes back to listening for connection attempts. Peer A then sends a chunk of data to Peer B which is in the listening mode. Peer B is waiting in a loop that runs as long as the SocketListener IsRunning property is true with a call to BeginReceive and a ReceiveDone.Wai tOne() call after the call. The ReceiveCallback gets called, reads the bytes, checks how many bytes that the EndReceive function returned, then set ReceiveDone.Set (). Peer A disconnects with calls to Shutdown(Socket Shutdown.Both) then calls close on the socket. I need to mention that when Peer B accepted the connection it done a SetSocketOption for KeepAlive and DontLinger options so the socket should be notified very quickly after a socket disconnect.

Observed Behavior:
Once Peer A disconnects Peer B socket that it is listening on ConnectedValue is never set to false. Also a SocketError is never thrown in the BeginReceive or BeginReceiveCal lback functions to indicate that Peer A has disconnected. Also on PeerB, which is in a BeginReceive -> BeginReceiveCal lback loop listening for data, the BeginReceiveCal lback functions fires all the time with 0 bytes read which the callback then does a ReceiveDone.Set () (manualResetEve nt) that the BeginRecieve function is waiting on before it issues another call to BeginReceive on the socket. From my understanding the socket doesn't call the ReceiveCallback until there is data in the socket buffer that needs to be read. Is this the correct behavior. If so, I can write code to handle it but it wasn't what I was expecting. So I have two behaviors that I didn't expect. 1.) The BeginReceiveCal lback is called even if there is no data to read and 2.) In the BeginReceive and BeginReceiveCal lback function of my listener no SocketError is thrown to indicate that a connected socket disconnected. Are these behaviors correct?

Thanks for listening to my long winded question but I wanted you to have all the facts to understand the situation. Thank you for any help that you may be able to supply.

John
Nov 16 '05 #1
1 3389
When you get zero bytes from the EndReceive method it means the socket was
closed.

From the MSDN docs on Socket.EndRecei ve

"If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the EndReceive method will
complete immediately and return zero bytes."

That should be of some help
Tom

"John Sheppard" <Jo**********@d iscussions.micr osoft.com> wrote in message
news:54******** *************** ***********@mic rosoft.com...
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand.
Programming environment. VS.NET 2003, C#, Windows XP.

About the architecture:
I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or
stopped by calls to the appropriate functions. The server class has a
custom developed collection class that is used to track all sockets that
connect to the server(server functionality) as well as any sockets that the
server must create to communicate with peers(client functionality). The
collection class contains a custom developed socketTrackerIt em class that
contains a reference to the socket as well as two custom developed classes.
One for listening that can be started and stopped and one for sending data
over the socket. All socket handling is Async with ManualResetEven ts
handling synchronization where appropriate.
Usage scenario:
The usage in which I am have a problem is this. A peer lets say Peer A connects to another peer, Peer B, on a specific IPAddress and Port to send a
one time only information packet. So Peer B's server class has been started
and is listening for incoming connection attempts. Peer A creates a new
socket to Peer B because one does not already exist and connects. Peer B is
setting waiting for a connection. Peer B get the connection request from
Peer A which is handled by BeginConnectCal lback. This callback checks to
see if Peer A should be allowed to connect, i.e. not on block list, and if
it is allowed creates a new SocketTrackerIt em, adds a new SocketListener and
SocketSender to the SocketTrackerIt em, starts the SocketListener to listen
for any data sent on the connected socket, then adds the SocketTrackerIt em
to the collection based upon connected socket's RemoteEndPoint hash. The
ConnectDone manual reset event is fired and the server goes back to
listening for connection attempts. Peer A then sends a chunk of data to
Peer B which is in the listening mode. Peer B is waiting in a loop that
runs as long as the SocketListener IsRunning property is true with a call to
BeginReceive and a ReceiveDone.Wai tOne() call after the call. The
ReceiveCallback gets called, reads the bytes, checks how many bytes that the
EndReceive function returned, then set ReceiveDone.Set (). Peer A
disconnects with calls to Shutdown(Socket Shutdown.Both) then calls close on
the socket. I need to mention that when Peer B accepted the connection it
done a SetSocketOption for KeepAlive and DontLinger options so the socket
should be notified very quickly after a socket disconnect.
Observed Behavior:
Once Peer A disconnects Peer B socket that it is listening on ConnectedValue is never set to false. Also a SocketError is never thrown in
the BeginReceive or BeginReceiveCal lback functions to indicate that Peer A
has disconnected. Also on PeerB, which is in a BeginReceive ->
BeginReceiveCal lback loop listening for data, the BeginReceiveCal lback
functions fires all the time with 0 bytes read which the callback then does
a ReceiveDone.Set () (manualResetEve nt) that the BeginRecieve function is
waiting on before it issues another call to BeginReceive on the socket.
From my understanding the socket doesn't call the ReceiveCallback until
there is data in the socket buffer that needs to be read. Is this the
correct behavior. If so, I can write code to handle it but it wasn't what I
was expecting. So I have two behaviors that I didn't expect. 1.) The
BeginReceiveCal lback is called even if there is no data to read and 2.) In
the BeginReceive and BeginReceiveCal lback function of my listener no
SocketError is thrown to indicate that a connected socket disconnected. Are
these behaviors correct?
Thanks for listening to my long winded question but I wanted you to have all the facts to understand the situation. Thank you for any help that you
may be able to supply.
John

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13734
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32 ------ Compiling... cl : Command line warning D4029 : optimization is not available in the
2
9233
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : <code> mySocket.BeginReceive(param1,param2,param3.....); mySocket.BeginReceive(param1,param2,param3......); mySocket.BeginReceive(param1,param2,param3......);
5
2089
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my application works great, the problem is if I want to "broadcast" a message to all my clients. Right now I just iterate through my sockets list and send the message to each client. This works fine until I need to send something large like a file. Is there a...
2
15324
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
7
1717
by: clintonG | last post by:
I'm puzzled and don't think this is possible but if an application that is running on websiteA generates a file can FTP be used from websiteA to transfer that file to websiteB which would be located on another server? -- <%= Clinton Gallagher NET csgallagher AT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/
4
4866
by: seets375 | last post by:
Hi, I have two ethernet interfaces on my system, with IPs assigned to the interfaces from different subnets (e.g. eth1 - 10.10.10.10 and eth2 - 20.20.20.20 ). I'm connecting these interfaces to a home gateway or router. The 'eth1' interface connects to LAN interface of the router with IP address 10.10.10.1 and the 'eth2' interface connects to WAN interface of the router with IP address 20.20.20.1. I'm trying a simple socket program...
3
2765
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: #include "TCPEchoServer.h" /* TCP echo server includes */ #include <pthread.h /* for POSIX threads */
1
1829
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket in non-blocking mode by using the proper socket option ( i.e. SO_RCVTIMEO). After which with the use of recv() I am trying to get into the receive mode. Here as the receive time out is being used the socket should come out of the block mode after...
4
11237
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to set the buffer? In specific, my question is what causes the 'completed' event in the OnSocketConnectCompleted method to fire? Does it fire at end of a packet or does happen at some end of data marker? public SocketClient(Page pg, Int32...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9026
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
8893
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
8861
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
7723
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
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

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.