views:

414

answers:

2

Hi,

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 ...

+1  A: 

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.

tommieb75
thanks for reply! both use the same port. Do you mean I should give one more higher?
make
@mk: no! Both ports should be the same but they have exceeded the maximum port no which is maximum of 16 bits is 65536, give it a lower port say...12345 for both client and server...
tommieb75
@tommieb75: Thanks very much for your comment as I haven't noticed that the port was higher than the limit. As I work on Solaris, I though the port depends on the OS, which is 32 or 64bits... thanks gain, it was a nice a comment I received today from you ...
make
I changed the ports to 2023 annd the problem is still on. client and server don't exchange data, although they connect to each other ...
make
@mk - what do you mean they connect? UDP is a connectionless protocol?
jschmier
yes! I mean client and server recognize each other ... but they don't exchange data
make
A: 

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.

Suroot
thanks for reply! please give an example ...
make