-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
201 lines (138 loc) · 4.65 KB
/
Server.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
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
#define _WIN32_WINNT 0x501
#include <iostream>
#include <sstream>
#include <Ws2tcpip.h> //includes winsock header file and other jazz
#pragma comment (lib, "ws2_32.lib");
using namespace std;
int main() {
//initialize winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOK = WSAStartup(ver, &wsData);
if (wsOK != 0)
{
cerr << "can't initialize winsock! Quitting" << endl;
}
//create a socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
cerr << "Can't create a socket! Quitting" << endl;
}
// Bind ip address and port to socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY;
bind(listening, (sockaddr*)&hint, sizeof hint);
//Tell Winsock the socket is for listening
listen(listening, SOMAXCONN);
fd_set master;
FD_ZERO(&master);
FD_SET(listening, &master); //adds listening socket to master set of file descriptors
char host[NI_MAXHOST]; //client's remote name
char service[NI_MAXHOST]; //Service (i.e port) the client is connected on
while (true) {
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXHOST);
fd_set copy = master;
int socketCount = select(0, ©, nullptr, nullptr, nullptr);
//synchronous queue of client connections
for (int num = 0; num < socketCount; num++) {
SOCKET socket = copy.fd_array[num];
if (socket == listening) {
//accept a new connection
SOCKET client = accept(listening, nullptr, nullptr);
if (getnameinfo((sockaddr*)&hint, sizeof hint, host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
cout << "NEW CONNECTION: " << host << " connected on port " << service << "\r\n";
}
//add the new connection to the list of connected clients
FD_SET(client, &master);
//send a welcome message to connected client
string welcomeMsg = "Welcome to the chatRoom \r\n";
send(client, welcomeMsg.c_str(), welcomeMsg.size() + 1, 0);
//TODO: Broadcast new connection
}
else {
char buf[4096];
ZeroMemory(buf, 4096);
//accept a new message if one has been sent
int bytesIn = recv(socket, buf, 4096, 0);
if (bytesIn <= 0) {
//drop the client
closesocket(socket);
FD_CLR(socket, &master);
cout << "SOCKET: " << socket << " has disconnected" << endl;
}
else {
for (int i = 0; i < master.fd_count; ++i) {
SOCKET outSock = master.fd_array[i];
if (outSock != listening and outSock != socket) {
ostringstream ss;
ss << "SOCKET: " << socket << ":" << buf << "\r\n";
string strOut = ss.str();
send(outSock, strOut.c_str(), strOut.size() + 1, 0);
}
}
//Send message to other client, and NOT listening socket
}
}
}
}
WSACleanup();
return 0;
}
/* SINGLE CLIENT ONLY
cout << "Waiting for connection....." << endl;
// Wait for a connection
sockaddr_in client;
int clientSize = sizeof client;
SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
if (clientSocket == INVALID_SOCKET)
{
cerr << "ERROR OPENING CLIENTSOCKET" << endl;
}
char host[NI_MAXHOST]; //client's remote name
char service[NI_MAXHOST]; //Service (i.e port) the client is connected on
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXHOST);
if (getnameinfo((sockaddr*)&client, sizeof client, host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
cout << host << "connected on port " << service << endl;
}
else
{
//inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
auto result = inet_ntoa(client.sin_addr);
//cout << "someone connected on " << result << endl;
cout << host << " connected on port " << ntohs(client.sin_port)
<< endl;
}
// Close listening socket
closesocket(listening);
// While loop: accept and echo message to client
char buf[4096];
for (;;) {
ZeroMemory(buf, 4096);
// Wait for client to send data
int bytesReceived = recv(clientSocket, buf, 4096, 0);
cout << "client says: " << buf << endl;
if (bytesReceived == SOCKET_ERROR)
{
cerr << "Error in recv(). Quitting" << endl;
break;
}
if (bytesReceived == 0) {
cout << "Client disconnected" << endl;
break;
}
// Echo message back to client
send(clientSocket, buf, 4096 + 1, 0);
}
// Close the socket
closesocket(clientSocket);
WSACleanup();
return 0
*/
//clean up WinSock