发布信号量返回错误 6(无效句柄)



我正在尝试使用两个线程来运行服务器和客户端。 我使用信号量来阻止客户端线程在设置服务器之前执行太多操作(ListenSocket等)。

当服务器足够远时,我使用ReleaseSemaphore()但它返回Invalid Handle错误。 我已经读到有许多可能的问题可能是此错误的根源。

确切的行是if (ReleaseSemaphore(semaphore, 1, NULL)){}可以在Server.cpp文件中找到大约 55%

的行标题.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;

资料来源.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: %dn", 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;
}

服务器.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: %in", 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: %dn", 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: %dn", 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: %ldn", 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: %dn", 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("%cn", 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: %in", 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 Releasedn", GetCurrentThreadId());
}
else
{
printf("Server Release Semaphore error: %dn", GetLastError());
printf("Server Semaphore value: %in", 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: %ldn", 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: %dn", 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: %dn", iResult);
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %dn", WSAGetLastError());
closesocket(ClientSocket); //why close a socket if something doesn't send correctly?
WSACleanup();
return 1; //kills main()
}
printf("Bytes sent: %dn", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...n");
else {
printf("recv failed with error: %dn", 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: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
_endthreadex(0); //end thread within the thread.
return 0;
}

客户.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: %in", 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-namen", 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: %dn", iResult);
return 1;
}
printf("Client winsock initializedn");
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: %dn", iResult);
WSACleanup();
return 1;
}*/

printf("%i Client: before semaphoren", 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: %in", semaphore);
switch (returnval)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
printf("Thread %d: wait succeededn", GetCurrentThreadId());
break;
// The semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed outn", GetCurrentThreadId());
break;
}
}
printf("%i Client: after semaphoren", 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: %ldn", 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: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ldn", 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: %dn", 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: %dn", iResult);
else if (iResult == 0)
printf("Connection closedn");
else
printf("recv failed with error: %dn", 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;
}

您在header.h中将semaphore声明为static HANDLE。 这意味着包含"header.h"的每个源模块都有自己的该变量副本。 因此,main.cpp 中保存 Windows 信号量句柄的semaphoreserver.cpp中引用的semaphore不同。

header.h中的声明中删除static,将其替换为extern,并在main.cpp中添加定义。

最新更新