1

If I run my below socket server program, it does not wait for a connection from the client in "accept" stage, rather it starts running in an infinite loop. My program is saved as server.c and I tried to run it in the command line with the below command:

$ ./server /tmp/socket

Full Socket Server Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>     



int server (int client_socket)
{
    while (1) {
        int length;
        char* text;             

        if (read (client_socket, &length, sizeof (length)) == 0) 
            return 0;

        text = (char*) malloc (length);

        read (client_socket, text, length);
        printf ("%s\n", text);

        free (text);

        if (!strcmp (text, "quit"))
            return 1;
    }
}


int main (int argc, char* const argv[])
{
    const char* const socket_name = argv[1];                
    int socket_fd; 
    struct sockaddr_un name; 
    int client_sent_quit_message;           
    socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); 

    name.sun_family = AF_LOCAL; 
    strcpy (name.sun_path, socket_name); 
    bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)); 

    listen (socket_fd, 5); 

    do {
        struct sockaddr_un client_name;

        socklen_t client_name_len;
        int client_socket_fd;

        client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
        client_sent_quit_message = server (client_socket_fd); 

        close (client_socket_fd);
      }
    while (!client_sent_quit_message);

    close (socket_fd);
    unlink (socket_name);
    return 0;
}   
harper
  • 13,345
  • 8
  • 56
  • 105
user3751012
  • 533
  • 1
  • 8
  • 20
  • `Why we need to write the below three lines?` Have you know about some basic concept of `C`? `malloc` and `free` are basic concept of `c`. Refresh your mind with [this link](http://www.programiz.com/c-programming/c-dynamic-memory-allocation) – Jayesh Bhoi Jun 30 '14 at 10:48
  • It is really easy to understand malloc and free but could you explain how the lines are actually working? @Jayesh – user3751012 Jun 30 '14 at 10:55
  • If it's easily understand to you then above line just use it. you must have idea about it. – Jayesh Bhoi Jun 30 '14 at 11:13
  • @user3751012, whether memory is allocated for particular line of code `text = (char*) malloc(length);`? – Kalanidhi Jun 30 '14 at 12:50
  • You are ignoring the returned result from read, (except for checking against 0), plus, do you understand what 'printf ("%s\n", text)' does? – Martin James Jun 30 '14 at 13:57
  • 4
    If you don't check error codes, you don't care whether your program runs correctly or not. – n. m. could be an AI Jul 01 '14 at 10:14

1 Answers1

1

Try the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>     
int server (int client_socket)
{
  while (1) {
    int length=30;
    char* text;             
    text = (char*) malloc (length);
    if (read (client_socket, text, length) == 0) 
        return 0;
    printf ("%s\n", text);
    if (!strcmp (text, "quit"))
    {
        free(text);
        return 1;
    }
    free (text);
  }
}
int main (int argc, char* const argv[])
{ 
  const char* const socket_name = argv[1];                
  int socket_fd; 
  struct sockaddr_un name; 
  int client_sent_quit_message;           
  socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); 
  name.sun_family = AF_LOCAL; 
  strcpy (name.sun_path, socket_name); 
  bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)); 
  listen (socket_fd, 5); 
  do {
    struct sockaddr_un client_name;
    int client_name_len=sizeof(struct sockaddr_un);
    int client_socket_fd;
    client_socket_fd = accept (socket_fd,(struct sockaddr *)&client_name, &client_name_len);
    client_sent_quit_message = server (client_socket_fd); 
    close (client_socket_fd);
}
  while (!client_sent_quit_message);
  close (socket_fd);
  unlink (socket_name);
  return 0;
}

The mistake is in accept function you didn't type cast the sockaddr_un to sockaddr *, then 3rd argument is not valid, we can't directly type cast the socklen_t to socklen_t * instead store the value in integer and give the pointer as the argument.

don't use this type of length:
read (client_socket, &length, sizeof (length));

It read the input from the socket and try to assign in &length, then you try to store the value in character variable, that time something missed according to the length is given or length is read.

Chandru
  • 1,306
  • 13
  • 21
  • 1
    There is a use after free for text in `server()` copied from the original code. – thuovila Jul 01 '14 at 12:03
  • Ahem.. 'printf ("%s\n", text)'. malloc : 'The content of the newly allocated block of memory is not initialized, remaining with indeterminate values'. Do I have to spell it out? – Martin James Jul 01 '14 at 16:28