473,396 Members | 1,608 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.

problems when enabling nonblocking i/o in udp socket

Hi,

I used C to write simple "udp" (not tcp) socket programming. The client will send the string to the server and server will just echo that string back to client. Everything went fine until I set the flag for client as nonblocking i/o (O_NONBLOCKING). When I sent the first string from client, the error occured and printed "Resource temporalily unavailable". But when I sent the second string from client, the server instead sent back the first. When I sent the third string from client, the server echoed the second, and so forth... I'm just curious what nonblocking i/o in client actually did to my code and made that a problem.

Thank you very much.

NOSW
Jan 21 '07 #1
3 6832
Banfa
9,065 Expert Mod 8TB
Non blocking means that the code will not wait for an event to happen before it returns a result where as blocking waits for something to happen.

You can't just switch from blocking to non-blocking, there require different programming structures.

Take your example, the server just echos the string it receives. so for this sequence

send "String 1"
receive

send "String 2"
receive

send "String 3"
receive

In blocking IO the receives will block until they receive something (or a timeout occurs). So when you send "String 1" and then receive the receive waits for the server to reply.

In non-blocking IO the receive will not wait to receive something, if there is something there it will return it otherwise it will return an error. Since the server is unlikely to have time to rely in the time it takes to move to the next statement after the first call to send "String 1" the call to receive results in an error because nothing is waiting to be received. The second time there is something waiting to be received, the reply from the first send so you recieve that.

To work for a non blocking socket the sequence needs to change to

send "String 1"
wait for notification of received data
receive

send "String 2"
wait for notification of received data
receive

send "String 3"
wait for notification of received data
receive

The useful thing about a non blocking socket is that you can set up your program so that it can receive unsolicited messages more easily (by having a thread sitting around waiting for received data while the main thread carries on doing other things).
Jan 22 '07 #2
Hello,

Thanks for a reply. I do understand the concept but what does "wait for notification of received data" mean in the code?

NOSW
Jan 22 '07 #3
DeMan
1,806 1GB
Usually, there is little point sending data unless you know the other end has recieved it. So you wait for an Ack (from Acknowledgement) that the data has been recieved. In some protocols, rather than you timing out when you haven't recieved an Ack, the client either times out between packets, or recieves packets in a different order to what is expected (eg outside the window he is working in). In this case, you should recieve a NAK (or Not Acknowledged).


Little bit off the Topic:
In simple protocols if you are sending one packet at a time, there is little point sending the next packets until you know tyhe fiorst has been recieved.

In more complex protocols, you may have a window of several packets (any of which the client is quite happy to recieve, because he can reconstruct the order). In this case Acks and NAKs are sent with the packet number that he has (or hasn't) recieved, to enable you to resend it. As the earliest packet in the window is recieved, the window moves to the next earliest non-recieved packet.
Jan 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to...
1
by: David Dvali | last post by:
Hello. What is nonblocking sockets and how can I ues it? Can you show me some seamlpe please?
2
by: Helen | last post by:
Guys and gals, I am having a problem with an IFRAME that is embedded into an ASPX page (ASP.NET application). The IFRAME is originally empty (src=''). When a user selects a date from the...
0
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples...
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
1
by: Andrew | last post by:
Hello Everyone I am receiving an error in an application I am working on. The application when its done will be a Dungeons and Dragons Network game. I am having problems with the Networked...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
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:
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
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
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.