Making asynchronous socket server and client working ( written in C)
Could some please help me with these codes? I am trying to make client and server to communicate asynchronously. I mean both client and server don't wait for each other (e.g. when a server or client reads from recvfrom() and data are not present, it takes the last received one开发者_开发问答 (what I named is backup). Here are the codes:
client
...
/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Set signal handler for SIGIO */
handler.sa_handler = SIGIOHandler;
/* Create mask that mask all signals */
if (sigfillset(&handler.sa_mask) < 0)
DieWithError("sigfillset() failed");
/* No flags */
handler.sa_flags = 0;
if (sigaction(SIGIO, &handler, 0) < 0)
DieWithError("sigaction() failed for SIGIO");
/* We must own the socket to receive the SIGIO message */
if (fcntl(sock, F_SETOWN, getpid()) < 0)
DieWithError("Unable to set process owner to us");
/* Arrange for nonblocking I/O and SIGIO delivery */
if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
DieWithError("Unable to put server sock into non-blocking");
...
server ...
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Set up the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interf*/
echoServAddr.sin_port = htons(echoServPort); /* Port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");
/* Set signal handler for SIGIO */
handler.sa_handler = SIGIOHandler;
/* Create mask that mask all signals */
if (sigfillset(&handler.sa_mask) < 0)
DieWithError("sigfillset() failed");
/* No flags */
handler.sa_flags = 0;
if (sigaction(SIGIO, &handler, 0) < 0)
DieWithError("sigaction() failed for SIGIO");
if (fcntl(sock, F_SETOWN, getpid()) < 0)
DieWithError("Unable to set process owner to us");
/* Arrange for nonblocking I/O and SIGIO delivery */
if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
DieWithError("Unable to put client sock into non-blocking");
...
The codes are compiled and linked with any problem but they don't exchange data to each other , why? ... is there a problem somewhere?
Thanks for your replies,
PS: the codes are now removed ...
Check your port, I think they maxed out...should be 65535 which is the maximum for a port number (16 bits)!
Give it a smaller number and you should be ok!
Edit: The maximum number of ports used is 65536, which is a maximum 16 bits as it is a short int. If you go over the maximum, it will fail. Give both client and server a arbitary port number that is greater than 1024 and less than 65536.
Have a look at the Beej's Guide to socket programming...
Hope this helps, Best regards, Tom.
During your recv you can use MSG_PEEK | MSG_DONTWAIT as options to recv which will allow you to just check if there is any data that needs to be read.
精彩评论