CB嶄喘socket api亟利大宥儷殻會
作者 佚名技术
来源 程序设计
浏览
发布时间 2012-06-30
eanup(); return; } void __fastcall TListenThread::InitSocket() { WORD version=MAKEWORD(2,0); if(::WSAStartup(version,&wsaData)){ sprintf(szTmp,"Failed to intiailize socket,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); return; } } void __fastcall TListenThread::CreateListenSocket() { switch(m_Protocol) { case UDP: m_Socket=socket(AF_INET,SOCK_DGRAM,0); break; case TCP: m_Socket=socket(AF_INET,SOCK_STREAM,0); break; default: sprintf(szTmp,"Error protocol!"); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); break; } if(m_Socket==INVALID_SOCKET){ sprintf(szTmp,"Failed to create socket!"); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); return; } } void __fastcall TListenThread::SetListenSocket() { server.sin_family=AF_INET; server.sin_port=htons(m_Port); server.sin_addr.S_un.S_addr=INADDR_ANY; int NewOpenType=SO_SYNCHRONOUS_NONALERT; if(setsockopt(INVALID_SOCKET,SOL_SOCKET,SO_OPENTYPE,(char*)&NewOpenType,4)){ sprintf(szTmp,"Set socket option error,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); return; } } void __fastcall TListenThread::BindListenSocket() { if(bind(m_Socket,(sockaddr*)&server,sizeof(struct sockaddr_in))){ sprintf(szTmp,"Failed to bind socket,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); return; } } void __fastcall TListenThread::ListenSocket() { if(listen(m_Socket,SOMAXCONN)){ sprintf(szTmp,"listen error,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); DoError(); return; } //Determine whether there is any connection FD_ZERO(&FDS); FD_SET(m_Socket,&FDS); } void __fastcall TListenThread::Execute() { char buf[4096]; struct sockaddr_in from; //for UDP int nLen=sizeof(from),nSize=0; //for UDP InitSocket(); CreateListenSocket(); SetListenSocket(); BindListenSocket(); if(m_Protocol==UDP){ while(!Terminated){ int nSize=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLen); if(nSize>0){ buf[nSize]=''\0''; PSTNForm->Memo1->Lines->Add("Received Length:"+String(nSize)); PSTNForm->Memo1->Lines->Add("Received:"+StrPas(buf)); ::Sleep(100); sendto(m_Socket,buf,nSize,0,(struct sockaddr*)&from,sizeof(struct socka |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: C++Builder调用Matlab下一篇: 用C++Builder获取应用程序图标
关于CB嶄喘socket api亟利大宥儷殻會的所有评论