날뛰는 코드

AWS EC2 에 TCP 소켓 통신 서버 만들기 (17/09/04) 본문

AWS

AWS EC2 에 TCP 소켓 통신 서버 만들기 (17/09/04)

미 냉 2017. 9. 5. 17:12

원래 라즈베리파이로 서버를 만들려 했는데

 

아마존 웹 서버에 Amazon Elastic Compute Cloud (EC2)를 1년간 무료로 이용 가능 하다는 소리를 듣고 이쪽으로 노선을 돌렸다.

 

고정아이피도 제공해주고 공짜여서 더 좋은것 같다. 

 

AWS 에 가입하고 EC2의 Ubuntu instance 로 만들었다.

 

그리고 elastic ip설정을 해서 고정아이피로 할 수 있게 했다.

 

친구의 도움을 받아서 대충만 아네

 

http://blog.naver.com/wewwe96/221086485305

여기 참조

 

이거 하고 고정아이피로 소켓 통신을 하려했는데

 

TCP 설정을 하지 않으면 안되서 TCP 설정을 해야 했다.

 

AWS EC2 관리 들어가서

 

Security Groups 에 들어간다.

 

 

 

그러면 내가 생성한 인스턴스가 보이는데

 

난 우분투로 생성해놔서 우분투가 있다.

 

 

우분투를 체크하고 앳션을 눌러 edit inbound rules 에 들어간다.

 

 

 

들어가면 만들었을떄 SSH 밖에 설정을 해놓지 않아서 Custom TCP를 원하는 포트와 함께 추가하였다.

HTTP, HTTPS는 나중에 쓸수도 있을거같아서 일단 추가해 놓았다.

 

키 받고 키세팅하고 ssh shell 로 접속하면 된다.

 

난 MobaXterm을 쓴다. 파일 접근도 쉽고 x11도 자동으로 해줘서 편한것 같다.

 

우분투라 아이디가 ubuntu 엿다.

 

 

 

그리고

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
 
#define BUF_SIZE 512
#define MAX_CLNT 100
#define NAME_SIZE 20
 
void * handle_clnt(void * arg);
void send_msg(char * msg, int len);
void error_handling(char * msg);
 
int clnt_cnt = 0;
int clnt_socks[MAX_CLNT];
char clnt_name[NAME_SIZE] = {};
char clnt_names[MAX_CLNT][NAME_SIZE] = {};
char cLatLng[BUF_SIZE] = {};
char cSend_msg[BUF_SIZE] = {};
 
pthread_mutex_t mutx;
 
int main(int argc, char *argv[])
{
    int serv_sock, clnt_sock, i;
    struct sockaddr_in serv_adr, clnt_adr;
    socklen_t clnt_adr_sz;
    pthread_t t_id;
    if (argc != 2) {
        printf("Usage : %s <port>\n", argv[0]);
        exit(1);
    }
 
    pthread_mutex_init(&mutx, NULL);
    serv_sock = socket(PF_INET, SOCK_STREAM, 0);
 
    memset(&serv_adr, 0sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_adr.sin_port = htons(atoi(argv[1]));
 
    if (bind(serv_sock, (struct sockaddr*&serv_adr, sizeof(serv_adr)) == -1)
{
}
        //error_handling("bind() error");
    if (listen(serv_sock, 5== -1)
{
}
        //error_handling("listen() error");
        printf("waiting Client...\n");
 
    while (1)
    {
        clnt_adr_sz = sizeof(clnt_adr);
        clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
 
        if (clnt_cnt >= MAX_CLNT) {
            printf("CONNECT FAIL : %d \n", clnt_sock);
            write(clnt_sock, "too many users. sorry", BUF_SIZE);
            continue;
        }
 
 
        pthread_mutex_lock(&mutx);
 
        clnt_socks[clnt_cnt] = clnt_sock;
        read(clnt_sock, clnt_name, NAME_SIZE);
        strcpy(clnt_names[clnt_cnt++], clnt_name);
        // ㄴ 클라이언트로부터 받은 접속자 이름입력
        pthread_mutex_unlock(&mutx);
 
 
 
        pthread_create(&t_id, NULL, handle_clnt, (void*)&clnt_sock);
        pthread_detach(t_id); 
        printf("Connected client IP: %s, Name : %s \n", inet_ntoa(clnt_adr.sin_addr),clnt_name);
    }
    close(serv_sock);
    return 0;
}
 
void error_handling(char * msg)
{
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}
 
void * handle_clnt(void * arg)
{
    int n;
    printf("%s\n",cSend_msg); 
    while((n=read(*(int*)arg, cLatLng, BUF_SIZE))>0)
    {
        printf("arg: %d, LatLng: %s ,size:  %d, n: %d\n",*(int*)arg,cLatLng,(int)sizeof(cLatLng),n);
        if(write(*(int*)arg,cLatLng,n)==-1)
            printf("full error\n");
    }
}
 
cs

 

여러 클라이언트 받을 수 있게 서버를 아직 간단히 만들었다.

 

안드로이드에서 클라이언트가 보낸 맵좌표를 받아서 다시 보내주는 형식이다.

 

g++ m_server.cpp -o m_server.o -lpthread
./m_server.o 9000

 

컴파일 하고 포트번호를 넣고 실행시킨다.

 

 

클라리언트 접속 되고

 

받아서 보내기 된다. size는 버프사이즈 

 

n은 클라리이트에게 받은 데이터 크기, 보낼떄 크기

 

 

 

Comments