Networking Forums

Networking Forums > Computer Networking > Linux Networking > sockets

Reply
 
 
Sam
Guest
Posts: n/a

 
      06-29-2004, 02:56 PM
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


 
Reply With Quote
 
 
 
 
Cameron Kerr
Guest
Posts: n/a

 
      06-29-2004, 04:39 PM
Sam <(E-Mail Removed)> 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
(E-Mail Removed) : http://nzgeeks.org/cameron/
Empowered by Perl!
 
Reply With Quote
 
ZoombyWoof
Guest
Posts: n/a

 
      07-07-2004, 08:54 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Max TCP sockets CJS Windows Networking 1 02-28-2007 06:20 PM
RAW TCP/IP sockets George Valkov Windows Networking 1 09-22-2006 02:49 AM
UDP sockets Sven Jacobs Linux Networking 0 05-10-2004 06:06 AM
sockets =?ISO-8859-1?Q?Andreas_R=F8sdal?= Linux Networking 5 02-03-2004 02:48 PM
raw sockets Linux Networking 1 12-22-2003 02:28 PM



1 2 3 4 5 6 7 8 9 10 11