-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServerFork.c
112 lines (88 loc) · 2.71 KB
/
TCPServerFork.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int writeFile(int socketDescriptor);
void DieWithError(char *errorMessage);
int main(int argc, char *argv[]){
int serverSocket, clientSocket, addr_size;
unsigned int clientLen;
pid_t processID;
struct sockaddr_in server, client;
unsigned int childProcCount = 0;
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
DieWithError(" socket () failed") ;
printf("[+]Server socket created successfully.\n");
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
if (bind(serverSocket, (struct sockaddr *)&server, sizeof(server)) < 0)
DieWithError ("bind () failed");
printf("[+]Binding successfull.\n");
if (listen(serverSocket, 5) < 0)
DieWithError("listen() failed") ;
printf("[+]Listening....\n");
addr_size = sizeof(struct sockaddr_in);
for (;;){
if ((clientSocket = accept(serverSocket, (struct sockaddr *) &client, (socklen_t*)&addr_size)) < 0)
DieWithError( "accept () failed");
printf("Handling client %s\n", inet_ntoa(client.sin_addr));
if ((processID = fork()) < 0)
DieWithError("fork() failed");
else if (processID == 0){
close(serverSocket);
exit(writeFile(clientSocket));
}
printf("with child process' %d\n", (int) processID);
close(clientSocket);
childProcCount ++;
while (childProcCount)
{
processID = waitpid((pid_t)-1, NULL, WNOHANG);
if (processID < 0)
DieWithError("waitpid() failed");
else if (processID == 0)
break;
else
childProcCount--;
}
}
exit(0);
}
void DieWithError(char *errorMessage){
perror (errorMessage);
exit(1);
}
int writeFile(int socketDescriptor){
int bytesReceived = 0;
FILE *uploadedFile;
char buffer[512];
if (recv(socketDescriptor, buffer, sizeof(buffer), 0) < 0)
DieWithError ("Error receiving name");
int fileNameSize = buffer[0];
char fileName[fileNameSize];
strcpy(fileName, buffer+1);
uploadedFile = fopen(fileName, "wb"); //overwrite if exists and create if not
if(NULL == uploadedFile){
DieWithError ("Error creating file");
}
printf("Created filename: %s\n", fileName);
puts("waiting\n");
sleep(10);
while((bytesReceived=recv(socketDescriptor, buffer, sizeof(buffer),0)) > 0){
printf("Received %d bytes\n", bytesReceived);
fwrite(buffer, 1, bytesReceived, uploadedFile);
}
if(bytesReceived == 0) {
puts("Client disconnected");
} else if(bytesReceived == -1) {
DieWithError("Error recieving file");
}
close(socketDescriptor);
fclose(uploadedFile);
return 0;
}