y0u_bat

c언어 - 소켓프로그래밍(1:1 채팅) 본문

프로그래밍/C언어

c언어 - 소켓프로그래밍(1:1 채팅)

유뱃 2015. 11. 19. 22:02



chatting_client.cpp

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
 
#define BUF_SIZE 1024
 
static pthread_t thr;
static int thr_id;
static bool thr_exit = true;
static char recv_data[BUF_SIZE];
static int client_fd,len,n,n2;
static void *treturn;
 
void *thread_recv(void *arg);
 
void thread_start()
{
        thr_exit = false;
        thr_id = pthread_create(&thr,NULL,thread_recv,NULL);
 
}
 
void thread_stop()
{
    thr_exit = true;
    thr_id = pthread_join(thr,&treturn);
}
 
void *thread_recv(void *arg)
{
    while(!thr_exit)
    {
         if((n = recv(client_fd,recv_data,sizeof(recv_data),0)) == -1)
         {
             printf("disconnect!!\n");
             return (int*)0;
         }
         else if(n > 0)
         {    
             recv_data[n] = '\0';
             printf("\n[관리자]: %s\n",recv_data);
         }
    }
 
    pthread_exit((void*)0);
}
 
int main(int argc,char *argv[])
{
    struct sockaddr_in client_addr;
    char *IP = argv[1];
    in_port_t PORT = atoi(argv[2]);
    char chat_data[BUF_SIZE];
    
    if(argc != 3)
    {
        printf("Usege : ./filename [IP] [PORT] \n");
        exit(0);
    }
 
    
    client_fd = socket(PF_INET,SOCK_STREAM,0);
    
    client_addr.sin_addr.s_addr = inet_addr(IP);
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(PORT);
    
            
    if(connect(client_fd,(struct sockaddr *)&client_addr,sizeof(client_addr))== -1)
    {
        printf("Can't connect\n");
        close(client_fd);
        return -1;
 
    }
    
    while(1)
    {
        thread_start();
        fgets(chat_data,sizeof(chat_data),stdin);
        send(client_fd,chat_data,sizeof(chat_data),0);
    }
    
    thread_stop();
 
 
    close(client_fd);
 
    return 0;
}
 
 
 
cs





chatting_server.cpp

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
 
#define BUF_LEN 1024
 
static pthread_t thr;
static bool thr_exit = true;
static int thr_id;
static void *treturn;
static struct sockaddr_in server_addr,client_addr;
static int server_fd,client_fd,n,n2;
static char recv_data[BUF_LEN];
 
 
void *thread_recv(void *arg);
 
void thread_start()
{
    thr_exit = false;
    thr_id = pthread_create(&thr,NULL,thread_recv,NULL);
}
 
void thread_stop()
{
    thr_exit = true;
    thr_id = pthread_join(thr,&treturn);
}
 
void *thread_recv(void *arg)
{
    while(!thr_exit)
    {
        if((n = recv(client_fd,recv_data,sizeof(recv_data),0)) == -1)
        {
            printf("[접속자: %s(%d)] disconnect\n",inet_ntoa(client_addr.sin_addr)
                    ,ntohs(client_addr.sin_port));
            thread_stop();
            close(client_fd);
            
 
        }else if(n > 0)
        {
            recv_data[n] = '\0';    
            printf("\n[접속자님: %s(%d)]: %s\n",inet_ntoa(client_addr.sin_addr)
                    ,ntohs(client_addr.sin_port),recv_data);
        }
    }
 
        pthread_exit((void *)0);
 
}
 
int main(int argc,char *argv[])
{
    char chat_data[BUF_LEN];
    char temp[20];
    int len;
 
    if(argc !=2)
    {
        printf("Usege ./filename [PORT] \n");
        exit(0);
    }
 
    if((server_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
    {
        printf("Server: can not Open Socket\n");
        exit(0);
    }
    
    memset(&server_addr,0x00,sizeof(server_addr));
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(atoi(argv[1]));
    
    if(bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))< 0)
    {
        printf("Server: cat not bind local addrss\n");
        exit(0);
        
    }
    
    
    if(listen(server_fd,5< 0)
    {
        printf("Server: cat not listen connnect.\n");
        exit(0);
    }
    
    
    memset(recv_data,0x00,sizeof(recv_data));
    len = sizeof(client_addr);
    printf("=====[PORT] : %d =====\n",atoi(argv[1]));
    printf("Server : wating connection request.\n");
 
    
    while(1)
    {
        client_fd = accept(server_fd,(struct sockaddr *)&client_addr,(socklen_t *)&len);
        
        if(client_fd < 0)
        {
            printf("Server: accept failed\n");
            exit(0);
        }
        
        inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,temp,sizeof(temp));
        printf("Server: %s client connect.\n",temp);
 
        printf("\n%s(%d)님이 들어오셨습니다. 나가려면 (quit)을 누르세요\n",
                inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
        while(1)
        {
        
            thread_start();        
            fgets(chat_data,sizeof(chat_data),stdin);
            if((n2 = send(client_fd,chat_data,sizeof(chat_data),0)) == -1)
            {
                break;
            }
        
        }
        
        thread_stop();
        close(client_fd);
        printf("Server: %s client closed.\n",temp);
        
    }
    
    close(server_fd);
    
    return 0;
}
 
 
 
cs






chatting_server






chatting_client



Comments