-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrec.c
executable file
·303 lines (247 loc) · 8.48 KB
/
rec.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include <error.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include "fft_socket_header.h"
#include "linkedlist.h"
//#define AXIS_START 0
char buff[20];
double *buffer;
int samp_rate;
int CAMERA_WIDTH;
int port_num;
int sockfd;
int n;
int count;
int size;
int length;
int readCount = 0;
char *tempBuf;
int bufSize;
int getData();
struct sockaddr_in serv_addr;
struct fft_header header;
struct fft_header tempHeader;
//variables for multiclient server
int sockfd1, portno1, n1, newsockfd1;
struct sockaddr_in serv_addr1, cli_addr1;
// struct hostent *server;
socklen_t clilen1;
List * list;
void error1(const char *msg)
{
fprintf(stderr, "%s: %s \n", msg, strerror(errno));
exit(1);
}
int getData()
{
int i;
int constant;
// get as many bytes in the socket to fill up the buffer
n = recv(sockfd, tempBuf + readCount, length - readCount, MSG_DONTWAIT);
if(n>0)
readCount += n;
if(readCount == length) //when get enough data
{
// check header constant
constant = ((int*)(tempBuf))[0];
fprintf(stderr, "\nReading header... ");
printf("header.constSync is %X\n", constant);
if(constant != 0xACFDFFBC)
error1("ERROR reading from socket, incorrect header placement\n");
//put data into a buffer
// for( i = 0 ; i < samp_rate; i++)
// buffer[i] = ((double*)(tempBuf + sizeof(struct fft_header)))[i];
//fprintf(stderr, "Reading data... ");
//shift
// call send to reciever here
readCount = 0;
if (list->head != NULL)
writeToClients();
}
return 1;
}
// writes the data in the buffer to all clients connected
// to the server (2nd socket)
int writeToClients(){
// head will never be NULL
Node *ln = list->head;
int x = ln->data;
n = write(x, tempBuf, length);
if (n < 0)
err("ERROR writing to socket");
//written to first client
while(ln->next != NULL){
ln = ln->next;
int x = ln->data;
n = write(x, tempBuf, length);
if (n < 0)
err("ERROR writing to socket");
}
//newsocket for client, tempbuffer, length
//n = write(newsockfd, tempbuffer, length);
//if (n < 0)
// err("ERROR writing to socket");
}
int main(int argc, char *argv[])
{
printf("main method running %d argc", argc);
int i, j, k;
count = 0;
struct hostent *server;
int sync = 0;
//if (argc < 2)
//{
//fprintf(stderr,"ERROR, no host provided\n");
//}
/*Initialize socket*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error1("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
port_num = 51717;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(port_num);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
error1("Error on connect(), exitting");
exit(-1);
}
//Synchronization
fprintf(stderr, "Syncing... ");
n = read(sockfd, &header, sizeof(struct fft_header));
if( n == 0)
{
printf("Sender has closed connection1\n");
exit(0);
} else if( n > 0)
{
printf("Read %d bytes from header, header.constSync is %X\n", n, header.constSync);
}
do
{
for(i = 0; i < sizeof(struct fft_header)-7; i++)
{
if(header.constSync != 0xACFDFFBC)
{
memcpy(&tempHeader, ((char*)(&header))+ 1, sizeof(struct fft_header) - 1 - i);
memcpy(&header, &tempHeader, sizeof(struct fft_header));
}
else
{
sync = 1;
i++;
break;
}
memset(&tempHeader, 0, sizeof(struct fft_header));
printf("%0x\n", header.constSync);
}
if(i > 1)
{
n = read(sockfd, &header + sizeof(struct fft_header) - 1- i,i - 1);
if( n == 0)
{
printf("Sender has closed connection1\n");
exit(0);
}
}
}while(!sync);
if(header.constSync != 0xACFDFFBC)
error1("ERROR reading from socket, incorrect header placement\n");
fprintf(stderr, "Synced... \n");
/*
//First Header
fprintf(stderr, "Reading header... ");
n = read(sockfd, &header, sizeof(struct fft_header));
if (n < 0)
error1("ERROR reading from socket");
else if (n > 0)
{
printf("header.constSync is %X\n", header.constSync);
if(header.constSync != 0xACFDFFBC)
error1("ERROR reading from socket, incorrect header placement\n");
}
else if( n == 0)
{
printf("Sender has closed connection\n");
exit(0);
}
*/
//Initializing structures
samp_rate = header.ptsPerFFT;
buffer = malloc(sizeof(double) * samp_rate);
bufSize = sizeof(double)* samp_rate;
length = (sizeof(struct fft_header) + sizeof(double)* samp_rate);
size = length* 2 + 1;
tempBuf = malloc(length);
//First Data
fprintf(stderr, "Reading data... ");
n = read(sockfd, (char *) buffer, header.ptsPerFFT * sizeof(double));
if (n < 0)
error1("ERROR reading from socket");
else if( n == 0)
{
printf("Sender has closed connection\n");
exit(0);
}
else
{
printf("Read %d bytes data\n", n);
printf("checkpoint 1\n");
}
/*End*/
// socket for server
// socket setup
printf("Setting up server\n");
// if (argc < 2)
// {
// if(debug)fprintf(stderr,"ERROR, no host provided\n");
// exit(1);
// }
portno1 = 51718;
sockfd1 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd1 < 0)
err("ERROR opening socket");
//server = gethostbyname(argv[1]);
bzero((char *) &serv_addr1, sizeof(serv_addr1));
serv_addr1.sin_family = AF_INET;
serv_addr1.sin_addr.s_addr = INADDR_ANY;
serv_addr1.sin_port = htons(portno1);
if (bind(sockfd1, (struct sockaddr *) &serv_addr1, sizeof(serv_addr1)) < 0)
err("ERROR on binding");
printf("Listening for connections..\n");
listen(sockfd1,5);
clilen1 = sizeof(cli_addr1);
int flags = fcntl(sockfd1, F_GETFL, 0);//set the new socket to non blocking
fcntl(sockfd1, F_SETFL, flags | O_NONBLOCK);
list = emptylist(); // initialize the linked list that will contain the client types
while(1){
//listen(sockfd1,5);
//printf("Listening....\n");
clilen1 = sizeof(cli_addr1);
newsockfd1 = accept(sockfd1, (struct sockaddr *) &cli_addr1, &clilen1);
printf(" file descriptor value %d \n",newsockfd1);
if (newsockfd1 < 0)
printf("No connection to client\n");
if(newsockfd1 != EAGAIN && newsockfd1 !=EWOULDBLOCK && newsockfd1 >= 0){
printf("Accepted a connection..\n");
printf(" file descriptor value %d \n",newsockfd1);
add(newsockfd1,list);
printf("Added a client file descriptor value %d \n",newsockfd1);
}
getData();
}
//newsocket for client, tempbuffer, length
//n = write(newsockfd, out, (N/2+1) * sizeof(double));
//loadeImage function
//call shift every 50msec
return 0;
}