I am facing a strange problem, maybe it's straightforward but my lack of much socket knowledge is not helping either.
I have created a socket program and it works perfectly when run in a separate program i.e within main separately as an independent exe. As you can see in the code, the socket fd returned is always 3 when run in this separate program.
When I copy the same piece of code and integrate this to another server program which which has it's own fd's opened, the select always returns 0 no matter what. The fd created in this case with my program is always 6.
See my program below. there is no problem in this piece of code, it's just when this is been run inside another server, the select always returns 0.
Can I create a new UDP socket fd when there is already one existing in the program. Why is this happening when my socket fd is different then already opened within the program.
Why is this happening, can anyone please guide me what to do or any pointers would be helpful.
#define MY_TIMEOUT_S 5
int mySock;
if ((mySock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Cannot create UDP socket");
return -1;
}
struct sockaddr_in myAddr;
myAddr.sin_family = AF_INET;
myAddr.sin_port = 0;
myAddr.sin_addr.s_addr = INADDR_ANY;
memset(&(myAddr.sin_zero), '\0', 8);
if (bind(mySock, (struct sockaddr*)&myAddr,
sizeof(struct sockaddr)) == -1) {
perror("Cannot bind UDP socket to desired port");
close(mySock);
return -1;
}
// here send to remote target host on udp socket and port
char buf[MAX_UDP_PACKET_SIZE];
struct sockaddr_in theirAddr;
int addrLen = sizeof(struct sockaddr);
fd_set readSet, curReadSet;
FD_ZERO(&readSet);
FD_SET(mySock, &readSet);
int maxFD = mySock;
while (true) {
struct timeval timeOutTV = {MY_TIMEOUT_S, 0};
memcpy(&curReadSet, &readSet, sizeof(fd_set));
int selectRet = select(maxFD+1, &curReadSet, NULL, NULL, &timeOutTV);
cout<<"select value =" <<selectRet<<endl;
if (selectRet == -1) {
if (errno == EINTR) {
continue; // Interrupted by signal, retry
}
close(mySock);
return -1;
} else if (selectRet == 0) {
close(mySock);
return -1;
}
if (FD_ISSET(mySock, &curReadSet)) {
int numBytes = recvfrom(mySock, buf, MAX_UDP_PACKET_SIZE, 0,
(struct sockaddr*)&theirAddr, (socklen_t*)&addrLen);
if (numBytes == -1) {
close(mySock);
}
}
}
Adding some details from lsof output
1) This is the target where I have to send my data sudo lsof -a -p 16061
target 16061 root 3r FIFO 0,8 0t0 153467 pipe
target 16061 root 4u IPv4 153470 0t0 UDP *:3290
target 16061 root 5u sock 0,6 0t0 153471 can't identify protocol
target 16061 root 6u IPv4 153472 0t0 TCP *:3290 (LISTEN)
target 16061 root 7u raw 0t0 153473 00000000:0001->00000000:0000 st=07
2) This is my above piece of code when run in a separate exe, in which only this is the code running inside main as the FD=3 works fine
sudo lsof -a -p 16112
myProg 16112 root 0u CHR 136,4 0t0 7 /dev/pts/4
myProg 16112 root 1u CHR 136,4 0t0 7 /dev/pts/4
myProg 16112 root 2u CHR 136,4 0t0 7 /dev/pts/4
myProg 16112 root 3u IPv4 153564 0t0 UDP *:60503
3) This is the web server where I integrated my same above piece of code which is working independently, but not now
this is the output of my web server running
webServ 18431 root 3u unix 0xc09f9b00 0t0 157975 socket
webServ 18431 root 4u IPv4 157976 0t0 UDP *:8080
webServ 18431 root 5u IPv4 157977 0t0 TCP *:8080 (LISTEN)
now I send request and on the server side my new integrated code is executed and I put an infinite loop and did not close the socket just to show the lsof output
4) Output after my integration
webServ 18449 root 3u unix 0xc09f9b00 0t0 157975 socket
webServ 18449 root 4u IPv4 157976 0t0 UDP *:8080
webServ 18449 root 5u IPv4 157977 0t0 TCP *:8080 (LISTEN)
webServ 18449 root 6u IPv4 158389 0t0 UDP *:48057
The only difference is when integrated to the server the socket FD is different that's the only difference.
Hope this helps in answering my question.
I am not sure why this is happening. Please help
selectwithmaxFd + 1would help? - Joseph Quinsey