在C++ Builder中用socket api來寫網路通訊程式(同時支援TCP和UDP協議)
原標題:在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
#include
#include
#include
#include
#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
#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協議型別
SOCKET m_Socket=INVALID_SOCKET; //定義一個socket物件
PROTO m_Protocol=TCP; //socket型別變數
__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]='';
TxtEdit->Text="Received Length:"+String(nLen)+"rn"+StrPas(buf);
}
break;
case UDP:
nLen=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLength);
if(nLen>0){
buf[nLen]='';
TxtEdit->Text="Received Length:"+String(nLen)+"rn"+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()){//IP為空結束
::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){//IP地址無效
::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;}
//1.初始化Winsock
WSAData wsaData;
::ZeroMemory(&wsaData,sizeof(WSAData)); //0填充
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);
if(!he){
sprintf(szTmp,"Failed to get information to host!","Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//2.建立新socket
m_Socket=INVALID_SOCKET;
switch(m_Protocol)
{
case TCP:
m_Socket=socket(AF_INET,SOCK_STREAM,0);
break;
case UDP:
m_Socket=socket(AF_INET,SOCK_DGRAM,0);
break;
}
if(m_Socket==INVALID_SOCKET){//建立socket失敗
sprintf(szTmp,"Failed to create a new socket!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//3.繫結SOCKET
struct sockaddr_in client;
unsigned long nClient;
memcpy(&nClient,he->h_addr_list[0],sizeof(int));
if(nClient==INADDR_NONE){
sprintf(szTmp,"Failed to obtain the local machine's IP!","Error",MB_OK+MB_ICONERROR);
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
client.sin_family=AF_INET;
client.sin_port=0;
client.sin_addr.S_un.S_addr=(int)nClient;
if(bind(m_Socket,(struct sockaddr*)&client,sizeof(struct sockaddr))){
sprintf(szTmp,"Failed to bind socket!","Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;}
struct sockaddr_in To;
To.sin_family=AF_INET;
To.sin_port=htons(m_Port);
To.sin_addr.S_un.S_addr=(int)nAddr;
fd_set FDSET;
FD_ZERO(&FDSET);
FD_SET(m_Socket,&FDSET);
//4.連線遠端主機
if(m_Protocol==TCP){
if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr))){
sprintf(szTmp,"Failed to connect the object!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
int nError=select(1,0,&FDSET,0,0);
if(nError<=0){
sprintf(szTmp,"Failed to select socket!,error no:%d",::WSAGetLastError());
closesocket(m_Socket);
::WSACleanup();
return;}
}
//5.傳送資料
int nLen=TxtEdit->Text.Length();
if(nLen>4096){
sprintf(szTmp,"The buffer is too size to send,it shoud not be more than 4096 bytes!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
strncpy(buf,TxtEdit->Text.c_str(),nLen);
switch(m_Protocol)
{
case TCP:
nSize=send(m_Socket,buf,nLen,0);
//ShowMessage(nSize);
break;
case UDP:
nSize=sendto(m_Socket,buf,nLen,0,(struct sockaddr*)&To,sizeof(struct sockaddr));
//ShowMessage(nSize);
break;
}
if(::WSAAsyncSelect(m_Socket,Handle,WM_SOCK,FD_READ|FD_CLOSE)){
sprintf(szTmp,"Failed to register socket event!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;}
}
void __fastcall TLANForm::FormDestroy(TObject *Sender)//斷開連線
{
closesocket(m_Socket);
::WSACleanup();
}
Socket api Server:
.h File
#ifndef UDPServerH
#define UDPServerH
#include
#include
#include
#include
#include
#include "WinSock.h"
/*#define WM_SOCKET WM_USER+1000
#define INITSOCKETSUCCESS 0
#define INITSOCKETFAILURE 1
#define CREATELISTENSOCKETSUCCESS 2
#define CREATELISTENSOCKETFAILURE 3
#define SETLISTENSOCKETSUCCESS 4
#define SETLISTENSOCKETFAILURE 5
#define BINDLISTENSOCKETSUCCESS 6
#define BINDLISTENSOCKETFAILURE 7
#define LISTENSOCKETSUCCESS 8
#define LISTENSOCKETFAILURE 9
#define ACCEPTSOCKETSUCCESS 10
#define ACCEPTSOCKETFAILURE 11
*/
class TPSTNForm : public TForm
{
__published: // IDE-managed Components
TEdit *Port;
TLabel *Label1;
TMemo *Memo1;
TButton *Button1;
TButton *Button2;
TLabel *Label2;
TComboBox *Prot;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TPSTNForm(TComponent* Owner);
};
enum PROTO {TCP,UDP};
class TCommunication:public TThread //Communication Thread
{
private:
SOCKET m_AcceptSocket;
char szTmp[256];//ERROR MESSAGE
public:
__fastcall TCommunication(SOCKET m_Socket,bool CreateSuspended);
__fastcall ~TCommunication();
protected:
virtual void __fastcall Execute();
};
class TListenThread:public TThread //Listen Thread
{
private:
WSAData wsaData;
struct sockaddr_in server;
fd_set FDS;
UINT m_Port;
PROTO m_Protocol;
char szTmp[256];//Error Message
public:
SOCKET m_Socket;
void __fastcall DoError();
void __fastcall InitSocket();
void __fastcall CreateListenSocket();
void __fastcall SetListenSocket();
void __fastcall BindListenSocket();
void __fastcall ListenSocket();
public:
__fastcall TListenThread(PROTO m_ProtocolA,UINT m_PortA,bool CreateSuspended);
virtual __fastcall ~TListenThread();
protected:
virtual void __fastcall Execute();
};
extern PACKAGE TPSTNForm *PSTNForm;
#endif
.cpp File
#include
#pragma hdrstop
#include "UDPServer.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TPSTNForm *PSTNForm;
TListenThread *pThread=0;
//******************************CLASS TCommunication For TCP****************************************************
__fastcall TCommunication::TCommunication(SOCKET m_Socket,bool CreateSuspended):TThread(FALSE)
{
m_AcceptSocket=m_Socket;
szTmp[0]='';
FreeOnTerminate=true;
}
__fastcall TCommunication::~TCommunication()
{
// closesocket(m_AcceptSocket);
}
void __fastcall TCommunication::Execute()
{
char buf[4096];
int nSize=0;
nSize=recv(m_AcceptSocket,(char FAR*)buf,4096,0);
if(nSize>0)
{
buf[nSize]='';
//Display
PSTNForm->Memo1->Lines->Add("Received Length:"+String(nSize));
PSTNForm->Memo1->Lines->Add("Received:"+StrPas(buf));
//Deliver
::Sleep(100);
send(m_AcceptSocket,buf,nSize,0);
}
}
//******************************CLASS TListenThread*****************************************************
__fastcall TListenThread::TListenThread(PROTO m_ProtocolA,UINT m_PortA,bool CreateSuspended):TThread(FALSE)
{
m_Socket=INVALID_SOCKET;
m_Port=m_PortA;
m_Protocol=m_ProtocolA;
szTmp[0]='';
::ZeroMemory(&wsaData,sizeof(WSAData));
::ZeroMemory(&server,sizeof(struct sockaddr_in));
FreeOnTerminate=TRUE;//Automatically delete while terminating.
}
__fastcall TListenThread::~TListenThread()
{
closesocket(m_Socket);
::WSACleanup();
m_Socket=INVALID_SOCKET;
m_Port=0;
m_Protocol=TCP;
szTmp[0]='';
::ZeroMemory(&wsaData,sizeof(WSAData));
::ZeroMemory(&server,sizeof(struct sockaddr_in));
}
void __fastcall TListenThread::DoError()
{
if(m_Socket!=INVALID_SOCKET) closesocket(m_Socket);
WSACleanup();
return;
}
void __fastcall TListenThread::InitSocket()
{
WORD version=MAKEWORD(2,0);
if(::WSAStartup(version,&wsaData)){
sprintf(szTmp,"Failed to intiailize socket,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::CreateListenSocket()
{
switch(m_Protocol)
{
case UDP:
m_Socket=socket(AF_INET,SOCK_DGRAM,0);
break;
case TCP:
m_Socket=socket(AF_INET,SOCK_STREAM,0);
break;
default:
sprintf(szTmp,"Error protocol!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
break;
}
if(m_Socket==INVALID_SOCKET){
sprintf(szTmp,"Failed to create socket!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::SetListenSocket()
{
server.sin_family=AF_INET;
server.sin_port=htons(m_Port);
server.sin_addr.S_un.S_addr=INADDR_ANY;
int NewOpenType=SO_SYNCHRONOUS_NONALERT;
if(setsockopt(INVALID_SOCKET,SOL_SOCKET,SO_OPENTYPE,(char*)&NewOpenType,4)){
sprintf(szTmp,"Set socket option error,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::BindListenSocket()
{
if(bind(m_Socket,(sockaddr*)&server,sizeof(struct sockaddr_in))){
sprintf(szTmp,"Failed to bind socket,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::ListenSocket()
{
if(listen(m_Socket,SOMAXCONN)){
sprintf(szTmp,"listen error,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
//Determine whether there is any connection
FD_ZERO(&FDS);
FD_SET(m_Socket,&FDS);
}
void __fastcall TListenThread::Execute()
{
char buf[4096];
struct sockaddr_in from; //for UDP
int nLen=sizeof(from),nSize=0; //for UDP
InitSocket();
CreateListenSocket();
SetListenSocket();
BindListenSocket();
if(m_Protocol==UDP){
while(!Terminated){
int nSize=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLen);
if(nSize>0){
buf[nSize]='';
PSTNForm->Memo1->Lines->Add("Received Length:"+String(nSize));
PSTNForm->Memo1->Lines->Add("Received:"+StrPas(buf));
::Sleep(100);
sendto(m_Socket,buf,nSize,0,(struct sockaddr*)&from,sizeof(struct sockaddr_in));
}
else return;
}
}
ListenSocket();
struct sockaddr_in client;
int nLength=sizeof(struct sockaddr_in);
while(!Terminated){
int nError=select(1,&FDS,0,0,0);
if(nError<=0) Terminate();
SOCKET m_AcceptSocket=accept(m_Socket,(struct sockaddr*)&client,&nLength);
if(m_AcceptSocket==INVALID_SOCKET){
sprintf(szTmp,"Failed to execute accept,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
Terminate();
return;
}
TCommunication *pCThread=new TCommunication(m_AcceptSocket,FALSE);
pCThread->Terminate();
pCThread->WaitFor();
}
}
//************************PSTNForm*********************************************//
__fastcall TPSTNForm::TPSTNForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TPSTNForm::Button1Click(TObject *Sender)
{
Close();
}
void __fastcall TPSTNForm::Button2Click(TObject *Sender)
{
if(pThread){
pThread->Suspend();
pThread->Terminate();
delete pThread;
pThread=0;
}
UINT m_Port;
try
{
m_Port=Port->Text.ToInt();
}
catch(Exception &e)
{
::MessageBox(0,e.Message.c_str(),"Error",MB_OK+MB_ICONERROR);
return;
}
PROTO m_Protocol;
switch(Prot->ItemIndex)
{
case 0:
m_Protocol=TCP;
break;
case 1:
m_Protocol=UDP;
break;
default:
break;
}
pThread=new TListenThread(m_Protocol,m_Port,FALSE);
//pThread->Terminate();
}
void __fastcall TPSTNForm::FormCreate(TObject *Sender)
{
::SendMessage(Prot->Handle,CB_SETCURSEL,0,1);
}
void __fastcall TPSTNForm::FormDestroy(TObject *Sender)
{
if(pThread){
pThread->Suspend();
pThread->Terminate();}
} [@more@]
原文: 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
#include
#include
#include
#include
#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
#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協議型別
SOCKET m_Socket=INVALID_SOCKET; //定義一個socket物件
PROTO m_Protocol=TCP; //socket型別變數
__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]='';
TxtEdit->Text="Received Length:"+String(nLen)+"rn"+StrPas(buf);
}
break;
case UDP:
nLen=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLength);
if(nLen>0){
buf[nLen]='';
TxtEdit->Text="Received Length:"+String(nLen)+"rn"+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()){//IP為空結束
::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){//IP地址無效
::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;}
//1.初始化Winsock
WSAData wsaData;
::ZeroMemory(&wsaData,sizeof(WSAData)); //0填充
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);
if(!he){
sprintf(szTmp,"Failed to get information to host!","Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//2.建立新socket
m_Socket=INVALID_SOCKET;
switch(m_Protocol)
{
case TCP:
m_Socket=socket(AF_INET,SOCK_STREAM,0);
break;
case UDP:
m_Socket=socket(AF_INET,SOCK_DGRAM,0);
break;
}
if(m_Socket==INVALID_SOCKET){//建立socket失敗
sprintf(szTmp,"Failed to create a new socket!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//3.繫結SOCKET
struct sockaddr_in client;
unsigned long nClient;
memcpy(&nClient,he->h_addr_list[0],sizeof(int));
if(nClient==INADDR_NONE){
sprintf(szTmp,"Failed to obtain the local machine's IP!","Error",MB_OK+MB_ICONERROR);
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
client.sin_family=AF_INET;
client.sin_port=0;
client.sin_addr.S_un.S_addr=(int)nClient;
if(bind(m_Socket,(struct sockaddr*)&client,sizeof(struct sockaddr))){
sprintf(szTmp,"Failed to bind socket!","Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;}
struct sockaddr_in To;
To.sin_family=AF_INET;
To.sin_port=htons(m_Port);
To.sin_addr.S_un.S_addr=(int)nAddr;
fd_set FDSET;
FD_ZERO(&FDSET);
FD_SET(m_Socket,&FDSET);
//4.連線遠端主機
if(m_Protocol==TCP){
if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr))){
sprintf(szTmp,"Failed to connect the object!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
int nError=select(1,0,&FDSET,0,0);
if(nError<=0){
sprintf(szTmp,"Failed to select socket!,error no:%d",::WSAGetLastError());
closesocket(m_Socket);
::WSACleanup();
return;}
}
//5.傳送資料
int nLen=TxtEdit->Text.Length();
if(nLen>4096){
sprintf(szTmp,"The buffer is too size to send,it shoud not be more than 4096 bytes!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;
}
strncpy(buf,TxtEdit->Text.c_str(),nLen);
switch(m_Protocol)
{
case TCP:
nSize=send(m_Socket,buf,nLen,0);
//ShowMessage(nSize);
break;
case UDP:
nSize=sendto(m_Socket,buf,nLen,0,(struct sockaddr*)&To,sizeof(struct sockaddr));
//ShowMessage(nSize);
break;
}
if(::WSAAsyncSelect(m_Socket,Handle,WM_SOCK,FD_READ|FD_CLOSE)){
sprintf(szTmp,"Failed to register socket event!,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
closesocket(m_Socket);
::WSACleanup();
return;}
}
void __fastcall TLANForm::FormDestroy(TObject *Sender)//斷開連線
{
closesocket(m_Socket);
::WSACleanup();
}
Socket api Server:
.h File
#ifndef UDPServerH
#define UDPServerH
#include
#include
#include
#include
#include
#include "WinSock.h"
/*#define WM_SOCKET WM_USER+1000
#define INITSOCKETSUCCESS 0
#define INITSOCKETFAILURE 1
#define CREATELISTENSOCKETSUCCESS 2
#define CREATELISTENSOCKETFAILURE 3
#define SETLISTENSOCKETSUCCESS 4
#define SETLISTENSOCKETFAILURE 5
#define BINDLISTENSOCKETSUCCESS 6
#define BINDLISTENSOCKETFAILURE 7
#define LISTENSOCKETSUCCESS 8
#define LISTENSOCKETFAILURE 9
#define ACCEPTSOCKETSUCCESS 10
#define ACCEPTSOCKETFAILURE 11
*/
class TPSTNForm : public TForm
{
__published: // IDE-managed Components
TEdit *Port;
TLabel *Label1;
TMemo *Memo1;
TButton *Button1;
TButton *Button2;
TLabel *Label2;
TComboBox *Prot;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TPSTNForm(TComponent* Owner);
};
enum PROTO {TCP,UDP};
class TCommunication:public TThread //Communication Thread
{
private:
SOCKET m_AcceptSocket;
char szTmp[256];//ERROR MESSAGE
public:
__fastcall TCommunication(SOCKET m_Socket,bool CreateSuspended);
__fastcall ~TCommunication();
protected:
virtual void __fastcall Execute();
};
class TListenThread:public TThread //Listen Thread
{
private:
WSAData wsaData;
struct sockaddr_in server;
fd_set FDS;
UINT m_Port;
PROTO m_Protocol;
char szTmp[256];//Error Message
public:
SOCKET m_Socket;
void __fastcall DoError();
void __fastcall InitSocket();
void __fastcall CreateListenSocket();
void __fastcall SetListenSocket();
void __fastcall BindListenSocket();
void __fastcall ListenSocket();
public:
__fastcall TListenThread(PROTO m_ProtocolA,UINT m_PortA,bool CreateSuspended);
virtual __fastcall ~TListenThread();
protected:
virtual void __fastcall Execute();
};
extern PACKAGE TPSTNForm *PSTNForm;
#endif
.cpp File
#include
#pragma hdrstop
#include "UDPServer.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TPSTNForm *PSTNForm;
TListenThread *pThread=0;
//******************************CLASS TCommunication For TCP****************************************************
__fastcall TCommunication::TCommunication(SOCKET m_Socket,bool CreateSuspended):TThread(FALSE)
{
m_AcceptSocket=m_Socket;
szTmp[0]='';
FreeOnTerminate=true;
}
__fastcall TCommunication::~TCommunication()
{
// closesocket(m_AcceptSocket);
}
void __fastcall TCommunication::Execute()
{
char buf[4096];
int nSize=0;
nSize=recv(m_AcceptSocket,(char FAR*)buf,4096,0);
if(nSize>0)
{
buf[nSize]='';
//Display
PSTNForm->Memo1->Lines->Add("Received Length:"+String(nSize));
PSTNForm->Memo1->Lines->Add("Received:"+StrPas(buf));
//Deliver
::Sleep(100);
send(m_AcceptSocket,buf,nSize,0);
}
}
//******************************CLASS TListenThread*****************************************************
__fastcall TListenThread::TListenThread(PROTO m_ProtocolA,UINT m_PortA,bool CreateSuspended):TThread(FALSE)
{
m_Socket=INVALID_SOCKET;
m_Port=m_PortA;
m_Protocol=m_ProtocolA;
szTmp[0]='';
::ZeroMemory(&wsaData,sizeof(WSAData));
::ZeroMemory(&server,sizeof(struct sockaddr_in));
FreeOnTerminate=TRUE;//Automatically delete while terminating.
}
__fastcall TListenThread::~TListenThread()
{
closesocket(m_Socket);
::WSACleanup();
m_Socket=INVALID_SOCKET;
m_Port=0;
m_Protocol=TCP;
szTmp[0]='';
::ZeroMemory(&wsaData,sizeof(WSAData));
::ZeroMemory(&server,sizeof(struct sockaddr_in));
}
void __fastcall TListenThread::DoError()
{
if(m_Socket!=INVALID_SOCKET) closesocket(m_Socket);
WSACleanup();
return;
}
void __fastcall TListenThread::InitSocket()
{
WORD version=MAKEWORD(2,0);
if(::WSAStartup(version,&wsaData)){
sprintf(szTmp,"Failed to intiailize socket,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::CreateListenSocket()
{
switch(m_Protocol)
{
case UDP:
m_Socket=socket(AF_INET,SOCK_DGRAM,0);
break;
case TCP:
m_Socket=socket(AF_INET,SOCK_STREAM,0);
break;
default:
sprintf(szTmp,"Error protocol!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
break;
}
if(m_Socket==INVALID_SOCKET){
sprintf(szTmp,"Failed to create socket!");
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::SetListenSocket()
{
server.sin_family=AF_INET;
server.sin_port=htons(m_Port);
server.sin_addr.S_un.S_addr=INADDR_ANY;
int NewOpenType=SO_SYNCHRONOUS_NONALERT;
if(setsockopt(INVALID_SOCKET,SOL_SOCKET,SO_OPENTYPE,(char*)&NewOpenType,4)){
sprintf(szTmp,"Set socket option error,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::BindListenSocket()
{
if(bind(m_Socket,(sockaddr*)&server,sizeof(struct sockaddr_in))){
sprintf(szTmp,"Failed to bind socket,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
}
void __fastcall TListenThread::ListenSocket()
{
if(listen(m_Socket,SOMAXCONN)){
sprintf(szTmp,"listen error,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
return;
}
//Determine whether there is any connection
FD_ZERO(&FDS);
FD_SET(m_Socket,&FDS);
}
void __fastcall TListenThread::Execute()
{
char buf[4096];
struct sockaddr_in from; //for UDP
int nLen=sizeof(from),nSize=0; //for UDP
InitSocket();
CreateListenSocket();
SetListenSocket();
BindListenSocket();
if(m_Protocol==UDP){
while(!Terminated){
int nSize=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLen);
if(nSize>0){
buf[nSize]='';
PSTNForm->Memo1->Lines->Add("Received Length:"+String(nSize));
PSTNForm->Memo1->Lines->Add("Received:"+StrPas(buf));
::Sleep(100);
sendto(m_Socket,buf,nSize,0,(struct sockaddr*)&from,sizeof(struct sockaddr_in));
}
else return;
}
}
ListenSocket();
struct sockaddr_in client;
int nLength=sizeof(struct sockaddr_in);
while(!Terminated){
int nError=select(1,&FDS,0,0,0);
if(nError<=0) Terminate();
SOCKET m_AcceptSocket=accept(m_Socket,(struct sockaddr*)&client,&nLength);
if(m_AcceptSocket==INVALID_SOCKET){
sprintf(szTmp,"Failed to execute accept,error no:%d",::WSAGetLastError());
::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
DoError();
Terminate();
return;
}
TCommunication *pCThread=new TCommunication(m_AcceptSocket,FALSE);
pCThread->Terminate();
pCThread->WaitFor();
}
}
//************************PSTNForm*********************************************//
__fastcall TPSTNForm::TPSTNForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TPSTNForm::Button1Click(TObject *Sender)
{
Close();
}
void __fastcall TPSTNForm::Button2Click(TObject *Sender)
{
if(pThread){
pThread->Suspend();
pThread->Terminate();
delete pThread;
pThread=0;
}
UINT m_Port;
try
{
m_Port=Port->Text.ToInt();
}
catch(Exception &e)
{
::MessageBox(0,e.Message.c_str(),"Error",MB_OK+MB_ICONERROR);
return;
}
PROTO m_Protocol;
switch(Prot->ItemIndex)
{
case 0:
m_Protocol=TCP;
break;
case 1:
m_Protocol=UDP;
break;
default:
break;
}
pThread=new TListenThread(m_Protocol,m_Port,FALSE);
//pThread->Terminate();
}
void __fastcall TPSTNForm::FormCreate(TObject *Sender)
{
::SendMessage(Prot->Handle,CB_SETCURSEL,0,1);
}
void __fastcall TPSTNForm::FormDestroy(TObject *Sender)
{
if(pThread){
pThread->Suspend();
pThread->Terminate();}
} [@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16396910/viewspace-1034599/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在C++ Builder中用socket api來寫網路通訊程式(同時支援TCP和UDP協議) (轉)C++UIAPITCPUDP協議
- 通訊協議:HTTP、TCP、UDP協議HTTPTCPUDP
- UDP協議網路Socket程式設計(java實現C/S通訊案例)UDP協議程式設計Java
- 網路通訊協議-TCP協議詳解!協議TCP
- 用C++ Builder中的TServerSocket,TClientSocket來寫網路通訊程式 (轉)C++UIServerclient
- TCP和UDP協議TCPUDP協議
- Java:基於TCP協議網路socket程式設計(實現C/S通訊)JavaTCP協議程式設計
- Android程式設計師必知必會的網路通訊傳輸層協議——UDP和TCPAndroid程式設計師協議UDPTCP
- Android與物聯網裝置通訊-UDP&TCP協議AndroidUDPTCP協議
- Socket,TCP,UDP,HTTP基本通訊原理和OC版本DemoTCPUDPHTTP
- 【網路協議】UDP協議協議UDP
- TCP 和 UDP 協議簡介TCPUDP協議
- [面試∙網路] TCP/IP(四):TCP 與 UDP 協議簡介面試TCPUDP協議
- 基於TCP/UDP的Socket程式設計,HTTP/HTTPS協議TCPUDP程式設計HTTP協議
- Socket:UDP協議小白UDP協議
- 網路協議之:socket協議詳解之Socket和Stream Socket協議
- Visual C++設計UDP協議通訊示例C++UDP協議
- 網路通訊協議協議
- 傳輸層協議 TCP 和 UDP協議TCPUDP
- DNS何時使用TCP與UDP協議?DNSTCPUDP協議
- 基於UDP協議的Socket網路程式設計UDP協議程式設計
- TCP協議之網路延時TCP協議
- 網路程式設計協議(TCP和UDP協議,黏包問題)以及socketserver模組程式設計協議TCPUDPServer
- 通過故事引申網路協議TCP協議TCP
- 通訊協議和網路協議有什麼區別協議
- TCP/IP、UDP/IP協議TCPUDP協議
- UDP 和 TCP 兩種協議簡介UDPTCP協議
- 網路通訊協議-ICMP協議詳解!協議
- 網路通訊協議-HTTP協議詳解!協議HTTP
- 網路通訊協議-SMTP協議詳解!協議
- udp網路通訊UDP
- 即時通訊技術文集(第10期):IM通訊協議該選TCP還是UDP [共12篇]協議TCPUDP
- 基於TCP協議的Socket網路程式設計( )TCP協議程式設計
- 快速理解網路通訊協議協議
- 網路協議之:socket協議詳解之Datagram Socket協議
- 【網路協議】TCP協議簡介協議TCP
- 終於懂了TCP和UDP協議區別TCPUDP協議
- 淺談TCP和UDP協議的區別TCPUDP協議