TCP echo server that only drives off the first packet (C language)

I wrote C code for a TCP server that resonates with what it gets. The problem is that when I send data for the first time, when they have something in common with it, and the next time the server sends back the first packet I sent. The log is as follows:

Client Send : Packet1
Server reply : Packet1
Client Send : Packet2
server reply : Packet1

The server code is as follows:

int main(int argc, char** argv) {
int listenfd,connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in servaddr,cliaddr;
listenfd = socket(AF_INET,SOCK_STREAM,0);
printf("Socket listenfd : %d    with %d And %d\n",listenfd,AF_INET,SOCK_STREAM);
bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
printf("Server address: %d\n",servaddr.sin_addr.s_addr);
bind(listenfd, (SA*) &servaddr, sizeof(servaddr));
printf("Listened: %d\n",listenfd);
listen(listenfd,LISTENQ);
printf("After Listening: %d\n",listenfd);
int num=0;
for( ; ; ){
    clilen=sizeof(cliaddr);
    connfd = accept(listenfd, (SA*) &cliaddr,&clilen);
    printf("Client no. %d connected\n",++num);
    if( (childpid=fork())==0){
        close(listenfd);
        echo(connfd);
        exit(0);
    printf("Client no. %d Terminated\n",++num);
    }
   close(connfd);
}
return (EXIT_SUCCESS);
}

And my echo function:

void echo(int sockfd) {
 ssize_t n;
 char    buf[MAXLINE];
 again:
 while ( (n = read(sockfd, buf, MAXLINE)) > 0)
     writen(sockfd, buf, n);
if (n < 0 && errno == EINTR)
     goto again;
 else if (n < 0)
     printf("read error");
}

main client code:

int main(int argc, char** argv) {
int sockfd;
struct sockaddr_in servaddr;

sockfd= socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_port= htons(SERV_PORT);
inet_pton(AF_INET,"0.0.0.0",&servaddr.sin_addr);
printf("%d , %d \n",sockfd,servaddr.sin_addr.s_addr);
connect(sockfd, (SA*) &servaddr,sizeof(servaddr));
printf("%d\n",sockfd);
replyBack(stdin,sockfd);

printf("RETURN\n");
return (EXIT_SUCCESS);
}

replyBack function:

void replyBack(FILE *fp, int sockfd) {
char sendline[MAXLINE], recvline[MAXLINE];
printf("ENTER  YOUR ECHOED:  \n");
while (fgets(sendline, MAXLINE, stdin) != NULL) {
    write(sockfd, sendline, sizeof(sendline));

    if (read(sockfd, recvline, MAXLINE) == 0)
    {
        printf("str_cli: server terminated prematurely");
        exit(-1);
    }
    fputs(recvline, stdout);

}
}
+3
source share
2 answers

Ok, look at this section of your code:

printf("After Listening: %d\n",listenfd);
int num=0;
for( ; ; ){
    clilen=sizeof(cliaddr);
    connfd = accept(listenfd, (SA*) &cliaddr,&clilen);
    printf("Client no. %d connected\n",++num);
    if( (childpid=fork())==0){
        close(listenfd);
        echo(connfd);
        exit(0);
    printf("Client no. %d Terminated\n",++num);
    }
   close(connfd);
}

A call exitterminates your application, so printfit will never be executed after it. Not enough, but worth pointing out.

, "" . , , , - :

if ( (childpid = fork ()) == 0 ) {
  echo ( connfd );
  close ( connfd );
  printf ( "Client no %d terminated.\n", num ); /* Don't use the ++ here or your count will be off */
  exit ( 0 );
}

:

void echo(int sockfd) {
  ssize_t n;
  char    buf[MAXLINE];
  again:
  while ( (n = read(sockfd, buf, MAXLINE)) > 0)
      writen(sockfd, buf, n);
 if (n < 0 && errno == EINTR)
      goto again;
 else if (n < 0)
      printf("read error");
}

, read write ( , IO), write , .

void
echo ( int sockfd )
{
  ssize_t bytes_in, bytes_out, bytes_remaining;
  int write_err;
  char buf[MAXLINE];
  char * send_start_pos;
  while ( 1 ) {
    bytes_in = read ( sockfd, buf, MAXLINE );
    if ( bytes_in < 1 ) {
      if ( errno == EINTR )
        continue;
      break; /* other error occurred, or EOF (0 bytes read) */
    }
    bytes_remaining = bytes_in;
    send_start_pos = buf;
    write_err = 0;
    while ( ( bytes_remaining > 0 ) && !( write_err ) ) {
      bytes_out = write ( sockfd, send_start_pos, bytes_remaining );
      if ( bytes_out < 0 ) {
        if ( errno == EINTR )
          continue;
        write_err = 1;
        break;
      }
      bytes_remaining -= bytes_out;
      send_start_pos += bytes_out;
    }
    if ( write_err )
      break;
  }
}

echo , . , echo, . , , , , .

, goto... , - .

+4

, TCP, , . .

, , , , ( ). .

.

, write read .

-, TCP - , , , . , . - , MAXLINE , (, writen(sockfd, buf, n) n , n MAXLINE).

, , . Telnet - : Telnet , -. ( ). , (UDP) .

+1

Source: https://habr.com/ru/post/1795813/


All Articles