Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

making send/receive non blocking

315 views
Skip to first unread message

M.S.Reddy

unread,
Feb 25, 2003, 12:29:33 PM2/25/03
to
Hi all,
I want to make a send/receive non blocking without setting the socket as
non blocking. There is way to do this under unix (using MSG_DONTWAIT flag).
I want to know if it is possible under windows. I would prefer not using
winsocks but just use the standard socks, if possible. Thank you
Reddy


LiquidJ

unread,
Feb 25, 2003, 1:28:20 PM2/25/03
to
Check out ioctlsocket(), with FIONBIO command.


{L}

"M.S.Reddy" <msr...@indiatimes.com> wrote in message
news:1-ednQkxMoi...@comcast.com...

M.S.Reddy

unread,
Feb 25, 2003, 5:01:14 PM2/25/03
to

"LiquidJ" <replyt...@boo.yah> wrote in message
news:O1nh1uP...@TK2MSFTNGP09.phx.gbl...

> Check out ioctlsocket(), with FIONBIO command.
>
I beleive ioctlsocket() makes the socket non blocking. But that is not what
i want. I want the socket to be blocking, but make a particular send/receive
to be non blocking (can be done in linux). Is it possible. Thanks
Reddy

LiquidJ

unread,
Feb 25, 2003, 5:11:36 PM2/25/03
to
Sorry, I guess I didn't read past the first line of your post :)

Just so I understand, you want to have the socket blocking, but be
temporarily non-blocking for one or two function calls? What is it you're
trying to accomplish? Perhaps there is a better way.


{L}


"M.S.Reddy" <msr...@indiatimes.com> wrote in message

news:B_OcnR1R6_4...@comcast.com...

Mark Wodrich [MSFT]

unread,
Feb 25, 2003, 7:14:10 PM2/25/03
to
The standard way to do this is to use the "select" API and check to see if
any data is ready to be read from the socket.
You then read the data using recv() as usual. This is not the most efficient
way of doing things in Windows, though. If you care about the performance of
your application you should look into using Winsock and either overlapped
I/O or WSAEventSelect/WSAAsyncSelect.

Mark.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"

Please do not send email directly to this alias. This alias is for newsgroup
purposes only.


"M.S.Reddy" <msr...@indiatimes.com> wrote in message

news:B_OcnR1R6_4...@comcast.com...

M.S.Reddy

unread,
Feb 25, 2003, 7:18:10 PM2/25/03
to

"LiquidJ" <replyt...@boo.yah> wrote in message
news:ODktmrR3...@TK2MSFTNGP10.phx.gbl...

> Sorry, I guess I didn't read past the first line of your post :)
>
> Just so I understand, you want to have the socket blocking, but be
> temporarily non-blocking for one or two function calls? What is it you're
> trying to accomplish? Perhaps there is a better way.

You are probably right. Perhaps there is a better way. I have a many
servers/clients each talking to one other. Clients keep sending lot of data
(mostly images). all are single threaded. So, the obvious choice would be
making the sockets non blocking. But what can i do, if the receiver's socket
is not ready. I need to save the data and try again later. But there are
some packets that I have to send right away. So I want the socket to be
blocking so that it doesnt return until it sends it.I can do it by putting
send in a loop. The otheway is to make the sockets blocking and send the
unimportant data in non blocking manner. I wanted to do it in the second way
also wanted to learn how this can be acheived under windows.
I am not sure if my method is the best. Any suggestions are most welcome

Hector Santos

unread,
Feb 25, 2003, 7:22:33 PM2/25/03
to

"M.S.Reddy" <msr...@indiatimes.com> wrote in message
news:B_OcnR1R6_4...@comcast.com...

>
> "LiquidJ" <replyt...@boo.yah> wrote in message
> news:O1nh1uP...@TK2MSFTNGP09.phx.gbl...
> > Check out ioctlsocket(), with FIONBIO command.
> >
> I beleive ioctlsocket() makes the socket non blocking. But that is not
what
> i want. I want the socket to be blocking, but make a particular
send/receive
> to be non blocking (can be done in linux). Is it possible. Thanks
> Reddy

There are several ways:

You can use the FD_XXXX macros with the select() function to test when data
is ready to be received.

You can also put your receive socket logic in a thread.

See the example at ftp://ftp.santronics.com/examples/socket/svrcli.zip

--
Hector Santos
Wildcat! Interactive Net Server
http://www.santronics.com

Eugene Gershnik

unread,
Feb 26, 2003, 2:09:41 AM2/26/03
to
You can use ReadFile/WriteFile with valid OVERLAPPED parameter.

Eugene

"M.S.Reddy" <msr...@indiatimes.com> wrote in message

news:1-ednQkxMoi...@comcast.com...

LiquidJ

unread,
Feb 26, 2003, 8:34:59 AM2/26/03
to
What do you mean by "the receiver's socket is not ready"? The state of the
receiver is not important to the sending abilities of the client (unless it
is closed) - even if the receiver is not actively waiting for data (blocking
call to recv(), WSAEventSelect(), etc.), the client can still send the data
(unless your system is structured this way). The IP packets are just queued
up on the receiver's computer until the socket owner is ready to read the
data (unless of course, the buffer overflows).

As for sending important and unimportant data using different methodologies,
I would recommend using two client-side sockets. If you still want to
follow your model, you can set one to blocking, and one to non-blocking.

As other people have stated, probably the best way to approach this is to
use either IOCP or to use blocking mode sockets in combination with
WSAEventSelect(), WSAAsyncSelect() or select().

Is communication via TCP or UDP?


{L}

"M.S.Reddy" <msr...@indiatimes.com> wrote in message

news:JB2cnTm3fbl...@comcast.com...

Phil Frisbie, Jr.

unread,
Feb 26, 2003, 11:21:50 AM2/26/03
to
M.S.Reddy wrote:
>
> You are probably right. Perhaps there is a better way. I have a many
> servers/clients each talking to one other. Clients keep sending lot of data
> (mostly images). all are single threaded. So, the obvious choice would be
> making the sockets non blocking. But what can i do, if the receiver's socket
> is not ready. I need to save the data and try again later. But there are
> some packets that I have to send right away. So I want the socket to be
> blocking so that it doesnt return until it sends it.

You have some very bad misconceptions about the way TCP/IP works. I suggest you
read a good book or some web tutorials.

A blocking socket does not block until the data is sent, it blocks until the
data can be copied to an internal buffer while waiting to be sent. There is no
way for you to know that the receiver has received the data you sent without
adding that to your own protocol.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

Alun Jones

unread,
Feb 26, 2003, 11:36:51 AM2/26/03
to
In article <JB2cnTm3fbl...@comcast.com>, "M.S.Reddy"
<msr...@indiatimes.com> wrote:
>You are probably right. Perhaps there is a better way. I have a many
>servers/clients each talking to one other. Clients keep sending lot of data
>(mostly images). all are single threaded. So, the obvious choice would be
>making the sockets non blocking. But what can i do, if the receiver's socket
>is not ready. I need to save the data and try again later. But there are
>some packets that I have to send right away. So I want the socket to be
>blocking so that it doesnt return until it sends it.I can do it by putting
>send in a loop.

If this is exactly what you want to do, then make the socket non-blocking, and
write yourself a small "blocked_send" function. Don't just blindly loop on
send(), use select() so that you don't use up CPU time that would be better
spent elsewhere, and so that you can have a timeout (because the best-behaved
software needs to cope with a non-responsive receiver).

Note, finally, that your focussing on "the receiver's socket is not ready" is
a _bad_ thing. You have no way of knowing this. All you can know, and that's
what blocking is about, is whether you have buffer space in your sender.
After your send() is fully accepted, and until you get an acknowledgement from
the receiver application, you don't know (and don't need to know) whether the
data is in the send buffer, going across the network (in which case it's still
in the send buffer), at the receiver's network stack (in which case it may
still be in your send buffer), in the application but unprocessed (in which
case it is probably not in your send buffer, but might be, if the ACK hasn't
yet been received), or processed by the application (but you haven't received
the application's ACK).

When you send data, you have the following states:

1. I have data to send.
2. I have asked the stack to send it.
3. The application at the other end has told me that it received it.

All other states are illusory.

Alun.
~~~~

[Please don't email posters, if a Usenet response is appropriate.]
--
Texas Imperial Software | Try WFTPD, the Windows FTP Server. Find us at
1602 Harvest Moon Place | http://www.wftpd.com or email al...@texis.com
Cedar Park TX 78613-1419 | VISA/MC accepted. NT-based sites, be sure to
Fax/Voice +1(512)258-9858 | read details of WFTPD Pro for XP/2000/NT.

0 new messages