<html style="direction: ltr;">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style type="text/css">body p { margin-bottom: 0.2cm; margin-top: 0pt; } </style>
</head>
<body bidimailui-detected-decoding-type="UTF-8" text="#000000"
bgcolor="#FFFFFF">
On 03/07/2012 07:27 AM, Jonathan Ben Avraham wrote:
<blockquote
cite="mid:alpine.DEB.2.00.1203070720340.10915@gamla.tkos.co.il"
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
cite="mid:alpine.DEB.2.00.1203070720340.10915@gamla.tkos.co.il"
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 class="moz-signature" cols="72">--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
<a class="moz-txt-link-freetext" href="http://www.lingnu.com">http://www.lingnu.com</a>
</pre>
</body>
</html>