套接字问题,急,在线等高手帮忙解决解决!
首先我说一下程序出现的问题,
当服务器运行后,然后在本机上运行客户端,并点击连接,这时候客户端就会弹出错误对话框,
Debug Assertion Failed!
Program:d\program\Tickets_Client\Debug\Tickets_Client.exe
File:sockcore.cpp
line:551
......
代码说明:
我在服务器端创建了个套接字,并设置为监听模式,需要说明的是:我用的从CAsyncSocket类派生出来的类,
服务器端的主要代码如下:
void CTickets_SellView::OnBtnServer()
{
// TODO: Add your control notification handler code here
if(IsSetup==TRUE)
{
return;
}
Sock.InitSock(this);
BOOL result;
result=Sock.Create(50000,SOCK_STREAM);
if(result==0)
{
Sock.Close();
CString errorcode;
errorcode.Format("%d",GetLastError());
errorcode+="创建套接字失败";
AfxMessageBox(errorcode);
return;
}
if(!Sock.Listen())
{
Sock.Close();
CString error;
error.Format("监听失败:%d",GetLastError());
AfxMessageBox(error);
return;
}
CString text;
CWnd *pWnd=NULL;
pWnd=GetDlgItem(IDC_STATIC_CONNNUM);
pWnd->SetWindowText("当前连数:0");
OnBtnSetup();
IsSetup=TRUE;
return;
}
客户端主要代码:
void CClientSocket::ConnectionToServer(CString buffer)
{
struct in_addr addr_to;
if((addr_to.S_un.S_addr=inet_addr(buffer))== INADDR_NONE)
{
AfxMessageBox("IP地址格式不正确");
return ;
}
if(!Create(60000))
{
Close();
CString error;
error.Format("创建套接字失败:%d",GetLastError());
AfxMessageBox(error);
}
SOCKADDR_IN addrto;
addrto.sin_family=AF_INET;
addrto.sin_addr.S_un.S_addr=addr_to.S_un.S_addr;
addrto.sin_port=htons(50000);
BOOL result;
result=Connect((SOCKADDR*)&addrto,sizeof(SOCKADDR));
if(!result)
{
Close();
CString error;
error.Format("连接服务器失败:%d",GetLastError());
AfxMessageBox(error);
return ;
}
}
void CTickets_ClientDlg::OnConnect()
{
// TODO: Add your command handler code here
CWnd *pWnd;
pWnd=GetDlgItem(IDC_EDIT_IP);
CString buffer;
pWnd->GetWindowText(buffer);
pClientSocket->ConnectionToServer(buffer);
}