[求助]关于 WinSocket+双线程实现同时收发问题
如题。我想实现Client端和Server端同时具有收发的功能,于是我想到了双线程。目前正在调试Client端。
如下:
WSAStartup
ZeroMemory
getaddrinfo
freeaddrinfo
_beginthreadex
在创建的新的线程里面,调用send函数。
发现发送不过去。
而如果将_beginthreadex开启的新的线程里面的函数,放在main函数中,则可以正常进行。
很困惑这个问题,是不是在新的线程里面要重新进行初始化——连接 等操作?
代码如下,为了方便调试,把接收的注释掉了。
程序代码:
#define WIN32_LEAN_AND_MEAN #define _CRT_SECURE_NO_WARNINGS #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> #include <process.h> // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "8888" unsigned int __stdcall Send(void* ConnectSocket) { char input[DEFAULT_BUFLEN]; gets(input); printf("%s", input); while (*input != EOF) { send((int)ConnectSocket, input, (int)strlen(input), 0); } closesocket((int)ConnectSocket); WSACleanup(); return 1; int iResult; iResult = shutdown((int)ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); closesocket((int)ConnectSocket); WSACleanup(); return 1; } } int __cdecl main(int argc, char **argv) { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; //~0==-1,因为Socket 类型被定义为unsigned //printf("%d", ~0); //-1 struct addrinfo *result = NULL, *ptr = NULL, hints; char sendbuf[DEFAULT_BUFLEN]; //发送的文字 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("WSAStartup failed with error: %d\n", iResult); return 1; } 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; } // 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("socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } // Connect to server. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); printf("Waiting...\n"); while (iResult == SOCKET_ERROR) { iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); } printf("Connect!\n"); break; } freeaddrinfo(result); // Send an initial buffer _beginthreadex(NULL, 0, Send, NULL, 0, NULL); //创建一个线程 //do { // iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); // if (iResult > 0){ // printf("Bytes received: %d\n", iResult); // recvbuf[iResult] = '\0'; // printf("Receive:\n%s\n", recvbuf); // } // //} while (true); // cleanup /*closesocket(ConnectSocket); WSACleanup();*/ while (true) { ; } system("pause"); return 0; }
最后,为了防止主函数结束,我用了死循环。
我的联系方式:littlesevenmo@vip.
不胜感激。