- sockets

PDA

View Full Version : sockets


Sam
07-25-2004, 01:36 AM
Hi guys, Just a problem with sockets


I've done a little C client/server


Server side, "classic" :


....
while (true)
{
sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen);

n = recv(sd_client,buffer,sizeof(buffer),0);

buffer[n]=0;
printf("from cli : %s\n", buffer);


sprintf(buffer, "ok %d\n", j++);
if (send(sd_client,buffer,strlen(buffer),0) == -1)
{
perror("send");
exit(1);
}

close (sd_client);
}



But now, when I try to connect to the client:


First connection works perfectly (server receives my stream)

n = send(sd, "hello",strlen("hello"),0);
n = recv(sd, buffer, bufsize,0);
buffer[n]=0;
printf ("from srv : %s\n",buffer);

Now, after that, if I try (again)

n = send(sd, "test 2",strlen("test 2"),0);
n = recv(sd, buffer, bufsize,0);
buffer[n]=0;
printf ("from srv : %s\n",buffer);

This time server doesn't receive datas at all ???

Where is my mistake ?


thanx

Cameron Kerr
07-25-2004, 01:36 AM
Sam <spe@x-media.fr> wrote:

> while (true)
> {
> sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen);
>

You need to be doing this part in a loop it would seem.

> n = recv(sd_client,buffer,sizeof(buffer),0);
>
> buffer[n]=0;
> printf("from cli : %s\n", buffer);
>
>
> sprintf(buffer, "ok %d\n", j++);
> if (send(sd_client,buffer,strlen(buffer),0) == -1)
> {
> perror("send");
> exit(1);
> }


> n = send(sd, "hello",strlen("hello"),0);
> n = recv(sd, buffer, bufsize,0);

> This time server doesn't receive datas at all ???

I suggest strongly that you get your hands on 'Unix Network
Programming', by Stevens. It'll tell you pretty much everything you need
to know about how to use sockets under Unix.

--
Cameron Kerr
cameron.kerr@paradise.net.nz : http://nzgeeks.org/cameron/
Empowered by Perl!

ZoombyWoof
07-25-2004, 01:37 AM
Sam wrote:

> Hi guys, Just a problem with sockets
>
>
> I've done a little C client/server
>
>
> Server side, "classic" :
>
>
> ...
> while (true)
> {
> sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen);
>
> n = recv(sd_client,buffer,sizeof(buffer),0);
>
> buffer[n]=0;
> printf("from cli : %s\n", buffer);
>
>
> sprintf(buffer, "ok %d\n", j++);
> if (send(sd_client,buffer,strlen(buffer),0) == -1)
> {
> perror("send");
> exit(1);
> }
>
> close (sd_client);
> }
>
>
>
> But now, when I try to connect to the client:
>
>
> First connection works perfectly (server receives my stream)
>
> n = send(sd, "hello",strlen("hello"),0);
> n = recv(sd, buffer, bufsize,0);
> buffer[n]=0;
> printf ("from srv : %s\n",buffer);
>
> Now, after that, if I try (again)
>
> n = send(sd, "test 2",strlen("test 2"),0);
> n = recv(sd, buffer, bufsize,0);
> buffer[n]=0;
> printf ("from srv : %s\n",buffer);
>
> This time server doesn't receive datas at all ???
>
> Where is my mistake ?
Your server closes the socket after the first message is received.
From the code you provided the client does NOT open a new connection
before the second message is sent to the server. Skip the close (sd_client)
in your server loop if you dont make a new connection before the second
call in the client.

/ZW