-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpchatserver.c
135 lines (96 loc) · 3.08 KB
/
udpchatserver.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
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
#include "udpchat.h"
int main(int argc, char** argv){
int status, sockfd, recvBytes, sentBytes, connected;
struct addrinfo hints, *res, *p;
struct sockaddr_in clientAddr, otherClientAddr;
char clientIp[INET_ADDRSTRLEN], recvBuf[BUFFER_SIZE];
socklen_t addrLen;
int clientPort;
addrLen = sizeof clientAddr;
fd_set readfds, writefds;
if (argc != 1 && argc != 2) {
fprintf(stderr, "usage: %s [svrport]\n", argv[0]);
return 1;
}
printf("CMPE 207 HW4 udpchatsvr Hanbing Leng 354\n");
char *serverPort;
serverPort = (argc == 1) ? "9091" : argv[1];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(NULL, serverPort, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 1;
}
for (p = res; p != NULL; p = p->ai_next){
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
perror("bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "failed to connect\n");
return 1;
}
freeaddrinfo(res);
connected = 0;
while(1) {
//use copy
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
FD_SET(STDIN_FILENO, &readfds);
/*FD_ZERO(&writefds);*/
/*FD_SET(sockfd, &writefds);*/
select(sockfd + 1, &readfds, NULL, NULL, 0);
if (FD_ISSET(sockfd, &readfds) && !connected) {
if ((recvBytes = recvfrom(sockfd, recvBuf, BUFFER_SIZE - 1, 0, (struct sockaddr*)&clientAddr, &addrLen)) == -1) {
perror("recvfrom");
return 1;
}
connected = 1;
clientPort = ntohs(clientAddr.sin_port);
recvBuf[recvBytes] = '\0';
printTime();
printf(", <%s:%d>: %s", inet_ntop(AF_INET, &clientAddr.sin_addr, clientIp, INET_ADDRSTRLEN), ntohs(clientAddr.sin_port), recvBuf);
FD_CLR(sockfd, &readfds);
}
else if (FD_ISSET(sockfd, &readfds) && connected) {
if ((recvBytes = recvfrom(sockfd, recvBuf, BUFFER_SIZE - 1, 0, (struct sockaddr*)&otherClientAddr, &addrLen)) == -1) {
perror("recvfrom");
return 1;
}
if (clientPort == ntohs(otherClientAddr.sin_port)) {
recvBuf[recvBytes] = '\0';
printTime();
printf(", <%s:%d>: %s", inet_ntop(AF_INET, &clientAddr.sin_addr, clientIp, INET_ADDRSTRLEN), ntohs(clientAddr.sin_port), recvBuf);
}
else {
char busymsg[20] = "<<Server Busy>>\n";
if ((sentBytes = sendto(sockfd, busymsg, strlen(busymsg), 0, (struct sockaddr*)&otherClientAddr, addrLen)) == -1) {
fprintf(stderr, "Error %d: ", errno);
perror("sendto");
return 1;
}
}
}
if (FD_ISSET(STDIN_FILENO, &readfds)) {
char testmsg[BUFFER_SIZE];
fgets(testmsg, BUFFER_SIZE, stdin);
if ((sentBytes = sendto(sockfd, testmsg, strlen(testmsg), 0, (struct sockaddr*)&clientAddr, addrLen)) == -1) {
fprintf(stderr, "Error %d: ", errno);
perror("sendto");
return 1;
}
/*printf("Sent %d Bytes\n", sentBytes);*/
FD_CLR(STDIN_FILENO, &readfds);
}
} //end while
close(sockfd);
return 0;
}