473,388 Members | 986 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,388 software developers and data experts.

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 socketTrackerItem 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 ManualResetEvents 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 BeginConnectCallback. 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 SocketTrackerItem, adds a new SocketListener and SocketSender to the SocketTrackerItem, starts the SocketListener to listen for any data sent on the connected socket, then adds the SocketTrackerItem 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.WaitOne() 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(SocketShutdown.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 BeginReceiveCallback functions to indicate that Peer A has disconnected. Also on PeerB, which is in a BeginReceive -> BeginReceiveCallback loop listening for data, the BeginReceiveCallback functions fires all the time with 0 bytes read which the callback then does a ReceiveDone.Set() (manualResetEvent) 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 BeginReceiveCallback is called even if there is no data to read and 2.) In the BeginReceive and BeginReceiveCallback 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 3360
When you get zero bytes from the EndReceive method it means the socket was
closed.

From the MSDN docs on Socket.EndReceive

"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**********@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.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 socketTrackerItem 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 ManualResetEvents
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 BeginConnectCallback. 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 SocketTrackerItem, adds a new SocketListener and
SocketSender to the SocketTrackerItem, starts the SocketListener to listen
for any data sent on the connected socket, then adds the SocketTrackerItem
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.WaitOne() 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(SocketShutdown.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 BeginReceiveCallback functions to indicate that Peer A
has disconnected. Also on PeerB, which is in a BeginReceive ->
BeginReceiveCallback loop listening for data, the BeginReceiveCallback
functions fires all the time with 0 bytes read which the callback then does
a ReceiveDone.Set() (manualResetEvent) 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
BeginReceiveCallback is called even if there is no data to read and 2.) In
the BeginReceive and BeginReceiveCallback 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
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...
2
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 : ...
5
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...
2
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...
7
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...
4
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...
3
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: ...
1
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.