1: #include <windows.h>
2: #include <winsock2.h>
3: #include <ws2tcpip.h>
4: #include <stdlib.h>
5: #include <stdio.h>
6:
7: // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
8: #define DEFAULT_BUFLEN 512
9: #define DEFAULT_PORT "6174"
10:
11: int __cdecl main(void)
12: {
13: WSADATA wsaData;
14: SOCKET ListenSocket = INVALID_SOCKET,
15: ClientSocket = INVALID_SOCKET;
16: struct addrinfo *result = NULL,
17: hints = {0};
18: CHAR recvbuf[DEFAULT_BUFLEN] = {0};
19: int iResult = 0;
20: int recvbuflen = DEFAULT_BUFLEN;
21:
22: // Initialize Winsock
23: iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
24: if (iResult != 0) {
25: wprintf(L"WSAStartup failed: %d\n", iResult);
26: return 1;
27: }
28:
29: ZeroMemory(&hints, sizeof(hints));
30: hints.ai_family = AF_INET;
31: hints.ai_socktype = SOCK_STREAM;
32: hints.ai_protocol = IPPROTO_TCP;
33: hints.ai_flags = AI_PASSIVE;
34:
35: // Resolve the server address and port
36: iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
37: if ( iResult != 0 ) {
38: wprintf(L"getaddrinfo failed: %d\n", iResult);
39: WSACleanup();
40: return 1;
41: }
42:
43: // Create a SOCKET for connecting to server
44: ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
45: if (ListenSocket == INVALID_SOCKET) {
46: wprintf(L"socket failed: %ld\n", WSAGetLastError());
47: freeaddrinfo(result);
48: WSACleanup();
49: return 1;
50: }
51:
52: // Setup the TCP listening socket
53: iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
54: if (iResult == SOCKET_ERROR) {
55: wprintf(L"bind failed: %d\n", WSAGetLastError());
56: freeaddrinfo(result);
57: closesocket(ListenSocket);
58: WSACleanup();
59: return 1;
60: }
61:
62: freeaddrinfo(result);
63:
64: wprintf(L"Waiting for message from host...\n");
65:
66: iResult = listen(ListenSocket, SOMAXCONN);
67: if (iResult == SOCKET_ERROR) {
68: wprintf(L"listen failed: %d\n", WSAGetLastError());
69: closesocket(ListenSocket);
70: WSACleanup();
71: return 1;
72: }
73:
74: // Accept a client socket
75: ClientSocket = accept(ListenSocket, NULL, NULL);
76: if (ClientSocket == INVALID_SOCKET) {
77: wprintf(L"accept failed: %d\n", WSAGetLastError());
78: closesocket(ListenSocket);
79: WSACleanup();
80: return 1;
81: }
82:
83: // No longer need server socket
84: closesocket(ListenSocket);
85:
86: iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
87: if (iResult > 0) {
88: wprintf(L"Bytes received: %d\n", iResult);
89: wprintf(L"Message received from host is '%s'\n", recvbuf);
90: }
91: else if (iResult == 0)
92: wprintf(L"Connection closing...\n");
93: else {
94: wprintf(L"recv failed: %d\n", WSAGetLastError());
95: closesocket(ClientSocket);
96: WSACleanup();
97: return 1;
98: }
99:
100:
101: // shutdown the connection since we're done
102: iResult = shutdown(ClientSocket, SD_SEND);
103: if (iResult == SOCKET_ERROR) {
104: wprintf(L"shutdown failed: %d\n", WSAGetLastError());
105: closesocket(ClientSocket);
106: WSACleanup();
107: return 1;
108: }
109:
110: // cleanup
111: closesocket(ClientSocket);
112: WSACleanup();
113:
114: return 0;
115: }