-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.c
99 lines (71 loc) · 2.05 KB
/
receiver.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAXPENDING 5
#define MAXSIZE 4096
void die(char *error)
{
perror(error);
exit(1);
}
int main(int argc, char **argv)
{
int servSock;
int clientSock;
unsigned short servPort;
unsigned int clientLen;
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clientAddr; /* Client address */
int recvMsgSize;
long key;
long encrypted_buffer[MAXSIZE];
if (argc != 3)
{
fprintf(stderr, "Usage: %s <server_port> "
"<key>\n" , argv[0]);
exit(0);
}
servPort = atoi(argv[1]);
key = atol(argv[2]);
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die("socket() failed");
/* Construct local address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
die("bind() failed");
if (listen(servSock, MAXPENDING) < 0)
die("listen() failed");
clientLen = sizeof(clientAddr);
for (;;)
{
int i;
memset(encrypted_buffer, 0, MAXSIZE*sizeof(long));
if ((clientSock = accept(servSock, (struct sockaddr *) &clientAddr,
&clientLen)) < 0)
die("accept() failed");
if ((recvMsgSize = recv(clientSock, encrypted_buffer, MAXSIZE*sizeof(int), 0)) < 0)
die("recv() failed");
for (i = 0; i <= recvMsgSize; i++) {
encrypted_buffer[i] *= key;
}
send(clientSock, encrypted_buffer, recvMsgSize*sizeof(long), 0);
if ((recvMsgSize = recv(clientSock, encrypted_buffer, MAXSIZE*sizeof(long), 0)) < 0)
die("recv() failed");
for (i = 0; i < recvMsgSize; i++) {
putchar(encrypted_buffer[i] / key);
}
close(clientSock);
}
return 0;
}