read hang on datagram socket

Daniel Eischen deischen at freebsd.org
Thu Jan 26 15:56:44 PST 2006


On Thu, 26 Jan 2006, Kurt Miller wrote:

> I'm working on 1.5 jdk certification on 5.4 and 6.0. One of the jck
> tests hangs on 5.4 but works ok on 6.0. I've reduced the problem
> down to the following C program that hangs on 5.4 but works fine
> (finishes) on 6.0 and 4.11.
>
> I could use some assistance with finding a work-around to the
> problem or an explanation as to why the program hangs on 5.4.

It exit'd in the last connect() in Solaris 9.  I modified it
to work -- see below.

>
> Thank you,
> -Kurt
>
> #include <stdio.h>
> #include <unistd.h>
> #include <netdb.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <sys/param.h>
> #include <netinet/in.h>

[ ... ]

> int
> main() {
>   int sock1, sock2;
>   int optval = 1;
>   struct sockaddr addr;
>   struct sockaddr sock1Addr, sock2Addr;
>   int sock1AddrLen, sock2AddrLen;
>   short port1, port2;
>   char sendBuf, readBuf;
>   int hostAddress;
>
>   if ((hostAddress = getHostAddress()) == 0)
>     exit(1);
>
>   buildAddr4((struct sockaddr_in *)&addr, 0, 0);
>
>   if ((sock1 = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
>     exit(1);
>   if (setsockopt(sock1, SOL_SOCKET, SO_BROADCAST, (char*) &optval, sizeof(optval)) != 0)
>     exit(1);
>   if (bind(sock1, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0)
>     exit(1);

  +  sock1AddrLen = sizeof(sock1Addr);

>   if (getsockname(sock1, &sock1Addr, &sock1AddrLen) != 0)
>     exit(1);
>   setAddress((struct sockaddr_in *)&sock1Addr, hostAddress);
>
>   if ((sock2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
>     exit(1);
>   if (bind(sock2, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0)
>     exit(1);

  +  sock2AddrLen = sizeof(sock2Addr);

>   if (getsockname(sock2, &sock2Addr, &sock2AddrLen) != 0)
>     exit(1);
>   setAddress((struct sockaddr_in *)&sock2Addr, hostAddress);
>   if (connect(sock2, &sock1Addr, sock1AddrLen) != 0)
>     exit(1);
>
>   sendBuf = 22;
>   if (sendto(sock1, &sendBuf, 1, 0, &sock2Addr, sock2AddrLen) != 1)
>     exit(1);
>
>   if (read(sock2, &readBuf, 1) != 1)
>     exit(1);
>
>   printf("no hang\n");
> }

-- 
DE



More information about the freebsd-hackers mailing list