-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseedInfoServer.cpp
117 lines (102 loc) · 3.46 KB
/
seedInfoServer.cpp
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
#include <bits/stdc++.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <ctime>
#include <fstream>
#include <google/protobuf/util/time_util.h>
#include <google/protobuf/text_format.h>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <google/protobuf/message.h>
#include "logging.h"
#include "seedInfo.pb.h"
using namespace std;
#define MAX_BUFFER 3000
#define MY_PORT_NUM 54145
#define MAX_BACKLOG_REQUEST 100
int main () {
int listenSock, connSock, ret, in, flags, i;
struct sockaddr_in servaddr;
struct sctp_initmsg initmsg;
struct sctp_event_subscribe events;
struct sctp_sndrcvinfo sndrcvinfo;
char buffer[MAX_BUFFER + 1];
int datalen = 0;
listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
if(listenSock == -1) {
higLog("%s","Failed to create socket");
exit(1);
}
int opt = SO_REUSEADDR;
if (setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
higLog("%s","setsockopt() failed");
exit(EXIT_FAILURE);
}
bzero ((void *) &servaddr, sizeof (servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("10.129.135.192");
servaddr.sin_port = htons (MY_PORT_NUM);
ret = bind (listenSock, (struct sockaddr *) &servaddr, sizeof (servaddr));
if(ret == -1 ) {
higLog("%s","bind() failed");
close(listenSock);
exit(1);
}
/* Specify that a maximum of 5 streams will be available per socket */
memset (&initmsg, 0, sizeof (initmsg));
initmsg.sinit_num_ostreams = 5;
initmsg.sinit_max_instreams = 5;
initmsg.sinit_max_attempts = 4;
ret = setsockopt (listenSock, IPPROTO_SCTP, SCTP_INITMSG,
&initmsg, sizeof (initmsg));
if(ret == -1 ) {
higLog("%s","setsockopt() failed while setting no of streams");
close(listenSock);
exit(1);
}
ret = listen(listenSock, MAX_BACKLOG_REQUEST);
if(ret == -1 ) {
higLog("listen() failed \n");
close(listenSock);
exit(1);
}
// create the List of seedNode < Name , IPs >
SeedInfoServer::seedInfo SeedInfoList;
auto& Map = *SeedInfoList.mutable_seedlist();
Map["SeedServer1"] = "10.129.135.161";
while(true) {
char buffer[MAX_BUFFER + 1];
int len;
bzero (buffer, MAX_BUFFER + 1);
printf("Awaiting a new connection\n");
connSock = accept (listenSock, (struct sockaddr *) NULL, (socklen_t *) NULL);
if (connSock == -1) {
printf("accept() failed\n");
perror("accept()");
close(connSock);
continue;
} else {
midLog("%s","New client connected....\n");
string protocol_buffer = SeedInfoList.SerializeAsString();
sprintf(buffer, "%s", protocol_buffer.c_str());
datalen = strlen(buffer);
ret = sendto(connSock, buffer, (size_t) datalen, 0,NULL,0);
if(ret == -1) {
higLog("%s"," error in sctp_sendmsg");
}else {
cout <<"Send the following list to the client"<<endl;
/* TODO get the IP address of the client also
from the accept call and print it here
*/
cout << SeedInfoList.DebugString() << endl;
}
}
close(connSock);
}
return 0;
}