[UNIX] 소켓 프로그래밍 활용

Taegang Yun·2023년 12월 9일
1

Unix 프로그래밍

목록 보기
19/19

인터넷 소켓(서버)

#define PORTNUM 9000

int main(){
	char buf[256];
	struct sockaddr_n sin, cli;
	int sd, ns, clientlen = sizeof(cli);

	sd = socket(AF_INET, SOCK_STREAM, 0);

	memset(&sin, '\0', sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORTNUM);
	sin.sin_addr.s_addr = inet_arrt("192.168.147.129");

	bind(sd, &sin, sizeof(sin));
	listen(sd, 5);
	
	ns = accept(ad, &cli, &clientlen);

	sprintf(buf, "Your IP Address is %s", inet_ntoa(cli.sin_addr));
	send(ns, buf, strlen(buf);

	close(ns);
	close(sd);
}

인터넷 소켓(클라이언트)

#define PORTNUM 9000

int main(){

	int sd;
	char buf[256];
	struct sockaddr_n sin;

	sd = socket(AF_INET, SOCK_STREAM, 0);

	memset(&sin, '\0', sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORTNUM);
	sin.sin_addr.s_addr = inet_addr("192.168.147.129");

	connect(sd, &sin, sizeof(sin));

	recv(sd, buf, sizeof(buf));

	close(sd);
}

데몬 프로세스

  • 네트워크를 통해 데이터를 주고받으며 동작하는 네트워크 프로그램은 일반적으로 서버와 클라이언트로 역할을 구분
  • 서버는 클라이언트의 요청에 따라 다양한 서비스를 제공하는 프로그램이며 데몬 프로세스 라고도 함
  • 반복 서버
    • 데몬 프로세스가 직접 클라이언트의 요청을 처리하는 형태
    • 네트워크 프로그램을 처음 도입한 당시에는 반복 서버 형태로 서비스를 제공
  • 동시 동작 서버
    • 데몬 프로세스가 직접 서비스를 제공 하지 않음
    • 서비스와 관련 있는 다른 프로세스를 fork() 함수로 생성한 후 이 프로세스를 클라이언트와 연결해 서비스를 제공
    • 데몬 프로세스의 개수가 너무 많아지는 문제 해결을 위해 도입
  • 반복 실행 서버
    • 데몬 프로세스가 직접 모든 클라이언트의 요청을 차례로 처리
    • 서버 프로그램이 클라이언트의 요청을 직접 처리하여 한 번에 한 클라이언트의 요청만 처리할 수 있음
    • 반복 실행 서버는 인터넷 소켓과 TCP(SOCK_STREAM)을 이용해 통신

반복 실행 서버(서버)

#define PORTNUM 9001

int main(){
	char buf[256];
	struct sockaddr_in sin, cli;
	int sd, ns, clientlen = sizeof(cli);

	memset(&sin, '\0', sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORTNUM);
	sin.sin_addr.s_addr = inet_addr("192.168.147.129");

	sd = socket(AF_INET, SOCK_STREAM, 0);

	bind(sd, &sin, sizeof(sin));

	listen(sd, 5);

	while(1){
		ns = accept(sd, &cli, &clientlen);
	
		sprintf(buf, "%s", inet_ntoa(cli.sin_addr));
		
		strcpy(buf, "Welcome to Network Server");
		send(ns, buf, strlen(buf), 0);
	
		recv(ns, buf, sizeof(buf), 0);

		printf("From Client : %s\n", buf);
		close(ns);
	}
	close(sd);
}

반복 실행 서버(클라)

#define PORTNUM 9001

int main() {
13     int sd;
14     char buf[256];
15     struct sockaddr_in sin;
16
17     memset((char *)&sin, '\0', sizeof(sin));
18     sin.sin_family = AF_INET;
19     sin.sin_port = htons(PORTNUM);
20     sin.sin_addr.s_addr = inet_addr("192.168.147.129");
21
22     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
23         perror("socket");
24         exit(1);
25     }
26
27     if (connect(sd, (struct sockaddr *)&sin, sizeof(sin))) {
28         perror("connect");
29         exit(1);
30     }
31
32     if (recv(sd, buf, sizeof(buf), 0) == -1) {
33         perror("recv");
34         exit(1);
35     }
36
37     printf("** From Server : %s\n", buf);
38
39     strcpy(buf, "I want a HTTP Service.");
40     if (send(sd, buf, sizeof(buf) + 1, 0) == -1) {
41         perror("send");
42         exit(1);
43     }
44
45     close(sd);
46 }

동시 동작 서버

동시 동작 서버(서버)

#define PORTNUM 9002
10
11 int main() {
12     char buf[256];
13     struct sockaddr_in sin, cli;
14     int sd, ns, clientlen = sizeof(cli);
15
16     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
17         perror("socket");
18         exit(1);
19     }
20
21     memset((char *)&sin, '\0', sizeof(sin));
22     sin.sin_family = AF_INET;
23     sin.sin_port = htons(PORTNUM);
24     sin.sin_addr.s_addr = inet_addr("192.168.147.129");
25
26     if (bind(sd, (struct sockaddr *)&sin, sizeof(sin))) {
27         perror("bind");
28         exit(1);
29     }
30
31     if (listen(sd, 5)) {
32         perror("listen");
33         exit(1);
34     }
36     while (1) {
37         if ((ns = accept(sd, (struct sockaddr *)&cli,
38                         &clientlen)) == -1) {
39             perror("accept");
40             exit(1);
41         }
42         switch (fork()) {
43             case 0:
44                 strcpy(buf, "Welcome to Server");
45                 if (send(ns, buf, strlen(buf) + 1, 0) == -1) {
46                     perror("send");
47                     exit(1);
48                 }
49
50                 if (recv(ns, buf, sizeof(buf), 0) == -1) {
51                     perror("recv");
52                     exit(1);
53                 }
54                 printf("** From Client: %s\n", buf);
55                 close(ns);
56                 sleep(5);
57                 exit(0);
58         }
59     }
60     close(sd);
61 }

동시 동작 서버(서버) - exec() 함수 사용

#define PORTNUM 9003
10
11 int main() {
12     struct sockaddr_in sin, cli;
13     int sd, ns, clientlen = sizeof(cli);
14
15     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
16         perror("socket");
17         exit(1);
18     }
19
20     printf("** Create Socket\n");
21
22     memset((char *)&sin, '\0', sizeof(sin));
23     sin.sin_family = AF_INET;
24     sin.sin_port = htons(PORTNUM);
25     sin.sin_addr.s_addr = inet_addr("192.168.147.129");
26
27     if (bind(sd, (struct sockaddr *)&sin, sizeof(sin))) {
28         perror("bind");
29         exit(1);
30     }
31     printf("** Bind Socket\n");
32
33     if (listen(sd, 5)) {
34         perror("listen");
35         exit(1);
36     }
37     printf("** Listen Socket\n");
38
39     while (1) {
40         if ((ns = accept(sd, (struct sockaddr *)&cli,
41                         &clientlen)) == -1) {
42             perror("accept");
43             exit(1);
44         }
45         printf("** Accept Client\n");
46
47         switch (fork()) {
48             case 0:
49                 printf("** Fork Client\n");
50                 close(sd);
51                 dup2(ns, STDIN_FILENO);
52                 dup2(ns, STDOUT_FILENO);
53                 close(ns);
54                 execl("./han", "han", (char *)0);
55         }
56         close(ns);
57     }
58 }

동시 동작 서버(클라) - exec() 함수 사용

10 #define PORTNUM 9003
11
12 int main() {
13     int sd, len;
14     char buf[256];
15     struct sockaddr_in sin;
16
17     memset((char *)&sin, '\0', sizeof(sin));
18     sin.sin_family = AF_INET;
19     sin.sin_port = htons(PORTNUM);
20     sin.sin_addr.s_addr = inet_addr("192.168.147.129");
21
22     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
23         perror("socket");
24         exit(1);
25     }
26
27     printf("==> Create Socket\n");
28     if (connect(sd, (struct sockaddr *)&sin, sizeof(sin))) {
29         perror("bind");
30         exit(1);
31     }
32
33     printf("==> Connect Server\n");
34     if ((len = recv(sd, buf, sizeof(buf), 0)) == -1) {
35         perror("recv");
36         exit(1);
37     }
38     buf[len] = '\0';
39
40     printf("==> From Server : %s\n", buf);
41
42     close(sd);
43 }
profile
언젠간 전문가가 되겠지

0개의 댓글