I want to simply connect to a server and get a response. My program is written in c++, and you can see the code here:
if((interpreterSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return SOCKETERR;
}
fcntl(interpreterSocket, F_SETFD, FD_CLOEXEC);
setsockopt(interpreterSocket, SOL_SOCKET, SO_REUSEADDR, (char *) &flag, sizeof(flag));
setsockopt(interpreterSocket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
setsockopt(interpreterSocket, SOL_SOCKET, SO_REUSEADDR , (char *) &flag, sizeof(flag));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(INTERPRETERADDR);
address.sin_port = htons(INTERPRETERPORT);
adlen = sizeof(address);
if((rc = connect(interpreterSocket, (struct sockaddr *) &address, adlen)) < 0)
{
close(interpreterSocket);
return SOCKETERR;
}
The problem is when I run this program sometimes it has some trouble, so I have to kill the process. After that when I run the program, the connect function does not return and the program stops at the if line. I think this problem most be related to a socket that does not close in a proper manner.
I should mention that I run this program in CentOS.
Thanks in advance.
EDIT
I had one more problem, that caused the program stop at connect() function and it was some routing problems at the server.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…