用VC编写C/S消息传送程序
2008-11-14 19:37:11 来源:WEB开发网5.在菜单中添加"拨号"项:
void CClientView::OnDial()
{
// TODO: Add your command handler code here
count=5;
if(m_bInitialized)
{
AfxMessageBox("Already dialing");
return;
}
Cdial dial;
if(dial.DoModal()==IDOK&&!dial.m_HostAddress.IsEmpty())
{
m_saSrvr.sin_family=AF_INET;
m_saSrvr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
m_saSrvr.sin_addr.S_un.S_addr=inet_addr(dial.m_HostAddress);
m_saSrvr.sin_port=htons(5050);
int status;
WSADATA wsaData;
m_data[0]="initializing Windows Sockets DLL....";
if((status=WSAStartup(0x0101,&wsaData))==0)
{
m_data[0]+="Succeeded";
m_bInitialized=TRUE;
}
else
{
m_bInitialized=FALSE;
}
m_sock=socket(AF_INET,SOCK_DGRAM,0);
m_data[1]="Creating socket....";
if(m_sock==INVALID_SOCKET)
{
m_data[1]+="Failed";
}
m_data[1]+="Succeeded";
m_data[2]="Binding socket....";
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
sa.sin_port=htons(0);
if(bind(m_sock,(PSOCKADDR)&sa, sizeof(sa))==SOCKET_ERROR)
{
m_data[2]+="Failed";
closesocket(m_sock);
}
m_data[2]+="Succeeded";
m_data[3]="Creating listener thread....";
unsigned long idThread;
m_hListenThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Listen,(void *)this,0,&idThread);
if(m_hListenThread)
{
m_data[3]+="Succeeded";
m_data[4]+="Waiting....";
}
else
m_data[4]+="Failed";
InvalidateRect(NULL);
}
}
更多精彩
赞助商链接