在CB中用socket api来写网络通讯程序
2008-03-08 12:53:50 来源:WEB开发网核心提示:本文转自csdn,作者:kingcaiyao原标题:在C++ Builder中用socket api来写网络通讯程序(同时支持TCP和UDP协议)原文:http://www.csdn.net/develop/read_article.asp?id=19883在7月4日看完sockcomp.pas后,在CB中用socke
本文转自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);
更多精彩
赞助商链接