7 DWORD WINAPI
StartServer(LPVOID lpParam
)
9 const TCHAR
* HostIP
= "127.0.0.1";
12 pServices
= (PSERVICES
)lpParam
;
14 SOCKET ListeningSocket
= SetUpListener(HostIP
, htons(pServices
->Port
));
15 if (ListeningSocket
== INVALID_SOCKET
)
17 _tprintf(_T("error setting up socket\n"));
21 _tprintf(_T("%s is waiting for connections on port %d...\n"),
22 pServices
->Name
, pServices
->Port
);
25 AcceptConnections(ListeningSocket
, pServices
->Service
, pServices
->Name
);
26 printf("Acceptor restarting...\n");
29 /* won't see this yet as we kill the service */
30 _tprintf(_T("Detaching Winsock2...\n"));
36 SOCKET
SetUpListener(const char* ServAddr
, int Port
)
41 Sock
= socket(AF_INET
, SOCK_STREAM
, 0);
42 if (Sock
!= INVALID_SOCKET
)
44 Server
.sin_family
= AF_INET
;
45 Server
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
46 Server
.sin_port
= Port
;
47 if (bind(Sock
, (SOCKADDR
*)&Server
, sizeof(SOCKADDR_IN
)) != SOCKET_ERROR
)
49 listen(Sock
, SOMAXCONN
);
53 printf("bind() failed\n");
56 return INVALID_SOCKET
;
62 VOID
AcceptConnections(SOCKET ListeningSocket
,
63 LPTHREAD_START_ROUTINE Service
, TCHAR
*Name
)
67 INT nAddrSize
= sizeof(Client
);
72 Sock
= accept(ListeningSocket
, (SOCKADDR
*)&Client
, &nAddrSize
);
73 if (Sock
!= INVALID_SOCKET
)
75 _tprintf(_T("Accepted connection to %s server from %s:%d\n"),
76 Name
, inet_ntoa(Client
.sin_addr
), ntohs(Client
.sin_port
));
77 _tprintf(_T("Creating new thread for %s\n"), Name
);
78 CreateThread(0, 0, Service
, (void*)Sock
, 0, &ThreadID
);
82 _tprintf(_T("accept() failed\n"));
88 BOOL
ShutdownConnection(SOCKET Sock
, BOOL bRec
)
90 /* Disallow any further data sends. This will tell the other side
91 that we want to go away now. If we skip this step, we don't
92 shut the connection down nicely. */
93 if (shutdown(Sock
, SD_SEND
) == SOCKET_ERROR
)
95 _tprintf(_T("Error in shutdown"));
99 /* Receive any extra data still sitting on the socket. After all
100 data is received, this call will block until the remote host
101 acknowledges the TCP control packet sent by the shutdown above.
102 Then we'll get a 0 back from recv, signalling that the remote
103 host has closed its side of the connection. */
106 char ReadBuffer
[BUF
];
107 int NewBytes
= recv(Sock
, ReadBuffer
, BUF
, 0);
108 if (NewBytes
== SOCKET_ERROR
)
110 else if (NewBytes
!= 0)
111 _tprintf(_T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes
);
114 /* Close the socket. */
115 if (closesocket(Sock
) == SOCKET_ERROR
)