CB嶄喘socket api亟利大宥儷殻會
作者 佚名技术
来源 程序设计
浏览
发布时间 2012-06-30
eturn; } unsigned long nAddr=inet_addr(addr.c_str()); if(nAddr==INADDR_NONE){ ::MessageBox(0,"Bad Internet IP!","Error",MB_OK+MB_ICONERROR); return;} try { m_Port=Port->Text.ToInt(); } catch(Exception &e) { ::MessageBox(0,e.Message.c_str(),"Error",MB_OK+MB_ICONERROR); return; } switch(Prot->ItemIndex) { case 0: m_Protocol=TCP; break; case 1: m_Protocol=UDP; break; } if(TxtEdit->Text.IsEmpty()){ ::MessageBox(0,"Please enter the text you want to send!","Error",MB_OK+MB_ICONERROR); return;} //Initialize Winsocket WSAData wsaData; ::ZeroMemory(&wsaData,sizeof(WSAData)); WORD version=MAKEWORD(2,0); if(::WSAStartup(version,&wsaData)){ sprintf(szTmp,"Failed to initial winsock enviroment!,error no:%d",::WSAGetLastError()); return;} //Obtain the active connection char ComputerName[255]; gethostname(ComputerName,255); struct hostent* he=gethostbyname(ComputerName); if(!he){ sprintf(szTmp,"Failed to get information to host!","Error",MB_OK+MB_ICONERROR); ::WSACleanup(); return; } //create new socket m_Socket=INVALID_SOCKET; switch(m_Protocol) { case TCP: m_Socket=socket(AF_INET,SOCK_STREAM,0); break; case UDP: m_Socket=socket(AF_INET,SOCK_DGRAM,0); break; } if(m_Socket==INVALID_SOCKET){ sprintf(szTmp,"Failed to create a new socket!,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); ::WSACleanup(); return; } //bind socket struct sockaddr_in client; unsigned long nClient; memcpy(&nClient,he->h_addr_list[0],sizeof(int)); if(nClient==INADDR_NONE){ sprintf(szTmp,"Failed to obtain the local machine''s IP!","Error",MB_OK+MB_ICONERROR); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); closesocket(m_Socket); ::WSACleanup(); return; } client.sin_family=AF_INET; client.sin_port=0; client.sin_addr.S_un.S_addr=(int)nClient; if(bind(m_Socket,(struct sockaddr*)&client,sizeof(struct sockaddr))){ sprintf(szTmp,"Failed to bind socket!","Error",MB_OK+MB_ICONERROR); closesocket(m_Socket); ::WSACleanup(); return;} //connect socket struct sockaddr_in To; To.sin_family=AF_INET; To.sin_port=htons(m_Port); To.sin_addr.S_un.S_addr=(int)nAddr; fd_set FDSET; FD_ZERO(&FDSET); FD_SET(m_Socket,&FDSET); |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: C++Builder调用Matlab下一篇: 用C++Builder获取应用程序图标
关于CB嶄喘socket api亟利大宥儷殻會的所有评论