2

Below is a part of code from my socket server. I am not clear about some steps so I have some questions. My questions are below:

  1. Why I need to use while (1)?
  2. What is the purpose of exit (-1), will it close my socket?
  3. Why the data_len is initialized as 1?
  4. If the server runs and there is no data from the client side, then what will happen to the server? Will it get closed?

Actually, I need a detail explanation for the below part of the code.

while(1) {
    if ((new = accept(sock, (struct sockaddr*)&client, &sockaddr_len)) == ERROR) {
        perror ("accept");
    exit (-1);

    }

    printf("New client connected from port no %d and IP %s\n",ntohs(client.sin_port), inet_ntoa(client.sin_addr));
data_len = 1;
while (data_len) {
    data_len = recv (new, data, MAX_DATA, 0);
    if (data_len) {
        send (new, data, data_len, 0);
    data [data_len]='\0';
    printf("Sent mesg: %s", data);
    }
}
printf("Client Disconnected\n");
close(new);
}
close (sock);
pwilmot
  • 315
  • 1
  • 7
user3751012
  • 533
  • 1
  • 8
  • 20
  • 1
    Be careful how you use the result returned by `recv`. It returns zero (i.e. false) when the connection is closed, and `-1` (which is non-zero and therefore true) on error. – Some programmer dude Jun 24 '14 at 16:09

4 Answers4

3

If you want to accept new connections after you're done with the first one, you need to do the same code over again to accept and handle the new connection. The easiest way to do it is by using a loop of some kind.

The exit function simply exits your process. Typically this will close all open sockets as well as release all other open/allocated resources. The function does not return.

If you don't initialize the data_len variable, its value will be indeterminate and using it will lead to undefined behavior. At least if the variable is a local non-static variable.

If there is no data from the client, the recv call will block indefinitely.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Joachim Pileborg, thanks! This is the answer of just one question. But I have understood your answer. Please try to answer the other questions. I hope you will be able to answer all my questions. – user3751012 Jun 24 '14 at 16:12
  • Joachim Pileborg,"the recv call will block indefinitely." so the server will run indefinitely? – user3751012 Jun 24 '14 at 16:15
  • @user3751012 Yes, until the client sends something, the client closes the connection or there is a network error the `recv` function simply will not return, leaving your server process hanging. Or of course you kill the process. – Some programmer dude Jun 24 '14 at 16:32
2
while(1)

is the same as

while(true)

which means loop forever until there is a break but in this case its not while(1) its while(data_len > 0)

exit(-1)

will end the program with a exit code -1. generally meaning there was an error

data_len is initialized to 1 so that the code enters the while loop. This value is overwritten in the first line of the loop anyways so the value of 1 doesn't really matter

pwilmot
  • 315
  • 1
  • 7
1

1 It is for looping forever, since C does not have a native boolean type, it is using integer and 1 is true

2 thats for closing the application, it is under your if statement when initialize the socket so the socket does not even created yet

3 It is the same as #1 is it to make the while statement true

4 If there are no client connected the server will keep running

user3309301
  • 301
  • 1
  • 4
1

while (1) just loops forever, or until the program exits.

exit(-1) causes the program to exit with a nonzero value (which means an error occurred). The resources used by the program are deallocated as the program exists, so yes, the socket will be closed.

datalen is initialized to 1 so the while loop body will execute.

If there is no incoming data, the program will sit patiently waiting for data, assuming you haven't created a nonblocking socket.

Fred
  • 8,582
  • 1
  • 21
  • 27