-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
162 lines (148 loc) · 3.76 KB
/
client.cpp
File metadata and controls
162 lines (148 loc) · 3.76 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//./client localhost port#
//./server port#
#include <vector>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "pthread.h"
#include <map>
using namespace std;
struct sockaddr_in serv_addr; //Declare this globally so we dont have to pass it through threads
struct hostent *server;
struct datas {
char name;
double prob;
double fx;
double prevfx; // prev fx + 1/2 prob
string fxbin;
};
void* clienthread(void* args)
{
int n, size;
struct datas &d = *(struct datas *)args;
int network_socket;
// Create a stream socket
network_socket = socket(AF_INET,SOCK_STREAM, 0);
// Initiate a socket connection
int connection_status = connect(network_socket,
(struct sockaddr*)&serv_addr,
sizeof(serv_addr));
// Check for connection error
if (connection_status < 0) {
puts("Error\n");
return 0;
}
// Send data to the socket
n = write(network_socket, &d.prob, sizeof(double));
if (n < 0)
{
cerr << "ERROR writing to socket";
exit(1);
}
n = write(network_socket, &d.prevfx, sizeof(double));
if (n < 0)
{
cerr << "ERROR writing to socket";
exit(1);
}
n = write(network_socket, &d.name, 1);
if (n < 0)
{
cerr << "ERROR writing to socket";
exit(1);
}
//Receive binary code from server
int nsize; //Size of string
n = read(network_socket, &nsize, sizeof(int));
if (n < 0)
{
cerr << "ERROR reading from socket";
exit(1);
}
char *buffer = new char[nsize + 1];
bzero(buffer, nsize + 1); //Initialize buffer byte size to 0
n = read(network_socket, buffer, nsize);
d.fxbin = buffer;
delete [] buffer;
// Close the connection
close(network_socket);
pthread_exit(NULL);
return 0;
}
int main(int argc, char *argv[])
{
int portno, n;
if (argc < 3)
{
cerr << "usage " << argv[0] << "hostname port\n";
exit(0);
}
portno = atoi(argv[2]);
//portno = 1235;
server = gethostbyname(argv[1]);
if (server == NULL)
{
cerr << "ERROR, no such host\n";
exit(0);
}
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
//Probability calculation
string s;
s = "AAAAABBBBBCCCCCDDDDDAAAAAAAAAABBBBBAAAAA";
string temp;
vector <datas> data;
int count = 0; //number of threads needed
//cin >> s; //Get input from user
map<char, double >m;
map<char, double >::iterator itr;
for (int i = 0; i < s.length(); i++)
{
m[s[i]]++; //Calcuate frequency
}
for (itr = m.begin(); itr != m.end(); itr++)
{
double p = itr->second/s.length(); //Calculate prob
data.push_back({itr->first, p}); //Push the pair data into struct
count++;
}
//Calculate Fx
for (int i = 0; i < count; i++)
{
if (i == 0)
{
data[i].fx = 0 + data[i].prob;
data[i].prevfx = 0;
}
else
{
data[i].fx = data[i - 1].fx + data[i].prob;
data[i].prevfx = data[i - 1].fx;
}
}
//Threads creation and join
pthread_t thread_id[count];
for (int i = 0; i < count; i++)
{
pthread_create(&thread_id[i], NULL, clienthread, &data[i]);
}
for (int j = 0; j < count; j++)
{
pthread_join(thread_id[j], NULL);
}
cout << "SHANNON-FANO-ELIAS Codes:\n";
for (int i = 0; i < count; i++)
{
cout << "Symbol " << data[i].name << ", Code: " << data[i].fxbin << endl;
}
return 0;
}