Repository navigation
Expand file tree
/
Copy pathserver-test.c
More file actions
109 lines (92 loc) · 2.56 KB
/
Copy pathserver-test.c
File metadata and controls
109 lines (92 loc) · 2.56 KB
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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <signal.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define SERVER_PORT 5000
#define BUFLEN 300
#define FILENAME "/tmp/infer_img"
#define PY_SERVER_PID_FILE "/tmp/py_server_pid"
#define PYTHONCMD "python simplep.py"
void eventhandler() {
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]){
int sock, clientsock;
struct sockaddr_in cliAddr,srvAddr;
//register signal event
signal(SIGINT, eventhandler);
/*create socket*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("fail to create a socket");
exit(EXIT_FAILURE);
}
srvAddr.sin_family = AF_INET;
srvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
srvAddr.sin_port = htons(SERVER_PORT);
//bind server address
if(bind(sock, (struct sockaddr *)&srvAddr, sizeof(srvAddr)) < 0 ) {
perror("fail to address");
exit(EXIT_FAILURE);
}
listen(sock, 50);
while(1) {
//start to read data from clients
printf("%s: start to wait connetions from client on TCP port %u\n", argv[0] , SERVER_PORT);
int clilen;
int status,pid;
clilen = sizeof(cliAddr);
clientsock = accept( sock, (struct sockaddr *)&cliAddr, &clilen);
printf("A client connected.\n");
if(clientsock < 0) {
perror("fail to address");
exit(EXIT_FAILURE);
}
//fork a child process to do the task; parent will accept another connection.
if( (pid=fork()) == 0) {
//read data
int n;
char buf[BUFLEN];
unlink(FILENAME);
FILE *fp;
fp = fopen(FILENAME, "w+");
recv(clientsock, buf, BUFLEN, 0);
if(strncmp(buf,"IMG_BEGIN", strlen("IMG_BEGIN")) != 0) {
printf("error img header\n");
exit(EXIT_FAILURE);
}
printf("receiving image data.%s\n",buf);
bzero(buf,sizeof(buf));
recv(clientsock, buf, BUFLEN, 0);
printf("receiving image data.%s\n",buf);
// while(1) {
// n = recv(clientsock, buf, BUFLEN, 0);
// if(strncmp(buf,"IMG_END",strlen("IMG_END")) == 0) break;
// printf("\n%s,%d",buf,strncmp(buf,"IMG_END",strlen("IMG_END")));
// fwrite(buf, 1, n, fp);
// bzero(buf,sizeof(buf));
// }
fclose(fp);
printf("finished!\n");
//save pidinfo
fp = fopen("/tmp/cli_pid","w");
bzero(buf,sizeof(buf));
sprintf(buf,"%d",getpid());
fwrite(buf, strlen(buf), 1, fp);
fclose(fp);
printf("session is finished!\n");
//close socket
close(clientsock);
exit(EXIT_SUCCESS);
}
//parent process.
//waitpid( pid, &status, WNOHANG);
waitpid( -1, &status, WNOHANG); //wait for any child
}
}