-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_udp.cpp
More file actions
67 lines (53 loc) · 1.89 KB
/
server_udp.cpp
File metadata and controls
67 lines (53 loc) · 1.89 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
#include "common.h"
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 4)
{
cout << "Expected execution : ./server port packetSize sleepTime" << endl;
return 0;
}
int count = 0,received,sent;
int portNumber = atoi(argv[1]),sleepTime = atoi(argv[3]),sckt,bufferSize = atoi(argv[2]);
char buffer[bufferSize];
uint i;
char message[bufferSize];
struct sockaddr_in server_addr,client_addr;
string dataToSend;
socklen_t size = sizeof(server_addr);
sckt = socket(AF_INET, SOCK_DGRAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNumber);
if ((bind(sckt, (struct sockaddr*)&server_addr,size)) < 0)
{
perror("Error binding the connection !!");
return 0;
}
cout << "Waiting for connections..." << endl;
while(count < 30)
{
cout <<"--------------------------------------------------------------------------------"<<endl;
memset(message,0,bufferSize);
received = recvfrom(sckt , message , bufferSize , 0 , (struct sockaddr *) &client_addr,&size);
if(received <0)
{
perror("Error receiving data !!");
return 0;
}
cout << "Client :" <<message<< endl;
usleep(sleepTime*1000);
dataToSend = "Packet " + to_string(count) + " received.";
cout << "Received packet : "<<count<<", acknowledging client..."<< endl ;
memset(buffer,0,bufferSize);
for(i=0;i<dataToSend.size();i++){
buffer[i]=dataToSend[i];
}
sent = sendto(sckt,buffer,bufferSize,0,(struct sockaddr *) &client_addr,sizeof(client_addr));
cout << "Data sent : " << sent <<" bytes, Data received : " << received <<" bytes." << endl;
count++;
cout <<"--------------------------------------------------------------------------------"<<endl;
}
close(sckt);
return 0;
}