-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat_client.c
91 lines (67 loc) · 1.78 KB
/
chat_client.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFSIZE 1024
void msg_out_error( char *msg );
int main( int argc, char *argv[] )
{
int sock;
char sendBuf[BUFSIZE];
char recvBuf[BUFSIZE];
size_t send_len;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
if( argc != 3 )
{
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
sock = socket( PF_INET, SOCK_STREAM, 0 );
if( sock == -1 )
msg_out_error("socket() error");
memset( &serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if( connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1 )
msg_out_error("connect() error");
printf("~~~~~1:1 chatting start!~~~~~\n");
printf("~~~~~If you want to exit, enter '/quit'~~~~~\n");
while(1)
{
//create buffer to input
memset(sendBuf, 0, strlen(sendBuf));
printf("Client : ");
fgets(sendBuf, BUFSIZE, stdin);
send_len = strlen(sendBuf);
ssize_t comu_num = send( sock, sendBuf, send_len, 0);
if( comu_num == -1 )
msg_out_error("send() error");
if( strcmp(sendBuf, "/quit\n") == 0 )
break;
memset(recvBuf, 0, sizeof(recvBuf));
comu_num = recv(sock, recvBuf, BUFSIZE, 0);
if(comu_num == -1 )
msg_out_error("recv() error");
if( strcmp(recvBuf, "/quit\n") == 0 )
break;
printf("from %s : %s", inet_ntoa(serv_addr.sin_addr),recvBuf);
memset(recvBuf, 0, sizeof(recvBuf));
}
fputc('\n', stdout);
printf("~~~~~1:1 chat has ended~~~~~\n");
close(sock);
return 0;
}
void msg_out_error(char* msg)
{
fputs(msg, stderr);
fputc('\n', stderr);
exit(1);
}