-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerTCP.cpp
158 lines (114 loc) · 3.72 KB
/
ServerTCP.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
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <bits/stdc++.h>
using namespace std;
vector<int> socketid;
vector<int> serversocket;
int main(int argc, char *argv[]){
if(argc < 2){
cerr<< "Missing port number.....\n";
exit(0);
}
char msg[1500];
for(int i=1; i<argc; i++){
int port = atoi(argv[i]);
//define sockaddr_in for server socket
sockaddr_in serverSocket;
serverSocket.sin_family = AF_INET;
serverSocket.sin_port = htons(port);
serverSocket.sin_addr.s_addr = htonl(INADDR_ANY);
//socket() initializing
int serverSocketID = socket(AF_INET, SOCK_STREAM, 0);
if(serverSocketID < 0){
cerr<< "Not successfull socket...\n";
exit(0);
}
serversocket.push_back(serverSocketID);
//bind socket to the port
int bindPort = bind(serverSocketID, (struct sockaddr*) &serverSocket, sizeof(serverSocket));
if(bindPort < 0){
cerr<< "Not successfull bind...\n";
exit(0);
}
cout<<"Waiting for client "<<port<<" to connect...\n";
//listen to socket
listen(serverSocketID, 1);
//define a new socket for connection
sockaddr_in newSocket;
socklen_t newsocketLen = sizeof(newSocket);
//accept request from client
int newSocketID = accept(serverSocketID, (sockaddr *) &newSocket, &newsocketLen);
if(newSocketID < 0){
cerr<< "Not successfull accept().....\n";
exit(0);
}
socketid.push_back(newSocketID);
cout<< "client "<<port<<" connected successfully...\n";
}
/*int port = atoi(argv[1]);
char msg[1500];
//define sockaddr_in for server socket
sockaddr_in serverSocket;
serverSocket.sin_family = AF_INET;
serverSocket.sin_port = htons(port);
serverSocket.sin_addr.s_addr = htonl(INADDR_ANY);
//socket() initializing
int serverSocketID = socket(AF_INET, SOCK_STREAM, 0);
if(serverSocketID < 0){
cerr<< "Not successfull socket...\n";
exit(0);
}
//bind socket to the port
int bindPort = bind(serverSocketID, (struct sockaddr*) &serverSocket, sizeof(serverSocket));
if(bindPort < 0){
cerr<< "Not successfull bind...\n";
exit(0);
}
cout<<"Waiting for client to connect...\n";
//listen to socket
listen(serverSocketID, 1);
//define a new socket for connection
sockaddr_in newSocket;
socklen_t newsocketLen = sizeof(newSocket);
//accept request from client
int newSocketID = accept(serverSocketID, (sockaddr *) &newSocket, &newsocketLen);
if(newSocketID < 0){
cerr<< "Not successfull accept().....\n";
exit(0);
}
cout<< "client connected successfully...\n";*/
//send and receive data
while(1){
cout<<"Waiting for message from client...\n";
for(int i=0; i<socketid.size(); i++){
recv(socketid[i], (char*)&msg, sizeof(msg), 0);
if(!strcmp(msg, "exit")){
cout<<"Session terminated...\n";
break;
}
cout<<msg<<endl;
}
string data;
cout<<"Server: ";
getline(cin, data);
strcpy(msg, data.c_str());
if(data == "exit"){
cout<<"Session terminated...\n";
break;
}
for(int i=0; i<socketid.size(); i++)
send(socketid[i], (char*) &msg, sizeof(msg), 0);
}
//close the socket
for(int i=0; i<socketid.size(); i++)
close(socketid[i]);
for(int i=0; i<serversocket.size(); i++)
close(serversocket[i]);
return 0;
}