Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Networking
cancel
Showing results for 
Search instead for 
Did you mean: 

HPUX 11.31 IA64 Non-blocking socket connection failing

HPUX 11.31 IA64 Non-blocking socket connection failing

Hi,

I have written a client c program which connects to a server socket on the specified port. I have compiled the program using gcc. If I am marking my socket as non-blocking, the connection fails. If I comment out the code which makes my socket non-blocking, the connection succeeds.

Here is my client code:

#include
#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket");
/* Code to mark the socket as non-blocking */
int flags;
flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect( sockfd, &serv_addr, sizeof(serv_addr)) < 0)
{
printf("ERROR connecting!!");
}

printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
printf("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
printf("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}

Is this a TCP layer issue? How can I get non-blocking sockets to connect successfully?

Thanks a lot,
Jithin
1 REPLY

Re: HPUX 11.31 IA64 Non-blocking socket connection failing

Hi,

I found out that connect() on a non-blocking socket will basically always return EINPROGRESS immediately. We have to do select() to establish connection.

A useful thread:
http://www.developerweb.net/forum/showthread.php?t=3196

Thanks