壓7埖4晩心頼sockcomp.pas朔?厘畳協喘socket api栖亟匯倖人薩極才捲暦匂旺拝揖扮屶隔TCP,UDP亅咏?噐頁厘祥肇恂??繍旗鷹薮竃栖?厮距編宥狛?
Socket api Client: #ifndef UDPClientH #define UDPClientH #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <stdio.h> #include "CCEdit.h" #define WM_SOCK WM_USER+100 class TLANForm : public TForm { __published: // IDE-managed Components TEdit *Port; TLabel *Label1; TLabel *Label2; TComboBox *Prot; TButton *Button1; TLabel *Label3; TEdit *Addr; TCCEdit *TxtEdit; void __fastcall FormCreate(TObject *Sender); void __fastcall Button1Click(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); private: // User declarations void __fastcall OnRecv(TMessage &Message); public: // User declarations __fastcall TLANForm(TComponent* Owner); BEGIN_MESSAGE_MAP VCL_MESSAGE_HANDLER(WM_SOCK,TMessage,OnRecv); END_MESSAGE_MAP(TForm); }; extern PACKAGE TLANForm *LANForm; #endif .cpp File #include <vcl.h> #pragma hdrstop #include "UDPClient.h" #include "WinSock.h" #pragma package(smart_init) #pragma link "CCEdit" #pragma resource "*.dfm" TLANForm *LANForm; enum PROTO {TCP=0,UDP=1}; SOCKET m_Socket=INVALID_SOCKET; PROTO m_Protocol=TCP; __fastcall TLANForm::TLANForm(TComponent* Owner) : TForm(Owner) { } void __fastcall TLANForm::FormCreate(TObject *Sender) { ::SendMessage(Prot->Handle,CB_SETCURSEL,0,0); } void __fastcall TLANForm::OnRecv(TMessage &Message) { char buf[4096]; int nLen; struct sockaddr_in from; int nLength=sizeof(struct sockaddr_in); switch(WSAGETSELECTEVENT(Message.LParam)) { case FD_READ: switch(m_Protocol) { case TCP: nLen=recv(m_Socket,buf,4096,0); if(nLen>0){ buf[nLen]=''\0''; TxtEdit->Text="Received Length:"+String(nLen)+"\r\n"+StrPas(buf); } break; case UDP: nLen=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLength); if(nLen>0){ buf[nLen]=''\0''; TxtEdit->Text="Received Length:"+String(nLen)+"\r\n"+StrPas(buf); } break; } break; case FD_CLOSE: closesocket(m_Socket); break; } } void __fastcall TLANForm::Button1Click(TObject *Sender) { char szTmp[256],buf[4096]; int nSize=0; UINT m_Port; AnsiString addr; addr=Addr->Text.Trim(); if(addr.IsEmpty()){ ::MessageBox(0,"Please enter the server IP!","Error",MB_OK+MB_ICONERROR); r
|