connect() fails?
Daniel Ng
daniel_ng11 at lycos.com
Mon Oct 22 12:21:20 UTC 2007
Hi,
I'm using uClibc 0.9.28 in a buildroot environment.
I'm having trouble connecting to a TCP socket. connect() returns -1 with errno
set to '1' which from looking at the errno include files translates
to 'EPERM'.
According to the man page for connect(), this means I was trying to either
connect to a broadcast address, or connect() was being blocked by a local
firewall rule. I know I'm not using a broadcast address (it's 127.0.0.1), and
I have no firewalls on my embedded platform. I can connect to the telnet
server using the standard linux telnet client. Changing the FD flags makes no
difference.
Any suggestions would be most helpful, short of upgrading uClibc.
Cheers,
Daniel
Here's the code-
struct addrinfo hints;
struct addrinfo *res;
const char *hostname = "localhost";
const char *service = TELNET_PORT;
char buf[BUF_LEN];
char stringError[ERROR_LEN];
long flags = 0;
int fd = 0;
int connectResult = 0;
int error = 0;
if(memset(stringError, 0, ERROR_LEN) == NULL)
{
Dprintf("ERROR: memset(stringError) returned Null Pointer\n");
return -1;
}
if(memset(&hints, 0, sizeof(hints)) == NULL)
{
Dprintf("ERROR: memset(hints) returned Null Pointer\n");
return -1;
}
hints.ai_socktype = SOCK_STREAM; // Telnet uses TCP
hints.ai_family = AF_INET;
if ( (error = getaddrinfo(hostname, service, &hints, &res)) != 0 )
{
Dprintf("ERROR: getaddrinfo() returned error: %d\n", error);
return -1;
}
if ( (fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0 )
{
Dprintf("ERROR: socket() returned error: %d- %s\n",
fd, strerror_r(errno,stringError,ERROR_LEN));
return -1;
}
if ( fcntl(fd, F_GETFL, flags) < 0 )
{
Dprintf("ERROR: fcntl(F_GETFL) returned error: %d- %s\n",
fd, strerror_r(errno,stringError,ERROR_LEN));
return -1;
}
flags |= (O_NONBLOCK|O_ASYNC);
if ( fcntl( fd, F_SETFL, flags ) < 0 )
{
Dprintf("ERROR: fcntl(F_SETFL) returned error: %d- %s\n",
fd, strerror_r(errno,stringError,ERROR_LEN));
return -1;
}
if ( (connectResult = connect(fd, res->ai_addr, res->ai_addrlen)) < 0 )
{
if (errno != EINPROGRESS)
{
Dprintf("ERROR: connect() returned error: %d- %s\n",
connectResult, strerror_r(errno,stringError,ERROR_LEN));
return -1;
}
else
{
;
// Check connect() succeeded in this case?
// If connect fails then nothing else will work anyway.
}
}
freeaddrinfo(res);
More information about the uClibc
mailing list