我正在尝试使用两个线程来运行服务器和客户端。
我使用信号量来阻止客户端线程在设置服务器之前做太多(ListenSocket
等。)。
当服务器足够远时,我使用ReleaseSemaphore()
但它返回Invalid Handle
错误。
我已经读过有很多可能的问题可能是这个错误的根源。
确切的行是if (ReleaseSemaphore(semaphore, 1, NULL)){}
,可以在Server.cpp
文件的55%左右找到
Header.h :
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
unsigned int __stdcall servermain(void*);
unsigned int __stdcall clientmain(void*);
static HANDLE semaphore;
Source.cpp :
#include "Header.h"
int main(int argc, char* argv[])
{
HANDLE serverHandle, clientHandle;
semaphore = CreateSemaphore(
NULL,
0, //initial count
1, //max count
NULL);
if (semaphore == NULL){
printf("CreateSemaphore error: %d\n", GetLastError());
return 1;
}
/*call to the methods in the Client and Server files*/
serverHandle = (HANDLE)_beginthreadex(0, 0, &servermain, 0, 0, 0);
clientHandle = (HANDLE)_beginthreadex(0, 0, &clientmain, 0, 0, 0);
WaitForSingleObject(serverHandle, INFINITE);
WaitForSingleObject(clientHandle, INFINITE);
CloseHandle(serverHandle);
CloseHandle(clientHandle);
CloseHandle(semaphore);
return 0;
}
Server.cpp :
#include "Header.h"
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
unsigned int __stdcall servermain(void* data)
{
printf("SERVER STARTED. Thread: %i\n", GetCurrentThreadId());
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo
*result = NULL,
hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //initiates the use of the Winsock DLL
if (iResult != 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
//////////////////////////////////////
// Creating a Socket for the Server //
//////////////////////////////////////
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for the server to listen for client connections
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("%i Server:\t", GetCurrentThreadId());
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
//////////////////////
// Binding a Socket //
//////////////////////
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
char answer = 'y'; // char answer[8]; // char *answer = "";
do{
printf("%i Server:\t", GetCurrentThreadId());
printf("Would you like to wait for client?(Y/N)\t");
answer = getchar(); // fgets(answer, 1, stdin);
printf("%c\n", answer);
} while (answer != 'y' && answer != 'n'); // double quotes "" are for c-strings. single quotes '' are for chars.
if (answer == 'n'){
return 1;
}
else printf("%i Server:\tListening for a client...\n", GetCurrentThreadId());
freeaddrinfo(result);
printf("Semaphore value: %i\n", semaphore);
//Now that the server is running, signal the semaphore to allow client to connect.
if (ReleaseSemaphore(semaphore, 1, NULL)) //ReleaseSemaphore() returns a bool value
{
printf("%i Server: Semaphore Released\n", GetCurrentThreadId());
}
else
{
printf("Server Release Semaphore error: %d\n", GetLastError());
printf("Server Semaphore value: %i\n", semaphore);
return 1;
}
///////////////////////////
// Listening on a Socket //
///////////////////////////
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("Listen failed with error: %ld\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
////////////////////////////
// Accepting a Connection //
////////////////////////////
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("%i Server:\t", GetCurrentThreadId());
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket); // This will need to be changed if multiple clients shall be allowed
//////////////////////////////////////////////
// Receiving and Sending Data on the Server //
//////////////////////////////////////////////
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("Bytes received: %d\n", iResult);
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket); //why close a socket if something doesn't send correctly?
WSACleanup();
return 1; //kills main()
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup(); //function terminates use of the Winsock 2 DLL (Ws2_32.dll)
return 1;
}
} while (iResult > 0);
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
_endthreadex(0); //end thread within the thread.
return 0;
}
Client.cpp :
#include "Header.h"
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
unsigned int __stdcall clientmain(void* data)
{
printf("CLIENT STARTED. Thread: %i\n", GetCurrentThreadId());
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters
/*if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
return 1;
}*/
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("%i Client:\t", GetCurrentThreadId());
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
printf("Client winsock initialized\n");
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
/*iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}*/
printf("%i Client: before semaphore\n", GetCurrentThreadId());
//Wait for Server to be up and running
bool cont = TRUE;
while (cont)
{
DWORD returnval = WaitForSingleObject(semaphore, 0L); // 0 vs 0L ?
//printf("Semaphore value: %i\n", semaphore);
switch (returnval)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
printf("Thread %d: wait succeeded\n", GetCurrentThreadId());
break;
// The semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n", GetCurrentThreadId());
break;
}
}
printf("%i Client: after semaphore\n", GetCurrentThreadId());
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("%i Client:\t", GetCurrentThreadId());
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("%i Client:\t", GetCurrentThreadId());
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("%i Client:\t", GetCurrentThreadId());
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("%i Client:\t", GetCurrentThreadId());
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
printf("%i Client:\t", GetCurrentThreadId());
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
printf("%i Client:\t", GetCurrentThreadId());
printf("\nReturn to close.\n");
getchar(); //allow user to do see screen before shutting down
_endthreadex(0);
return 0;
}
答案 0 :(得分:2)
您在semaphore
中将static HANDLE
声明为header.h
。这意味着包含"header.h"
的每个源模块都将拥有自己的变量副本。因此,main.cpp中包含Windows信号量句柄的semaphore
与semaphore
中引用的server.cpp
不同。
从static
中的声明中移除header.h
,将其替换为extern
,然后在main.cpp
中为其添加定义。