TCP 통신과정 추적해보기 (draft)

이동욱

2021/04/20

Categories: 네트워크 Tags: 네트워크

추적 과정


추적하기 전에 클라이언트와 서버 코드를 컴파일 한다.

client.c

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define MAXLINE 1024

int main(int argc, char **argv) {
        struct sockaddr_in serveraddr;
        int server_sockfd;
        int client_len;
        char buf[MAXLINE];

        if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("error : ");
                return 1;
        }
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serveraddr.sin_port = htons(4000);

        client_len = sizeof(serveraddr);

        if(connect(server_sockfd, (struct sockaddr*)&serveraddr, client_len) == -1) {
                perror("connect error : ");
                return 1;
        }
        memset(buf, 0x00, MAXLINE);
        read(0, buf, MAXLINE);
        if(write(server_sockfd, buf, MAXLINE) <= 0) {
                perror("write error : ");
                return 1;
        }
        memset(buf, 0x00, MAXLINE);
        if(read(server_sockfd, buf, MAXLINE) <= 0) {
                perror("read error: ");
                return 1;
        }
        close(server_sockfd);
        printf("server:%s\n", buf);
        return 0;
}

server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#define BUFF_SIZE 1024

int main(void){
   int server_socket;
   int client_socket;
   int client_addr_size;
   struct sockaddr_in server_addr;
   struct sockaddr_in client_addr;
   char buff_rcv[BUFF_SIZE+5];
   char buff_snd[BUFF_SIZE+5];

   server_socket = socket(PF_INET, SOCK_STREAM, 0);
   if(-1 == server_socket){
      printf( "server socket 생성 실패n");
      exit( 1);
   }

   memset(&server_addr, 0, sizeof(server_addr));
   server_addr.sin_family     = AF_INET;
   server_addr.sin_port       = htons(4000);
   server_addr.sin_addr.s_addr= htonl(INADDR_ANY);

   if(-1 == bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))){
      printf( "bind() 실행 에러n");
      exit(1);
   }

   while(1){
      if(-1 == listen(server_socket, 5)){
         printf( "대기상태 모드 설정 실패n");
         exit(1);
      }

      client_addr_size  = sizeof( client_addr);
      client_socket     = accept( server_socket, (struct sockaddr*)&client_addr, &client_addr_size);

      if (-1 == client_socket){
         printf( "클라이언트 연결 수락 실패n");
         exit(1);
      }

      read (client_socket, buff_rcv, BUFF_SIZE);
      printf("receive: %s\n", buff_rcv);

      sprintf(buff_snd, "%ld : %s", strlen(buff_rcv), buff_rcv);
      write(client_socket, buff_snd, strlen(buff_snd)+1);          // +1: NULL까지 포함해서 전송
      close(client_socket);
   }
}
gcc -pg -g -o client client.c
gcc -pg -g -o server server.c

UFTRACE


Screen Shot 2021-04-20 at 11 57 42 PM

참고 문헌


>> Home