UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » recv and unix domain sockets

#1 2007-04-21 03:00 PM

segfault
Guest

Re: recv and unix domain sockets

Hi,

I'm trying to write a client server program using unix domain sockets.
But I ran into a weird error.

Server

while((n=recv(sock2,(void *) &code, sizeof(unsigned), 0)) > 0)
        {

                reply = code + 2000;
                printf("Sending back response %u...\n", reply);
                while((send(sock2, (void *) &reply, sizeof(unsigned), MSG_DONTWAIT)) > 0)
                        printf(".");
                printf("\ndone sending\nsent: %u\n", reply);
                printf("-------------------\n");
        }
        if(n < 0)
                perror("recv");

Client (this code is in a big infinite while loop)

if ((nsent = send(sock, (void *) &buttoncode, sizeof(unsigned), MSG_DONTWAIT)) == -1) {
                perror("send");
                exit(1);
        }

        while((nrecv = recv(sock, (void *) &reply, sizeof(unsigned), 0)) > 0 && nrecv != sizeof(unsigned))
                printf("\n%d\n", nrecv);
        if(nrecv < 0)
                perror("recv");

        printf("received: %u\n", reply);
        nrecv=0;
        reply=0;

What it (should) do.
Client sends a number (0 to 100) to the server.
Server adds 2000 to it, and sends it back.

What really happens.
Server seems to be doing everything it should do.
But the client keeps showing the first number the server sent back after every number it sends.
So instead of getting the number +2000. I just keep getting 2000 on every reply from the server. So it would seem there's something wrong with the recv function on the client side?

Anybody have any idea what causes this?

Thanks

#2 2007-04-21 03:30 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: recv and unix domain sockets

First of, no need to cast anything to void*, or else Rob gets upset. ;-)

Basically your while loops are wrong. Why keep sending and receiving data when it succeeded the first time?

while((send(sock2, (void *) &reply, sizeof(unsigned), MSG_DONTWAIT)) > 0)

Makes the server send the same value until its send buffer is full.

Offline

#3 2007-04-21 06:16 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: recv and unix domain sockets

Yes, and once you fix that problem in the server, you'll probably also want to change
the client to do its send() inside the recv() loop, as well...  Basically, you want to do
one send() for every recv(), I think...  If that's NOT your intentions, and you really
want the server to just keep sending over and over forever after a single recv(),
then you probably want to add the increment by 2000 inside the current send() loop
there...

Offline

  • Index
  • » C
  • » recv and unix domain sockets

Board footer

Powered by FluxBB