本文转自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)