Crystal Cat
Crystal Cat

Reputation: 151

socket programming in linux: send()

I'm a beginner in socket programming in linux env.

the code is :

void proccess_server(int s)
{

    ssize_t size =0 ;
    char buffer[1024];
    for(;;)
    {
        printf("proccess:%d proccessing socket :%d\n",getpid(),s);
        size = recv(s,buffer,sizeof(buffer),0);

        if(0 == size)
          return ;
        sprintf(buffer,"Response from server:%d,%d bytes altogether\n",getpid(),size);

        send(s,buffer,strlen(buffer)+1,0);
    }

}

and I don't understand why len param in send() add another byte (strlen(buffer)+1) in send(s,buffer,strlen(buffer)+1,0);

please help me !

Upvotes: 2

Views: 451

Answers (1)

tcak
tcak

Reputation: 2212

In C language, compiler puts a \0 (NULL) character at the end of every string. So while you are using a string, computer can understand where that string ends. In Pascal language example, compiler puts a byte front of string to store length of string.

This must be the reason why there is +1 there.

Upvotes: 2

Related Questions