-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
99 lines (90 loc) · 1.98 KB
/
client.c
File metadata and controls
99 lines (90 loc) · 1.98 KB
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<sys/socket.h>
#include<stdlib.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<poll.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
struct sigaction old_action;
int sockfd;
void sigint_handler(int sig_no){
printf("exiting\n");
send(sockfd, "/exit", 255, 0);
close(sockfd);
sigaction(SIGINT, &old_action, NULL);
kill(0, SIGINT);
}
int main(){
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = &sigint_handler;
sigaction(SIGINT, &action, &old_action);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address = {
AF_INET,
htons(8888),
0
};
if (connect(sockfd, &address, sizeof(address)) != 0) {
perror("call to connect failed");
close(sockfd);
return 1;
}
struct pollfd fds[2] = {
{
0,
POLLIN,
0
},
{
sockfd,
POLLIN,
0
}
};
char channel[15] = "#main";
while (0 == 0){
char buffer[256] = { 0 };
char nick[30];
poll(fds, 2, 50000);
if (fds[0].revents & POLLIN) {
read(0, buffer, 255);
if (strcmp(buffer, "/exit\n") == 0){
send(sockfd, "/exit", 255, 0);
break;
}
else if (strcmp(buffer, "/join\n") == 0){
send(sockfd, "/exit", 255, 0);
close(sockfd);
printf("channel to join: ");
scanf("%s", channel);
strcpy(channel, strtok(channel, "\n"));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (connect(sockfd, &address, sizeof(address)) != 0) {
perror("call to connect failed");
close(sockfd);
return 1;
}
}
else if (strcmp(buffer, "/nick\n") == 0){
printf("new nick: ");
scanf("%s", nick);
}else {
char tmp[256];
snprintf(tmp, 256, "%s:%s: %s", channel, strtok(nick, "\n"), strtok(buffer, "\n"));
send(sockfd, tmp, 255, 0);
}
} if (fds[1].revents & POLLIN){
if (recv(sockfd, buffer, 255, 0) == 0){ break; }
char tmp[15];
strcpy(tmp, buffer);
if (strcmp(channel, strtok(tmp, ":")) == 0){
printf("%s\n", buffer);
}
}
}
close(sockfd);
return 0;
}