<p>Even select can't guarantee against blocked write since other events can fill them up.</p>
<div class="gmail_quote">On Mar 7, 2012 5:03 PM, "Shachar Shemesh" <<a href="mailto:shachar@shemesh.biz">shachar@shemesh.biz</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
On 03/07/2012 07:27 AM, Jonathan Ben Avraham wrote:
<blockquote type="cite">Hi linux-il colleagues,
<br>
In >= 2.6.31 are there any conditions under which a write to a
connected UDP socket can block? e.g. some change in routing or
iface config after connect and before write? Has anyone
encountered a concrete exampe of a write to a UDP socket that
blocks? See test code below. That is, I am trying to demonstrate
that I can get to a situation where UDP write in fact does block.
<br>
Best regards,
<br>
</blockquote>
<br>
A UDP (or, for that matter, TCP) socket will block under only one
situation that I'm aware of: if the send buffers are full. Use
select to test whether you can write to it, just like you would any
other file descriptor.<br>
<br>
One area where this actually works is cellular modems. If you are
transmitting over your bandwidth, sooner or later the socket will
block.<br>
<br>
As for the program below: are you sure it doesn't block?<br>
<br>
Shachar<br>
<blockquote type="cite">
<br>
- yba
<br>
<br>
<br>
#include <stdio.h>
<br>
#include <strings.h>
<br>
#include <sys/socket.h>
<br>
#include <sys/types.h>
<br>
#include <netinet/in.h>
<br>
#include <net/if.h>
<br>
#include <stdlib.h>
<br>
#include <errno.h>
<br>
<br>
<br>
int main()
<br>
{
<br>
int sd;
<br>
int rc;
<br>
struct ifreq interface;
<br>
struct sockaddr_in sin;
<br>
unsigned char buf[1024];
<br>
struct sockaddr_in serveraddr;
<br>
<br>
if (0 > (sd = socket(AF_INET, SOCK_DGRAM, 0))) {
<br>
printf("socket failed\n");
<br>
exit(1);
<br>
}
<br>
<br>
sin.sin_family = AF_INET;
<br>
sin.sin_port = htons(0);
<br>
bzero(&(sin.sin_zero), 8);
<br>
<br>
sin.sin_addr.s_addr = INADDR_ANY;
<br>
<br>
if (-1 == bind(sd, (struct sockaddr *)&sin,
sizeof(struct sockaddr_in))) {
<br>
printf("bind to sd failed [%m]");
<br>
return -1;
<br>
}
<br>
<br>
<br>
bzero(&serveraddr, sizeof(struct sockaddr_in));
<br>
serveraddr.sin_family = AF_INET;
<br>
serveraddr.sin_port = htons(80);
<br>
inet_pton(AF_INET, "1.0.1.2", &serveraddr.sin_addr);
<br>
<br>
if (0 > (rc = connect(sd, (const struct
sockaddr*)&serveraddr, sizeof(serveraddr)))) {
<br>
printf("connect failed [%m]");
<br>
exit(1);
<br>
}
<br>
<br>
while ( 1 ) {
<br>
if (0 > (rc = write(sd, buf, 1024))) {
<br>
printf("write error [%m]\n");
<br>
}
<br>
else
<br>
printf("wrote to socket [%d]\n", rc);
<br>
}
<br>
}
<br>
<br>
<br>
</blockquote>
<br>
<br>
<pre cols="72">--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
<a href="http://www.lingnu.com" target="_blank">http://www.lingnu.com</a>
</pre>
</div>
<br>_______________________________________________<br>
Linux-il mailing list<br>
<a href="mailto:Linux-il@cs.huji.ac.il">Linux-il@cs.huji.ac.il</a><br>
<a href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il" target="_blank">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a><br>
<br></blockquote></div>