-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketServer.c
70 lines (54 loc) · 1.58 KB
/
socketServer.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
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <signal.h>
#define SIZE sizeof(struct sockaddr_in)
int main(int argc, char *argv[]) {
int socketFD, newFD, idx = 0;
int n, Diff;
char buf[2];
struct sockaddr_in server = {AF_INET, 1124, INADDR_ANY};
if ((socketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Failed to create server Socket.\n");
exit(1);
}
if (bind(socketFD, (struct sockaddr *)&server, SIZE) == -1) {
perror("Failed to bind.\n");
exit(1);
}
if (listen(socketFD, 6) == -1) {
perror("Listen failed.\n");
exit(1);
}
if ((newFD = accept(socketFD, NULL, NULL)) == -1) {
perror("Failed to accept.\n");
}
printf("Client connected successfully.\n");
sleep(1);
if (fork() == 0) {
//Receiving Integers
for (int i = 0; i < 2; i++) {
if (recv(newFD, &n, sizeof(int), 0) > 0) {
buf[i] = n;
}
}
printf("The two integers received are: %d %d.\n", buf[0], buf[1]);
printf("I will find the difference between them and let the client know.\n");
Diff = buf[0] - buf[1];
//Sending Difference
send(newFD, &Diff, sizeof(int), 0);
printf("Done. Server terminated.\n");
close(newFD);
close(socketFD);
}
return 0;
}