title: Establishing TCP socket connection between PHP client and C server on Ubuntu
body: <p>I am writing a simple PHP client and C server programs on Ubuntu 16.04. On client side I have made a simple graphical user interface in HTML, in which there is only one drop down box, and a submit button. When I click on the button, PHP client code is executed (though Apache server), but the client does not connect to the server, which is initially launched in terminal.</p>
  
  <p>I want to pass the value of drop down box to the C server, whatever user selects from drop down. Please help me to figure out what's wrong with my code.</p>
  
  <p><strong>Server.c</strong></p>
  
  <pre><code>//All Libraries are present including pthread
  
  #define BUFFER_SIZE 1024
  
  //the thread function
  
  void *connection_handler(void *);
  
  int main(int argc , char *argv[])
  {
  
    int socket_desc , client_sock , c , *new_sock;
  
    struct sockaddr_in server , client;
  
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  
    if (socket_desc == -1)
    {
  
      printf("Could not create socket");
  
    }
  
    puts("Socket created");
  
    server.sin_family = AF_INET;
  
    server.sin_addr.s_addr = INADDR_ANY;
  
    server.sin_port = htons( 8888 );
    //Bind
    if( bind(socket_desc,(struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
    {
  
  
      perror("bind failed. Error");
      return 1;
    }
    puts("bind done");
  
    listen(socket_desc , 3);
  
    c = sizeof(struct sockaddr_in);
  
    int threadID;
  
    //Accept and incoming connection
  
    puts("Waiting for incoming connections...");
  
    c = sizeof(struct sockaddr_in);
  
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&amp;client, (socklen_t*)&amp;c)) )
  
    {
  
      puts("Connection accepted");
  
      pthread_t sniffer_thread;
  
      new_sock = malloc(1);
  
      *new_sock = client_sock;
  
      threadID=pthread_create( &amp;sniffer_thread , NULL ,  connection_handler , (void*) new_sock);
  
      if( threadID &lt; 0)
  
      {
  
        perror("could not create thread");
  
        return 1;
  
      }
  
      puts("Handler assigned");
  
    }
  
  
  
    if (client_sock &lt; 0)
  
    {
  
      perror("accept failed");
  
      return 1;
  
    }
  
    return 0;
  
  }
  
  void *connection_handler(void *socket_desc)
  
  {
  
    int read_size;
  
    char *serverErrorMsg="Invalid Details!";
  
    char client_message[2000],client_poll[2000];
  
    int sock = *(int*)socket_desc;
  
  
    //Receive a message from client
  
    if( (read_size = recv(sock , client_message , 2000 , 0)) &gt; 0 )
  
    {
      puts("Server Received: ");
  
      puts(client_message);
  
    }
  
    else
  
    {
  
      write(sock , serverErrorMsg , strlen(serverErrorMsg));
  
    }
  
    free(socket_desc);    
  
    return 0;
  
  }
  </code></pre>
  
  <p><strong>Client.php</strong></p>
  
  <pre><code>&lt;?php 
  
  error_reporting(E_ALL);
  
  $host    = "127.0.0.1";
  
  $port    = 8888;
  
  // create socket
  
  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  
  // connect to server
  
  $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
  
  // sending test type to server
  
  $selectedType = $_POST['testType']; //html drop down with name='testType'
  
  socket_write($socket, $selectedType, strlen($message)) or die("Could not send data to server\n");
  
  socket_close($socket);
  
  ?&gt;
  </code></pre>