线程访问内存的问题。

xf_pan xf_pan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如下程序,我在main里调用WinSocket的AcceptSocket方法没问题。。
但把步移到线程里,调试发现不能访问WinSocket的成员变量,nClientSocketAddrLen。。
这是什么问题啊,,线程里是用的这个对象的引用阿。。为什么无法访问呢~~
听听高手的解答。
程序如下
TestMian:

//#include <windows.h>
#include <iostream>
#include <winsock2.h>
#include "WinSocket.h"
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
const u_long PORT = 5555;
HANDLE hHand;

DWORD WINAPI doAcceptConnect(WinSocket &winSocket);

int main()
{
WinSocket winSocket;
if(winSocket.InitSocketLib())
{
return 0;
}
SOCKADDR_IN serverSockAddr;
serverSockAddr.sin_addr.S_un.S_addr = htons(INADDR_ANY);
serverSockAddr.sin_family = AF_INET;
serverSockAddr.sin_port = htons(PORT);

if(!winSocket.CreateSocket())
{
return 0;
}
if(!winSocket.BindSocket(serverSockAddr))
{
return 0;
}
if(!winSocket.ListenSocket(2))
{
return 0;
}

/* if(!winSocket.AcceptSocket())
{
return 0;
}*/
hHand = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)doAcceptConnect,NULL,0,NULL);

while(1)
{
Sleep(500);
}
}

DWORD WINAPI doAcceptConnect(WinSocket &winSocket)
{
while(true)
{
if(!winSocket.AcceptSocket())
{
return 0;
}

char *buffer = new char[255];
if(!winSocket.ReceiveSocket(&buffer))
{
return 1;
}

cout<<buffer<<endl;
}
return 0;
}


WinSocket.h WinSocket.cpp

/**
*create a sokcet server or client
**/
#ifndef WINSOCKET_H
#define WINSOCKET_H
#pragma once
//#include <windows.h>
#include <winsock2.h>
#include <iostream>

using namespace std;

#pragma comment(lib,"Ws2_32.lib")

const int SUCCESS = 1;
const int MAXBUFLEN = 256;
const int NO_FLAGS_SET = 0;

class WinSocket
{

public:
WinSocket(void);
~WinSocket(void);

/**
*listen socket
**/
int SendMessage(SOCKET ¶Socket,SOCKADDR_IN ¶SeverAddr);


/**
*init the socket lib
**/
int InitSocketLib();

/**
*craate a socket
**/
int CreateSocket();

/**
*bind the socket
**/
int BindSocket(SOCKADDR_IN ¶SeverAddr);

/**
*listen socket
**/
int ListenSocket(int nType);

/**
*listen socket
**/
int AcceptSocket();

/**
*listen socket
**/
int ConnectSocket(string strServerIP,int nPort);

/**
*listen socket
**/
int ReceiveSocket(char **buffer);

private:
WSADATA m_WSAdata;
SOCKET m_socket,m_ClientSocket;
SOCKADDR_IN m_ServerSockAddr,m_ClientSocketAddr;
int nClientSocketAddrLen;
};
#endif


#include "WinSocket.h"

WinSocket::WinSocket(void)
{
}

WinSocket::~WinSocket(void)
{
}

/**
*init the socket lib
**/
int WinSocket::InitSocketLib()
{
return WSAStartup(MAKEWORD(1, 1), &m_WSAdata);
}
/**
*craate a Socket
**/
int WinSocket::CreateSocket()
{
m_socket = socket(AF_INET, SOCK_STREAM, 0);
if(m_socket==INVALID_SOCKET)
{
return ERROR;
}
return SUCCESS;

}

/**
*bind the socket
**/
int WinSocket::BindSocket(SOCKADDR_IN ¶SeverAddr)
{
if(bind(m_socket,(LPSOCKADDR)¶SeverAddr,sizeof(paraSeverAddr)) == SOCKET_ERROR)
{
return ERROR;
}

return SUCCESS;
}

/**
*listen socket
**/
int WinSocket::ListenSocket(int nType)
{
if(listen(m_socket,nType)==SOCKET_ERROR)
{
return ERROR;
}
return SUCCESS;
}

/**
*listen socket
**/
int WinSocket::AcceptSocket()
{
nClientSocketAddrLen = sizeof(m_ClientSocketAddr);
int nResult = accept(m_socket,(LPSOCKADDR)&m_ClientSocketAddr,&nClientSocketAddrLen);
if(nResult==SOCKET_ERROR)
{
return ERROR;
}
return SUCCESS;
}

/**
*listen socket
**/
int WinSocket::ConnectSocket(string strServerIP,int nPort)
{
m_ServerSockAddr.sin_addr.S_un.S_addr = inet_addr(strServerIP.c_str());
m_ServerSockAddr.sin_port = nPort;
m_ServerSockAddr.sin_family = AF_INET;

if(connect(m_socket,(LPSOCKADDR)&m_ServerSockAddr,sizeof(m_ServerSockAddr))==SOCKET_ERROR)
{
return ERROR;
}
return SUCCESS;
}

/**
*listen socket
**/
int WinSocket::ReceiveSocket(char **buffer)
{
int nResult = recv(m_ClientSocket,*buffer,MAXBUFLEN, NO_FLAGS_SET);
if(nResult == 0 || nResult == SOCKET_ERROR)
{
return ERROR;
}
return SUCCESS;
}

...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
xf_pan
C++ 语言 65,214 成员 250,514 内容