C++中_beginthread传递的参数值错误
有以下两个方法,我在Connect中创建SOCKET但是在Connect中的值是正常的,到了Receive中就变了:Connect:780Receive:18956795...
有以下两个方法,我在Connect中创建SOCKET但是在Connect中的值是正常的,到了Receive中就变了:
Connect:780
Receive:1895679544
求高手帮看看原因!!!
// 连接设备
LONG Czh_OcxIFCtrl::Connect(LPCTSTR ComNum, LONG NaudNum)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: 在此添加调度处理程序代码
// 初始化套接字
WSADATA wsa;
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
Sleep(3000);
return 0;
}
//创建套接字
static SOCKET sock;
if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
return 0;
}
struct sockaddr_in serverAddress;
memset(&serverAddress,0,sizeof(sockaddr_in));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.S_un.S_addr = inet_addr(ComNum);
serverAddress.sin_port = htons(NaudNum);
//建立和服务器的连接
if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
Sleep(3000);
return 0;
}
static int sockID = sock;
//启动线程接收数据
char str[20];
sprintf(str, "%d", sock);
FILE* log;
log=fopen("log/log1.txt","a+");
if (log!=NULL){
fprintf(log,"%s\n",str);
}
fclose(log);
_beginthread(Receive,0,&sockID);
//记录连接
string ipadd(ComNum);
socketList[ipadd] = sockID;
ipList[sockID] = ipadd;
return 1;
}
void Czh_OcxIFCtrl::Receive(PVOID param)
{
int* sockID = (int*)param;
char str[20];
sprintf(str, "%d", sockID);
FILE* log;
log=fopen("log/log1.txt","a+");
if (log!=NULL){
fprintf(log,"%s\n",str);
}
fclose(log);
char buf[2096];
while(1)
{
SOCKET* sock=(SOCKET*)param;
int bytes;
if((bytes=recv(*sock,buf,sizeof(buf),0))==SOCKET_ERROR){
continue;
}
}
} 展开
Connect:780
Receive:1895679544
求高手帮看看原因!!!
// 连接设备
LONG Czh_OcxIFCtrl::Connect(LPCTSTR ComNum, LONG NaudNum)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: 在此添加调度处理程序代码
// 初始化套接字
WSADATA wsa;
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
Sleep(3000);
return 0;
}
//创建套接字
static SOCKET sock;
if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
return 0;
}
struct sockaddr_in serverAddress;
memset(&serverAddress,0,sizeof(sockaddr_in));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.S_un.S_addr = inet_addr(ComNum);
serverAddress.sin_port = htons(NaudNum);
//建立和服务器的连接
if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
Sleep(3000);
return 0;
}
static int sockID = sock;
//启动线程接收数据
char str[20];
sprintf(str, "%d", sock);
FILE* log;
log=fopen("log/log1.txt","a+");
if (log!=NULL){
fprintf(log,"%s\n",str);
}
fclose(log);
_beginthread(Receive,0,&sockID);
//记录连接
string ipadd(ComNum);
socketList[ipadd] = sockID;
ipList[sockID] = ipadd;
return 1;
}
void Czh_OcxIFCtrl::Receive(PVOID param)
{
int* sockID = (int*)param;
char str[20];
sprintf(str, "%d", sockID);
FILE* log;
log=fopen("log/log1.txt","a+");
if (log!=NULL){
fprintf(log,"%s\n",str);
}
fclose(log);
char buf[2096];
while(1)
{
SOCKET* sock=(SOCKET*)param;
int bytes;
if((bytes=recv(*sock,buf,sizeof(buf),0))==SOCKET_ERROR){
continue;
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询