来自网络课程设计.下面的程序客户端可以向服务器发数据,而服务器发的数据客户端收不到,客户和服务器使用独立的线程进行收包工作.
client.cPP#include
using namespace std;
DWORD WINAPI ThreadFunc1(LPVOID pParam);
CRITICAL_SECTION m_cs;
WSADATA wsaData;
char recvBuf[50],sendBuf[50];
char peer_ip[20];
SOCKET sockClient;
void main(void)
{
WORD wVersionRequested;
int err;
wVersionRequested = MAKEWORD(1,1);
::InitializeCriticalSection(&m_cs);
if((err = WSAStartup(wVersionRequested,&wsaData))!=0)
return;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!= 1){
WSACleanup();
return;
}
SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port=htons(6000);
while(1){
cout<<"PLEASE ENTER PEER IP ADDRESS:";
cin>>peer_ip;
addrSrv.sin_addr.S_un.S_addr = inet_addr(peer_ip);
if(connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)))
cout<<"CONNECTION FAILED!\n";
else {
cout<<"CONNECTION BUILT!\n";
break;
}
}
CreateThread(NULL,0,ThreadFunc1,NULL,0,0);
while(1){
fgets(sendBuf,20,stdin);
send(sockClient,sendBuf,strlen(sendBuf)+1,0);
}
closesocket(sockClient);
WSACleanup();
}
DWORD WINAPI ThreadFunc1(LPVOID pParam)
{
int ret;
// EnterCriticalSection(&m_cs);
while(1){
ret = recv(sockClient,recvBuf,50,0);
if(ret != SOCKET_ERROR && ret != 0 && ret != 2)
cout< }
// LeaveCriticalSection(&m_cs);
return 0;
}
server.cpp
#include
#include
using namespace std;
DWORD WINAPI ThreadFunc1(LPVOID pParam);
CRITICAL_SECTION m_cs;
WSADATA wsaData;
SOCKET sockSrv,sockConn;
char recvBuf[50],sendBuf[50];
SOCKADDR_IN addrClient;
void main(void)
{
WORD wVersionRequested;
int err;
wVersionRequested = MAKEWORD(1,1);
::InitializeCriticalSection(&m_cs);
if((err = WSAStartup(wVersionRequested,&wsaData))!=0)
return;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return;
}
sockSrv = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
cout<<"SOCKET ESTABLISHED,WAITING FOR CONNECTION...\n";
int len = sizeof(SOCKADDR);
sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
cout<<"INCOMING CONNECTION: "< CreateThread(NULL,0,ThreadFunc1,NULL,0,0);
while(1){
printf(" server input\n");
fgets(sendBuf,20,stdin);
printf("%s\n", sendBuf);
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
}
closesocket(sockConn);
WSACleanup();
}
DWORD WINAPI ThreadFunc1(LPVOID pParam)
{
int ret;
// EnterCriticalSection(&m_cs);
while(1){
printf(" server entered\n");
ret = recv(sockConn,recvBuf,50,0);
if(ret != SOCKET_ERROR && ret != 0 && ret != 2)
cout< }
// LeaveCriticalSection(&m_cs);
return 0;
}
答:
错误在这client端的这两行,主线程看到的是局部的sockClient,而新线程看到的是全局的sockClient.
SOCKET sockClient;
SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
新线程的sockClient无效,实际检测recv的返回值,利用WSAGetLastError ()便可以发现该问题
阅读(1428) | 评论(0) | 转发(0) |