-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver4.cc
180 lines (136 loc) · 3.72 KB
/
server4.cc
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
#include <iostream>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sstream>
#include <time.h>
#define HOST_BUFFER 256
#define BUFFER_SIZE 256
#define MESSAGE "message to be sent "
using namespace std;
int main(int argc, char * argv[])
{
//check parameter
if (argc < 1) {
cerr << "Port number missing in the argument" << endl;
exit(0);
}
int portNumber;
istringstream ss(argv[1]);
if (! (ss >> portNumber))
cerr << "Invalid port number" << argv[1] << "\n";
//initialize random generator
srand(time(NULL));
struct sockaddr_in socketInfo;
char host[HOST_BUFFER + 1]; // hostname of this computuer
struct hostent *hPtr;
int socketHandle;
// clear struct memory
bzero(&socketInfo, sizeof(sockaddr_in));
//get system info
/*
gethostname(host, HOST_BUFFER); // name of this computer
if ((hPtr = gethostbyname(host)) == NULL) {
cerr << "System hostname misconfigured." << endl;
exit(EXIT_FAILURE);
}
*/
// create socket
if ((socketHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
cout << "cannot create a socket" << endl;
close(socketHandle);
exit(EXIT_FAILURE);
}
// initialize socket data
socketInfo.sin_family = AF_INET;
socketInfo.sin_addr.s_addr = htonl(INADDR_ANY); //use any addr available
socketInfo.sin_port = htons(portNumber);
//loop through all the results and bind to the first we can
//bind socket to local socket addr
if (bind(socketHandle, (struct sockaddr *) &socketInfo, sizeof(socketInfo)) < 0) {
close(socketHandle);
perror("bind failure");
exit(EXIT_FAILURE);
}
// freeaddrinfo(servinfo);
//int rc = 0; // number of bytes read
char buf[BUFFER_SIZE];
memset(&socketInfo, '0', sizeof(socketInfo));
memset(buf, '0', sizeof(buf));
int connfd; //?
int done = 0;
char filename[10];
strcpy(filename, "test1"); //10MB
strcat(filename, ".txt");
int randtime;
int totaltime = 0;
int blocksent = 0; // counter for data block
while (true) {
//connfd = accept(socketHandle, (struct sockaddr *) NULL, NULL);
FILE * f = fopen(filename, "r");
if (f == NULL) {
cout << "cannot open file: " << filename << endl;
exit(1);
}
bzero(f, BUFFER_SIZE);
cout << "trying to send " << endl;
//char *message = "message to send";
while (true) {
unsigned char buffer[BUFFER_SIZE] = {0};
//int nread = fread(buffer, 1, BUFFER_SIZE, f);
int nread = send(socketHandle, MESSAGE, strlen(MESSAGE) + 1, 0);
cout << "Bytes read: " << nread << endl;
//send data
if (nread > 0) {
cout << "sending..." << endl;
//write(connfd, buffer, nread);
}
blocksent += 1;
/*
// error, or reached end of file
if (nread < BUFFER_SIZE) {
if (feof(f)) {
cout << "end of file" << endl;
done = 1;
break;
}
if (ferror(f)) {
cout << "ok" << endl;
cout << "error reading" << endl;
break;
}
//break;
}
*/
// create random delay
randtime = rand() % 10; // 0~9
cout << "delaying " << randtime << " seconds" << endl;
sleep(randtime);
cout << "done! moving on " << endl;
totaltime += randtime;
}
//close (connfd);
if (done)
break;
//sleep(1);
}
close (socketHandle);
/*
FILE *f;
f = fopen("add.txt","r");
if (f == NULL) {
cout << "file open error" << endl; //error
exit(1);
}
fscanf(f,"%s",buf); //segmentation error
write(socketHandle, buf, 256);
cout << "the file was sent successfully" << endl;
bzero(buf, 256);
close(socketHandle);
*/
return 0;
}