-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-client.c
56 lines (44 loc) · 1.01 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
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address = {
AF_INET,
htons(9999),
0
};
connect(sockfd, &address, sizeof(address));
//stdin - 0
struct pollfd fds[2] = {
{
0,
POLLIN,
0
},
{
sockfd,
POLLIN,
0
}
};
for (;;) {
char buffer[256] = { 0 };
poll(fds, 2, 50000);
// Send to the server.
if (fds[0].revents & POLLIN) {
read(0, buffer, 255);
send(sockfd, buffer, 255, 0);
}
// If there is something to be read in from the client.
else if (fds[1].revents & POLLIN ) {
if (recv(sockfd, buffer, 255, 0) == 0) {
return 0;
}
printf("%s\n", buffer);
}
}
return 0;
}