首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

有个关于网络编程的异常,麻烦帮小弟看看

2012-01-10 
有个关于网络编程的错误,麻烦帮小弟看看!这个是发送消息线程,但是有错,我调试了,每次执行到sockClient.Cre

有个关于网络编程的错误,麻烦帮小弟看看!
这个是发送消息线程,但是有错,我调试了,每次执行到sockClient.Create();就出错,但是我不知道这句话错在哪里了,求指点

C/C++ code
/////TCP发送信息线程//////////UINT _SendMsgThread(LPVOID lparam)    {    //    CTestDlg *pDlg=(CTestDlg *)lparam;    //if(pDlg->StopServer==true)    return -1;    //pThreadLisen=::AfxBeginThread(_ListenTcpThread,this);    //开始TCP线程    CSocket sockClient;    sockClient.Create();    CString ip,strError;    //pDlg->m_You_IP.GetWindowText(ip);    int conn=sockClient.Connect(otherip, PORT+pDlg->m_client);    if(conn==0)    ///////////////////////////////////    {        AfxMessageBox("_SendMsgThread Connect错误!"+pDlg->GetError(GetLastError()));        sockClient.ShutDown(2);        sockClient.Close();        AfxEndThread(1L);        return 0;            }    //首先发送标记M为信息,2    int end=0;    end=sockClient.Send("M",FLAG);     if(end==SOCKET_ERROR)    {        AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));        return -1;    }    else if(end!=2)    {        AfxMessageBox("消息头错误");        return -1;    }    CString strMsg=pDlg->m_MsgSend;    end=sockClient.Send(strMsg,strMsg.GetLength());     if(end==SOCKET_ERROR)    {        AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));        return -1;    }    CString strLocalName;    pDlg->GetLocalHostName(strLocalName);    CString strLocalIP;    pDlg->GetIpAddress(strLocalName,strLocalIP);    pDlg->AddMsgList(strLocalIP+"->"+strLocalName,strMsg);        int i=0;    sockClient.Close();    return 0;}


[解决办法]
初始化socket
wsastartup(xxx)
[解决办法]
启动Winsock服务WSAStartup()
格式:int WSAStartup(WORD wVersionRequested,LPWSADATA lpWASData);
功能:此函数是对Windows Sockets DLL进行初始化,只有此函数调用成功后才可以调用其他Windows Sockets DLL的函数。
[解决办法]
估计是没加载套接字库

WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 2, 2 );
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
 
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
 
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return; 
}
 
/* The WinSock DLL is acceptable. Proceed. */


Declared in Winsock2.h.


[解决办法]
探讨
这个是发送消息线程,但是有错,我调试了,每次执行到sockClient.Create();就出错,但是我不知道这句话错在哪里了,求指点

C/C++ code
/////TCP发送信息线程//////////
UINT _SendMsgThread(LPVOID lparam)
{
//
CTestDlg *pDlg=(CTestDlg *)lparam;
/……

[解决办法]
WSAGetLastError()

热点排行