Server Client program in C using sockets and tcp
<p>I have written a program for simple text chatting between server and client using socket programming. The server side runs okay since it's listening but when I run the client side program it runs but shows no output, meaning nothing shows up on server side if I type in Client side. Why am I not getting any response from Client side?</p>

<pre><code>    /****************** CLIENT CODE ****************/

        #include &lt;stdio.h&gt;
        #include &lt;sys/socket.h&gt;
        #include &lt;netinet/in.h&gt;
        #include &lt;string.h&gt;

        int main(){
  int clientSocket,n;
  char buffer[256];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/
  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &amp;serverAddr, addr_size);

  if (connect(clientSocket,(struct sockaddr *) &amp;serverAddr,sizeof(serverAddr)) &lt; 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(clientSocket,buffer,strlen(buffer));
    if (n &lt; 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(clientSocket,buffer,255);
    if (n &lt; 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(clientSocket);
  /*---- Read the message from the server into the buffer ----*/
  //recv(clientSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
  //printf("Data received: %s",buffer);   

  return 0;
}


/****************** SERVER CODE ****************/

#include &lt;stdio.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;string.h&gt;

int main(){
  int welcomeSocket, newSocket;
  char buffer[256];
  int n;
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(welcomeSocket, (struct sockaddr *) &amp;serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  newSocket = accept(welcomeSocket, (struct sockaddr *) &amp;serverStorage, &amp;addr_size);

  /*---- Send message to the socket of the incoming connection ----*/
  bzero(buffer,256);
     n = read(welcomeSocket,buffer,255);
     if (n &lt; 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(welcomeSocket,"I got your message",18);
if (n &lt; 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(welcomeSocket,"I got your message",18);
     if (n &lt; 0) error("ERROR writing to socket");
     close(newSocket);
     close(welcomeSocket);

  return 0;
}
</code></pre>