扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱晓华 来源:VCHELP 2007年10月19日
关键字:
#include "winsock.h" |
CString m_data[1000]; HANDLE m_hListenThread; SOCKET m_sock; SOCKADDR_IN m_saSrvr; BOOLIsconnect; int count; CSize sizeTotal; BOOLflag; |
CClientView::CClientView() { // TODO: add construction code here Isconnect=FALSE; sizeTotal.cy=350; sizeTotal.cx=300; flag=FALSE; } |
CClientView::~CClientView() { if(m_bInitialized) WSACleanup(); closesocket(m_sock); if(m_hListenThread) ::TerminateThread(m_hListenThread,0); } |
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); } } |
#include dial.h |
void CClientView::OnUpdateDial (CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable(TRUE); if(Isconnect) pCmdUI->Enable(FALSE); } |
long WINAPI Listen(CClientView *pView) { char msg[2000]; pView- >m_data[5]="Sending ATTACH command"; pView- >InvalidateRect(NULL); wsprintf(msg,"A: "); sendto(pView- >m_sock,msg,1024,0,(PSOCKADDR)&pView- >m_saSrvr,sizeof(pView- >m_saSrvr)); int saSrvrLen ,nchar; while(1) { saSrvrLen=sizeof(pView->m_saSrvr); nchar=recvfrom(pView- >m_sock,msg,1024,0,(PSOCKADDR)&pView- >m_saSrvr,&saSrvrLen); if(nchar<0) { pView->m_data[pView- >count++]="Error in recvform"; pView- >InvalidateRect(NULL); } else { pView->m_data[pView- >count++]=msg; pView->Isconnect=TRUE; pView->flag=TRUE; pView->InvalidateRect(NULL); } } return(0); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者