CB嶄喘socket api亟利大宥儷殻會
作者 佚名技术
来源 程序设计
浏览
发布时间 2012-06-30
if(m_Protocol==TCP){ if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr))){ sprintf(szTmp,"Failed to connect the object!,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); closesocket(m_Socket); ::WSACleanup(); return; } int nError=select(1,0,&FDSET,0,0); if(nError<=0){ sprintf(szTmp,"Failed to select socket!,error no:%d",::WSAGetLastError()); closesocket(m_Socket); ::WSACleanup(); return;} } //Send data int nLen=TxtEdit->Text.Length(); if(nLen>4096){ sprintf(szTmp,"The buffer is too size to send,it shoud not be more than 4096 bytes!"); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); closesocket(m_Socket); ::WSACleanup(); return; } strncpy(buf,TxtEdit->Text.c_str(),nLen); switch(m_Protocol) { case TCP: nSize=send(m_Socket,buf,nLen,0); //ShowMessage(nSize); break; case UDP: nSize=sendto(m_Socket,buf,nLen,0,(struct sockaddr*)&To,sizeof(struct sockaddr)); //ShowMessage(nSize); break; } if(::WSAAsyncSelect(m_Socket,Handle,WM_SOCK,FD_READ|FD_CLOSE)){ sprintf(szTmp,"Failed to register socket event!,error no:%d",::WSAGetLastError()); ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR); closesocket(m_Socket); ::WSACleanup(); return;} } void __fastcall TLANForm::FormDestroy(TObject *Sender) { closesocket(m_Socket); ::WSACleanup(); } Socket api Server: .h File #ifndef UDPServerH #define UDPServerH #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <stdio.h> #include "WinSock.h" /*#define WM_SOCKET WM_USER+1000 #define INITSOCKETSUCCESS 0 #define INITSOCKETFAILURE 1 #define CREATELISTENSOCKETSUCCESS 2 #define CREATELISTENSOCKETFAILURE 3 #define SETLISTENSOCKETSUCCESS 4 #define SETLISTENSOCKETFAILURE 5 #define BINDLISTENSOCKETSUCCESS 6 #define BINDLISTENSOCKETFAILURE 7 #define LISTENSOCKETSUCCESS 8 #define LISTENSOCKETFAILURE 9 #define ACCEPTSOCKETSUCCESS 10 #define ACCEPTSOCKETFAILURE 11 */ class TPSTNForm : public TForm { __published: // IDE-managed Components TEdit *Port; TLabel *Label1; TMemo *Memo1; TButton *Button1; TButton *Button2; TLabel *Label2; TComboBox *Prot; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall FormCreate(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); private: // User declarations public: // User declarations __fastcall TPSTNForm(TComponent* Owner); }; enum PROTO {TCP,UDP}; class TCommunication:publi |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: C++Builder调用Matlab下一篇: 用C++Builder获取应用程序图标
关于CB嶄喘socket api亟利大宥儷殻會的所有评论