多线程下MFC网络编程CSock类
UINT ReceiverThreadProc(LPVOID pParam)//receive thread
{
if (!AfxSocketInit())
{
AfxMessageBox("应用程序没有调用 WSAStartup,或者 WSAStartup 失败");
return FALSE;
}
CSocket RecSock;
CSocket Connect;
//sockaddr_in RecAddr;
//memset(&RecAddr,0,sizeof(RecAddr));
//RecAddr.sin_family=AF_INET;
//RecAddr.sin_addr.s_addr=htonl(INADDR_ANY);
//RecAddr.sin_addr.s_addr=inet_addr("127.0.0.1");;
//RecAddr.sin_port =htons(PORT);
/************得到本机IP地址***********/
char m_SysName[50];
char lpAddr[50];
gethostname(m_SysName,sizeof(m_SysName));
struct hostent * p_HostInfo=::gethostbyname(m_SysName);
in_addr inAddr;
memcpy(lpAddr,p_HostInfo->h_addr_list[0],strlen(lpAddr));
memmove(&inAddr,lpAddr,4);
/***********************************/
if(!RecSock.Create(PORT,SOCK_STREAM,inet_ntoa(inAddr)))
//create后,bind出错.需要bind的地址直接在create参数中指定.
//if(!RecSock.Bind((SOCKADDR*)&RecAddr,sizeof(SOCKADDR)))
if (!RecSock.Listen())
while(true){
if(!RecSock.Accept(Connect))
{
CString str;
str.Format("Accept error code:%d",RecSock.GetLastError());
AfxMessageBox(str);
return 0;
}
if (Connect.Receive(buffer,sizeof(buffer))<0)
{
CString str;
str.Format("Receive error code:%d",RecSock.GetLastError());
AfxMessageBox(str);
Connect.Close();
continue;
}
//处理数据
Connect.Close();
}
}
//发送线程
UINT SenderThreadProc(LPVOID pParam)
{
if (!AfxSocketInit())
{
AfxMessageBox("应用程序没有调用 WSAStartup,或者 WSAStartup 失败");
return FALSE;
}
char buf[BUFLEN];
CSocket SendSock;
sockaddr_in RecAddr;
RecAddr.sin_family=AF_INET;
RecAddr.sin_addr.s_addr=inet_addr(/*char * receiverip地址*/);
RecAddr.sin_port =htons(PORT);
SendSock.Create();
if (!SendSock.Connect((SOCKADDR *)&RecAddr,sizeof(RecAddr)))
{
CString str;
str.Format("Connect error code:%d",SendSock.GetLastError());
AfxMessageBox(str);
free(temp);
return 0;
}
//往buf中添加数据
SendSock.Send(buf,BUFLEN);
SendSock.Close();
return 1;
}
ps:线程函数要声明为static类型,或是全局的.如果在线程函数中要访问类中的成员,就将this指针传给pParam.
{
if (!AfxSocketInit())
{
AfxMessageBox("应用程序没有调用 WSAStartup,或者 WSAStartup 失败");
return FALSE;
}
CSocket RecSock;
CSocket Connect;
//sockaddr_in RecAddr;
//memset(&RecAddr,0,sizeof(RecAddr));
//RecAddr.sin_family=AF_INET;
//RecAddr.sin_addr.s_addr=htonl(INADDR_ANY);
//RecAddr.sin_addr.s_addr=inet_addr("127.0.0.1");;
//RecAddr.sin_port =htons(PORT);
/************得到本机IP地址***********/
char m_SysName[50];
char lpAddr[50];
gethostname(m_SysName,sizeof(m_SysName));
struct hostent * p_HostInfo=::gethostbyname(m_SysName);
in_addr inAddr;
memcpy(lpAddr,p_HostInfo->h_addr_list[0],strlen(lpAddr));
memmove(&inAddr,lpAddr,4);
/***********************************/
if(!RecSock.Create(PORT,SOCK_STREAM,inet_ntoa(inAddr)))
//create后,bind出错.需要bind的地址直接在create参数中指定.
//if(!RecSock.Bind((SOCKADDR*)&RecAddr,sizeof(SOCKADDR)))
if (!RecSock.Listen())
while(true){
if(!RecSock.Accept(Connect))
{
CString str;
str.Format("Accept error code:%d",RecSock.GetLastError());
AfxMessageBox(str);
return 0;
}
if (Connect.Receive(buffer,sizeof(buffer))<0)
{
CString str;
str.Format("Receive error code:%d",RecSock.GetLastError());
AfxMessageBox(str);
Connect.Close();
continue;
}
//处理数据
Connect.Close();
}
}
//发送线程
UINT SenderThreadProc(LPVOID pParam)
{
if (!AfxSocketInit())
{
AfxMessageBox("应用程序没有调用 WSAStartup,或者 WSAStartup 失败");
return FALSE;
}
char buf[BUFLEN];
CSocket SendSock;
sockaddr_in RecAddr;
RecAddr.sin_family=AF_INET;
RecAddr.sin_addr.s_addr=inet_addr(/*char * receiverip地址*/);
RecAddr.sin_port =htons(PORT);
SendSock.Create();
if (!SendSock.Connect((SOCKADDR *)&RecAddr,sizeof(RecAddr)))
{
CString str;
str.Format("Connect error code:%d",SendSock.GetLastError());
AfxMessageBox(str);
free(temp);
return 0;
}
//往buf中添加数据
SendSock.Send(buf,BUFLEN);
SendSock.Close();
return 1;
}
ps:线程函数要声明为static类型,或是全局的.如果在线程函数中要访问类中的成员,就将this指针传给pParam.