Create server and wait for a client to connect.
34{
35 WSADATA wsaData;
36 int iResult;
37
38 SOCKET ListenSocket = INVALID_SOCKET;
39 SOCKET ClientSocket = INVALID_SOCKET;
40
42 struct addrinfo hints;
43
44
45
46
47 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
48 if (iResult != 0)
49 {
51 return 1;
52 }
53
54 ZeroMemory(&hints, sizeof(hints));
55 hints.ai_family = AF_INET;
56 hints.ai_socktype = SOCK_STREAM;
57 hints.ai_protocol = IPPROTO_TCP;
58 hints.ai_flags = AI_PASSIVE;
59
60
61
62
63 iResult = getaddrinfo(NULL, Port, &hints, &result);
64 if (iResult != 0)
65 {
67 WSACleanup();
68 return 1;
69 }
70
71
72
73
74 ListenSocket =
76 if (ListenSocket == INVALID_SOCKET)
77 {
78 ShowMessages(
"socket failed with error: %ld\n", WSAGetLastError());
79 freeaddrinfo(result);
80 WSACleanup();
81 return 1;
82 }
83
84
85
86
87 iResult = ::bind(ListenSocket,
result->ai_addr, (
int)
result->ai_addrlen);
88 if (iResult == SOCKET_ERROR)
89 {
90 ShowMessages(
"err, bind failed (%x)\n", WSAGetLastError());
91 freeaddrinfo(result);
92 closesocket(ListenSocket);
93 WSACleanup();
94 return 1;
95 }
96
97 freeaddrinfo(result);
98
99 iResult = listen(ListenSocket, SOMAXCONN);
100 if (iResult == SOCKET_ERROR)
101 {
102 ShowMessages(
"err, listen failed (%x)\n", WSAGetLastError());
103 closesocket(ListenSocket);
104 WSACleanup();
105 return 1;
106 }
107
108
109
110
111 sockaddr_in name = {0};
112 int addrlen = sizeof(name);
113
114 ClientSocket = accept(ListenSocket, (struct sockaddr *)&name, &addrlen);
115
116 if (ClientSocket == INVALID_SOCKET)
117 {
118 ShowMessages(
"err, accept failed (%x)\n", WSAGetLastError());
119 closesocket(ListenSocket);
120 WSACleanup();
121 return 1;
122 }
123
124
125
126
127 ShowMessages(
"connected to : %s:%d\n", inet_ntoa(name.sin_addr), ntohs(name.sin_port));
128
129
130
131
132 *ClientSocketArg = ClientSocket;
133 *ListenSocketArg = ListenSocket;
134
135 return 0;
136}
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
result
Definition modelsim.py:117
NULL()
Definition test-case-generator.py:530