-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathserver.c
65 lines (52 loc) · 1.57 KB
/
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
/**********************************************
* Author: ideawu(www.ideawu.net)
* Date: 2007-04
* File: server.c
*********************************************/
#include "tftpx.h"
#include "work_thread.h"
void config(){
conf_document_root = ".";//"/home/ideawu/books";
}
int main (int argc, char **argv){
int sock;
int done = 0; // Server exit.
socklen_t addr_len;
pthread_t t_id;
struct sockaddr_in server;
unsigned short port = SERVER_PORT;
printf("Usage: %s [port]\n", argv[0]);
printf(" port - default 10220\n");
if(argc > 1){
port = (unsigned short)atoi(argv[1]);
}
config();
if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
printf("Server socket could not be created.\n");
return 0;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0){
printf("Server bind failed. Server already running? Proper permissions?\n");
return 2;
}
printf("Server started at 0.0.0.0:%d.\n", port);
struct tftpx_request *request;
addr_len = sizeof(struct sockaddr_in);
while(!done){
request = (struct tftpx_request *)malloc(sizeof(struct tftpx_request));
memset(request, 0, sizeof(struct tftpx_request));
request->size = recvfrom(
sock, &(request->packet), MAX_REQUEST_SIZE, 0,
(struct sockaddr *) &(request->client),
&addr_len);
request->packet.cmd = ntohs(request->packet.cmd);
printf("Receive request.\n");
if(pthread_create(&t_id, NULL, work_thread, request) != 0){
printf("Can't create thread.\n");
}
}
return 0;
}