pick the bug the server enters an infinite loop

here is the server and client side code now there is a bug after which the server enters an infinite loop.the server is designed as an echo server and if it reads /q then the server closes while the client can send messages till /q now after the frst msg when another msg is send infinite loop is entered
Server code

 while(j!=0)
       {
	  memset(buffer,0,1024);/*clear buffer*/
	 n = read(newsockfd,buffer,1024);/*read from client*/
	 if (n < 0) perror("\n ERROR reading from socket \n");/*check for errors*/
	 len=strlen(buffer);
	 puts(buffer);/*print message by client*/
	 write(newsockfd,"\n Client wrote \n",12);/*write to client*/
	  n=write(newsockfd,buffer,sizeof(buffer));/*write to client*/
	 if(n<0)perror("Write Failed");
	  while(i<len)
	   {
	     //printf("%d\n",i);
	     j= strcmp(buf,&buffer);
	     if(j==0)
	       break;
	     i+=2;
	   }	   

Client Code

while(j==0)
	  {
    printf("\n Enter Message for Server");
    gets(buf);
    /* now that we are connected, start writing to the socket */	
    /* till write() returns 0, meaning the server closed	*/
    /* the connection.				*/
    
    rc = write(s, buf,sizeof(buf)); 
          if(rc<0)
      {
	perror("write failed");
      }
	  else
	    {
	  read(s,buf1,12);
	  read(s,buf2,sizeof(buf));
	    }
	  puts(buf1);/*print messages from server*/
	  puts(buf2);/*print messages from server*/
	  printf("want to write more data");
	  gets(ans);
	  j=strcmp(ans,"yes");
	  break;
      }
      }
	 n = read(newsockfd,buffer,1024);/*read from client*/
	 if (n <= 0) 
	 {
	 	 perror("\n ERROR reading from socket \n");/*check for errors*/
	 	 break;
	 }

A way out of the loop when read returns zero or -1 would be good.

hey porter
the frst bug is fixed now another bug has come.if the client side after "want to send another message" types yes the server functions well but when apart from yes any other message is typed in the server again enters infinite loop.how to remove this

I could see that its not corrected for a snippet in the client code as well.

Use porter's point to address everywhere when expected return value is not returned from any of send / read

rc = write(s, buf,sizeof(buf)); 
          if(rc<0)
      {
	perror("write failed");
      }