-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
111 lines (97 loc) · 2.48 KB
/
server.cpp
File metadata and controls
111 lines (97 loc) · 2.48 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
110
111
#include <cstdlib>
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <bits/stdc++.h>
using namespace std;
void error(string msg)
{
cout<<msg;
exit(1);
}
int main()
{
int c_sock, s_sock;
char msg[750];
s_sock = socket(AF_INET, SOCK_STREAM, 0);
if (s_sock < 0)
{
error("Error opening socket");
}
struct sockaddr_in server, other;
memset(&server, 0, sizeof(server));
memset(&other, 0, sizeof(other));
server.sin_family = AF_INET;
server.sin_port = htons(9000);
server.sin_addr.s_addr = INADDR_ANY;
socklen_t add;
if (bind(s_sock, (struct sockaddr *)&server, sizeof(server)))
{
error("Binding Failed");
}
listen(s_sock, 10);
add = sizeof(other);
c_sock = accept(s_sock, (struct sockaddr *)&other, &add);
if (c_sock < 0)
{
error("Error on accept");
}
recv(c_sock, msg, sizeof(msg), 0);
int i = 0;
// message is of format numOfBits+Present Characters
// we extract number of bits
while (msg[i] < 65 || msg[i] > 90)
i++;
string msg2(msg);
int istr = i;
string temp = msg2.substr(0, i);
// v is number of bits
int v = stoi(temp);
// no of bits req to represent
int bit = msg2.length() - i;
bit = ceil(log2((float)bit));
int bytes = v / 8;
if (v % 8 != 0)
bytes++;
char *buf = new char[bytes];
recv(c_sock, buf, bytes * sizeof(char), 0);
int s = strlen(buf);
// convert to encoded to original
int *encodedValues = new int[v];
for (int i = 0; i < v; i++)
{
encodedValues[i] = 0;
}
for (int i = 0; i < v; i++)
{
encodedValues[i] = ((buf[i / 8] & (1 << (i % 8))) != 0);
}
string outp = "";
for (i = 0; i < v;)
{
int sum = 0;
for (int j = (bit - 1); j >= 0; j--)
{
sum += (pow(2, j) * encodedValues[i++]);
}
char ch = msg2[istr + sum];
outp += ch;
}
cout << endl;
char outt[outp.length()];
strcpy(outt, outp.c_str());
ofstream fp;
fp.open("received.txt");
fp << outt;
fp.close();
cout<<"Data received from client and saved to file received.txt\n";
close(s_sock);
delete[] encodedValues;
return 0;
}