473,418 Members | 2,338 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,418 software developers and data experts.

Socket.Available and UDP datagrams

I'm implementing a UDP server socket from the system.net.sockets.socket
class. (no, I don't want to use the udpclient class)
I'm polling the socket in a thread, testing the socket.available property on
each pass.
With datagrams, does socket.available only advertise when there are complete
packets in its buffers?
What i mean to say is that if socket.Available > 0 then there will be at
least 1 whole datagram ready to be read? So, if less than a full datagram
arrived, socket.available would return zero?
I've already noticed that if socket.Available is greater than the size of a
single datagram Socket.Read method returns a single datagram leaving the
remainder of the bytes in a buffer ready to be collected with subsequent
calls to socket.read (even if you ask for the whole buffer)
Hoping that someone else will have studied the socket classes' behaviour.
thanks

Example server UDP dotnet socket code
myServerSocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwo rk,
System.Net.Sockets.SocketType.Dgram,
System.Net.Sockets.ProtocolType.Udp);
myServerEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any,
12345);
myServerSocket.Bind(myServerEndpoint);
while(Condition)
{
Available = myServerSocket.Available;
if (Available > 0)
{
foobar();
}
Thread.Sleep(sleeptime);
}
Nov 17 '05 #1
2 6330
Hi Claire,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know if socket.available
only advertise when there are complete packets in its buffers. If there is
any misunderstanding, please feel free to let me know.

As far as I know, it has to return the size of the first message when a
complete message is available in buffer. According to MSDN, if you are
using a message-oriented socket type such as Dgram, the available data is
the first message in that buffer. If no data is queued in the network
buffer, Available returns 0.

So there are complete messages in buffer, it returns the size of the first
one. Or it returns 0.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2
Unless you have some need not given here, polling like this is very
inefficient for a service. Instead, block on Receive(). The thread will
wait/block until a whole datagram arrives.

--
William Stacey [MVP]

"Claire" <cz*********@community.nospam> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
I'm implementing a UDP server socket from the system.net.sockets.socket
class. (no, I don't want to use the udpclient class)
I'm polling the socket in a thread, testing the socket.available property
on each pass.
With datagrams, does socket.available only advertise when there are
complete packets in its buffers?
What i mean to say is that if socket.Available > 0 then there will be at
least 1 whole datagram ready to be read? So, if less than a full datagram
arrived, socket.available would return zero?
I've already noticed that if socket.Available is greater than the size of
a single datagram Socket.Read method returns a single datagram leaving the
remainder of the bytes in a buffer ready to be collected with subsequent
calls to socket.read (even if you ask for the whole buffer)
Hoping that someone else will have studied the socket classes' behaviour.
thanks

Example server UDP dotnet socket code
myServerSocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwo rk,
System.Net.Sockets.SocketType.Dgram,
System.Net.Sockets.ProtocolType.Udp);
myServerEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any,
12345);
myServerSocket.Bind(myServerEndpoint);
while(Condition)
{
Available = myServerSocket.Available;
if (Available > 0)
{
foobar();
}
Thread.Sleep(sleeptime);
}

Nov 17 '05 #3

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

Similar topics

2
by: David Konerding | last post by:
Hello, I have written an app which opens a TCP connection to a server and uses a protocol to communicate with it. Specifically, I've written a python IMD client for the molecular dynamics...
4
by: Ajay | last post by:
hi! whats the maximum number of datagrams that can queue up on a UDP server socket? or is that system dependent? i have some code that iteratively handles datagrams, and while testing it, i...
4
by: Stephan Steiner | last post by:
Hi I have a networking application that periodically needs to go into sleep mode (from an application point of view, I'm simply suspending the receiver thread until it's time to start listening...
3
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been...
1
by: Rolln_Thndr | last post by:
I'm vey new to network programing and have a few rather fundemental questions. I'm creating a very basic UDP proxy server and having a few issues regarding the sockets. Is it possible to change...
10
by: feel52 | last post by:
Below you'll find the code i'm working on. It's in a button click routine and hangs after 3 or 4 sometimes 5 loops done, probably in sock.receive(....). Some code was found here( on google i mean)...
6
by: J Rice | last post by:
Hi, I feel like I should apologize in advance because I must be missing something fairly basic and fundamental here. I don't have a book on Python network programming (yet) and I haven't been able...
14
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
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: 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
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...
0
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...
0
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,...

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.