-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
53 lines (41 loc) · 1021 Bytes
/
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
/* server.c
General TCP socket handling, not instrument specific
*/
#include "server.h"
#define DEBUG
int create_tcp_socket(int port)
{
#ifdef DEBUG
printf("create_tcp_socket()\n");
#endif
struct sockaddr_in addr;
int sd;
/*--- create socket ---*/
sd = socket(PF_INET, SOCK_STREAM, 0);
if ( sd < 0 )
perror("socket");
/*--- bind port/address to socket ---*/
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY; /* any interface */
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
perror("bind");
/*--- make into listener with 10 slots ---*/
if ( listen(sd, 10) != 0 )
perror("listen");
return sd;
}
int wait_for_connection(int sd, void *handler)
{
FILE *fp;
pthread_t child;
handler_args *args;
sd = accept(sd, 0, 0);
args = malloc(sizeof(*args));
args->sd = sd;
args->config = kt15_config;
pthread_create(&child, 0, handler, (void *)args);
pthread_detach(child);
return 0;
}