国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在CB中用socket api來寫網絡通訊程序

2019-11-17 05:28:49
字體:
來源:轉載
供稿:網友

  本文轉自csdn,作者:kingcaiyao
原標題:在C++ Builder中用socket api來寫網絡通訊程序(同時支持TCP和UDP協議)
原文:  http://www.csdn.net/develop/read_article.asp?id=19883


在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);

      return;
   }
   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);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尖扎县| 景德镇市| 辽宁省| 固始县| 平安县| 山东| 德令哈市| 清涧县| 武穴市| 鹤庆县| 苏州市| 乌鲁木齐县| 贡山| 托克逊县| 阳山县| 开封县| 敦化市| 郓城县| 儋州市| 马鞍山市| 长宁区| 绥中县| 胶州市| 石泉县| 巩义市| 桃园市| 和龙市| 周宁县| 惠来县| 信宜市| 湛江市| 金湖县| 北京市| 托里县| 肇源县| 延边| 东海县| 虹口区| 宁晋县| 彭泽县| 富锦市|