-
Notifications
You must be signed in to change notification settings - Fork 8
/
pthread_server.c
100 lines (80 loc) · 2.17 KB
/
pthread_server.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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
struct thread_data {
int sock;
};
void *thread_handle(void *data)
{
struct thread_data *thread_data = data;
int ret;
while (1) {
char rxbuf[1024];
ret = recv(thread_data->sock, rxbuf, sizeof(rxbuf), 0);
if (ret <= 0) {
fprintf(stderr, "failed to recv %s\n", strerror(errno));
break;
}
printf("data %s from client %d\n", (char *)rxbuf, thread_data->sock);
}
}
int main(int argc, char **argv)
{
struct sockaddr_in serv;
int sock;
int csock;
struct thread_data data;
pthread_t tid;
int ret;
if (argc != 3) {
fprintf(stderr, "<%s> <ip> <port>\n", argv[0]);
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr, "failed to socket open %s\n", strerror(errno));
return -1;
}
memset(&serv, 0, sizeof(serv));
serv.sin_addr.s_addr = inet_addr(argv[1]);
serv.sin_port = htons(atoi(argv[2]));
serv.sin_family = AF_INET;
ret = bind(sock, (struct sockaddr *)&serv, sizeof(serv));
if (ret < 0) {
fprintf(stderr, "failed to bind %s\n", strerror(errno));
return -1;
}
ret = listen(sock, 4);
if (ret < 0) {
fprintf(stderr, "failed to listen %s\n", strerror(errno));
return -1;
}
while (1) {
csock = accept(sock, NULL, NULL);
if (csock < 0) {
fprintf(stderr, "failed to accept %s\n", strerror(errno));
return -1;
}
struct thread_data *thr;
thr = calloc(1, sizeof(struct thread_data));
if (!thr) {
fprintf(stderr, "failed to allocate %s\n", strerror(errno));
return -1;
}
thr->sock = csock;
ret = pthread_create(&tid, NULL, thread_handle, thr);
if (ret < 0) {
fprintf(stderr, "failed to pthrad_create %s\n", strerror(errno));
return -1;
}
}
return 0;
}