快速业务通道

CB嶄喘socket api亟利大宥儷殻會

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-30

壓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

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号