-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
76 lines (56 loc) · 1.72 KB
/
server.cpp
File metadata and controls
76 lines (56 loc) · 1.72 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
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <ws2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
string o = ":(";
int main() {
//init
WSADATA wsa;
int wsaOk = WSAStartup(MAKEWORD(2, 2), &wsa);
if (wsaOk != 0) { cerr << ":(" << endl; return -1; }
//create socket
SOCKET listens = socket(AF_INET, SOCK_STREAM, 0);
if (listens == INVALID_SOCKET) { cerr << o << endl; return -1; }
//bind socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY;
bind(listens, (sockaddr*)&hint, sizeof(hint));
//listen
listen(listens, SOMAXCONN);
//wait to connect
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSock = accept(listens, (sockaddr*)&client, &clientSize);
if (clientSock == INVALID_SOCKET) { cerr << o << endl; return -1; }
char host[NI_MAXHOST];
char service[NI_MAXSERV];
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);
if (getnameinfo((sockaddr*)&client, clientSize, host, NI_MAXHOST, service, NI_MAXSERV, 0))
{
cout << host << " port " << service << endl;
}
else {
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << "on port " << ntohs(client.sin_port) << endl;
}
closesocket(listens);
//talk
char buf[4096];
while (true) {
ZeroMemory(buf, 4096);
int bytesrecv = recv(clientSock, buf, 4096, 0);
if (bytesrecv == SOCKET_ERROR) { cerr << o << endl; break; }
if (bytesrecv == 0) { cerr << "nooooooooo" << endl; break; }
cout << string(buf, 0, bytesrecv);
send(clientSock, buf, bytesrecv + 1, 0);
}
//close socket
closesocket(clientSock);
//cleanup
WSACleanup();
system("pause>nul");
return 0;
}