wxwidgets应用手记(5) TCP/IP操作

贴两个主要的类:

winsockclient.h

   1: #ifndef WINSOCKCLIENT_H
   2: #define WINSOCKCLIENT_H
   3:  
   4: #include <wx/event.h>
   5: #include <wx/socket.h>
   6:  
   7: namespace ty
   8: {
   9:     class IWinsockClient
  10:     {
  11:     public:
  12:         virtual void Connected(wxObject*) = 0;
  13:         virtual void Disconnected(wxObject* ,bool server_closed/* 断开的原因,服务器关闭=true */) = 0;
  14:         virtual void Received(wxObject* ,char*,int) = 0;
  15:     };
  16:  
  17:     class CWinsockClient:wxEvtHandler
  18:     {
  19:     public:
  20:         void Connect(wxString ip,long port);
  21:         void Disconnect();
  22:         void Send(const void* buf,int len);
  23:         bool IsConnected();
  24:  
  25:     private:
  26:         void OnSocketEvent(wxSocketEvent& event);
  27:  
  28:     public:
  29:         CWinsockClient(IWinsockClient* owner);
  30:         virtual ~CWinsockClient(void);
  31:  
  32:     private:
  33:         wxString m_ip;
  34:         unsigned short m_port;
  35:         wxSocketBase* m_sock;
  36:         IWinsockClient* m_owner;
  37:  
  38:     private:
  39:         static const long ID_WinsockClient;
  40:     };
  41: };
  42:  
  43: #endif // WINSOCKCLIENT_H

winsockserver.h

   1: #ifndef WINSOCKSERVER_H
   2: #define WINSOCKSERVER_H
   3:  
   4: #include <wx/event.h>
   5: #include <wx/socket.h>
   6: #include <wx/dynarray.h>
   7:  
   8: namespace ty
   9: {
  10:     class IWinsockServer
  11:     {
  12:     public:
  13:         virtual void Connected(wxObject*,wxSocketBase*) = 0;
  14:         virtual void Disconnected(wxObject*,wxSocketBase*) = 0;
  15:         virtual void Received(wxObject*,wxSocketBase*,char*,int) = 0;
  16:     };
  17:  
  18:     class CWinsockServer:wxEvtHandler
  19:     {
  20:     public:
  21:         CWinsockServer(IWinsockServer* owner);
  22:         virtual ~CWinsockServer();
  23:  
  24:     public:
  25:         void Start(long port);
  26:         void Stop();
  27:         bool IsListenning();
  28:         int GetClientCount();
  29:         wxSocketBase* GetClientItem(int index);
  30:         wxString GetClientAddressInfo(int index);
  31:  
  32:     private:
  33:         void OnServerEvent(wxSocketEvent& event);
  34:         void OnSocketEvent(wxSocketEvent& event);
  35:  
  36:     private:
  37:         unsigned short m_port;
  38:         wxSocketServer* m_server;
  39:         wxArrayPtrVoid m_clientList;
  40:         IWinsockServer* m_owner;
  41:  
  42:     private:
  43:         static const long ID_WinsockServer;
  44:         static const long ID_WinsockClient;
  45:  
  46:     };
  47: };
  48:  
  49: #endif // WINSOCKSERVER_H

winsockclient.cpp

   1: #include "winsockclient.h"
   2: using namespace ty;
   3:  
   4: const long CWinsockClient::ID_WinsockClient = wxNewId();
   5:  
   6: CWinsockClient::CWinsockClient(IWinsockClient* owner)
   7: {
   8:     wxASSERT(owner != NULL);
   9:  
  10:     m_sock = NULL;
  11:     m_owner = owner;
  12:  
  13:     wxEvtHandler::Connect(ID_WinsockClient,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockClient::OnSocketEvent);
  14: }
  15:  
  16: CWinsockClient::~CWinsockClient(void)
  17: {
  18:     Disconnect();
  19:     wxEvtHandler::Disconnect(ID_WinsockClient,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockClient::OnSocketEvent);
  20: }
  21:  
  22: void CWinsockClient::OnSocketEvent(wxSocketEvent& event)
  23: {
  24:     wxSocketBase* sock = event.GetSocket();
  25:     switch(event.GetSocketEvent())
  26:     {
  27:     case wxSOCKET_CONNECTION:
  28:     {
  29:         m_sock = sock;
  30:         m_owner->Connected(this);
  31:  
  32:         break;
  33:     }
  34:  
  35:     case wxSOCKET_INPUT:
  36:     {
  37:         char buf[4096];
  38:         sock->Read(buf,sizeof(buf));
  39:         m_owner->Received(this,buf,sock->LastCount());
  40:  
  41:         break;
  42:     }
  43:  
  44:     case wxSOCKET_LOST:
  45:     {
  46:         m_owner->Disconnected(this,true);
  47:  
  48:         sock->Destroy();
  49:         m_sock = NULL;
  50:  
  51:         break;
  52:     }
  53:     }
  54: }
  55:  
  56: void CWinsockClient::Connect(wxString ip,long port)
  57: {
  58:     if(m_sock == NULL)
  59:     {
  60:         this->m_ip = ip;
  61:         this->m_port = port;
  62:  
  63:         wxIPV4address addr;
  64:         addr.Hostname(m_ip);
  65:         addr.Service(m_port);
  66:  
  67:         wxSocketClient* sock = new wxSocketClient();
  68:         sock->SetEventHandler(*this,ID_WinsockClient);
  69:         sock->SetNotify(wxSOCKET_CONNECTION_FLAG |wxSOCKET_INPUT_FLAG |wxSOCKET_LOST_FLAG);
  70:         sock->Notify(true);
  71:         sock->Connect(addr,false);
  72:     }
  73: }
  74:  
  75: void CWinsockClient::Disconnect()
  76: {
  77:     if(m_sock != NULL)
  78:     {
  79:         m_owner->Disconnected(this,false);
  80:         m_sock->Destroy();
  81:         m_sock = NULL;
  82:     }
  83: }
  84:  
  85: void CWinsockClient::Send(const void* buf,int len)
  86: {
  87:     if(m_sock != NULL)
  88:     {
  89:         m_sock->Write(buf,len);
  90:     }
  91: }
  92:  
  93: bool CWinsockClient::IsConnected()
  94: {
  95:     return m_sock != NULL;
  96: }

winsockserver.cpp

   1: #include "winsockserver.h"
   2: using namespace ty;
   3:  
   4: const long CWinsockServer::ID_WinsockServer =wxNewId();
   5: const long CWinsockServer::ID_WinsockClient = wxNewId();
   6:  
   7:  
   8: CWinsockServer::CWinsockServer(IWinsockServer* owner)
   9: {
  10:     wxASSERT(owner != NULL);
  11:  
  12:     //ctor
  13:     this->m_owner = owner;
  14:     wxEvtHandler::Connect(ID_WinsockServer,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockServer::OnServerEvent);
  15:     wxEvtHandler::Connect(ID_WinsockClient,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockServer::OnSocketEvent);
  16:  
  17:     m_server = NULL;
  18:     m_clientList.clear();
  19: }
  20:  
  21: CWinsockServer::~CWinsockServer()
  22: {
  23:     //dtor
  24:     wxEvtHandler::Disconnect(ID_WinsockServer,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockServer::OnServerEvent);
  25:     wxEvtHandler::Disconnect(ID_WinsockClient,wxEVT_SOCKET,(wxObjectEventFunction)&CWinsockServer::OnSocketEvent);
  26:  
  27:     Stop();
  28:     for(unsigned int i=0; i< m_clientList.Count(); i++)
  29:     {
  30:         ((wxSocketBase*)m_clientList[i])->Destroy();
  31:     }
  32:     m_clientList.clear();
  33: }
  34:  
  35: void CWinsockServer::Start(long port)
  36: {
  37:     if(m_server == NULL)
  38:     {
  39:         m_port = port;
  40:  
  41:         wxIPV4address addr;
  42:         addr.Service(m_port);
  43:  
  44:         m_server = new wxSocketServer(addr);
  45:         m_server->SetEventHandler(*this,ID_WinsockServer);
  46:         m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
  47:         m_server->Notify(true);
  48:     }
  49: }
  50:  
  51: void CWinsockServer::Stop()
  52: {
  53:     if(m_server != NULL)
  54:     {
  55:         m_server->Close();
  56:         delete m_server;
  57:         m_server = NULL;
  58:     }
  59: }
  60:  
  61: bool CWinsockServer::IsListenning()
  62: {
  63:     return m_server != NULL;
  64: }
  65:  
  66: void CWinsockServer::OnServerEvent(wxSocketEvent& event)
  67: {
  68:     wxSocketBase* sock = m_server->Accept(false);
  69:  
  70:     sock->SetEventHandler(*this,ID_WinsockClient);
  71:     sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_LOST_FLAG);
  72:     sock->Notify(true);
  73:  
  74:     m_clientList.Add(sock);
  75:     m_owner->Connected(this,sock);
  76: }
  77:  
  78: void CWinsockServer::OnSocketEvent(wxSocketEvent& event)
  79: {
  80:     wxSocketBase* sock = event.GetSocket();
  81:     switch(event.GetSocketEvent())
  82:     {
  83:     case wxSOCKET_INPUT:
  84:     {
  85:         char buf[4096];
  86:         sock->Read(buf,sizeof(buf));
  87:         m_owner->Received(this,sock,buf,sock->LastCount());
  88:  
  89:         break;
  90:     }
  91:  
  92:     case wxSOCKET_LOST:
  93:     {
  94:         m_owner->Disconnected(this,sock);
  95:         m_clientList.Remove(sock);
  96:         sock->Destroy();
  97:  
  98:         break;
  99:     }
 100:     }
 101: }
 102:  
 103: int CWinsockServer::GetClientCount()
 104: {
 105:     return m_clientList.Count();
 106: }
 107:  
 108: wxSocketBase* CWinsockServer::GetClientItem(int index)
 109: {
 110:     return (wxSocketBase*)m_clientList[index];
 111: }
 112:  
 113: wxString CWinsockServer::GetClientAddressInfo(int index)
 114: {
 115:     wxIPV4address addr;
 116:     GetClientItem(index)->GetLocal(addr);
 117:     return wxString::Format(_T("%s:%d"),addr.IPAddress().c_str(),addr.Service());
 118: }

客户端主窗口实现接口 IWinsockClient

   1: class IWinsockClient
   2: {
   3: public:
   4:     virtual void Connected(wxObject*) = 0;
   5:     virtual void Disconnected(wxObject* ,bool server_closed/* 断开的原因,服务器关闭=true */) = 0;
   6:     virtual void Received(wxObject* ,char*,int) = 0;
   7: };

服务器主窗口实现接口 IWinsockServer

   1: class IWinsockServer
   2: {
   3: public:
   4:     virtual void Connected(wxObject*,wxSocketBase*) = 0;
   5:     virtual void Disconnected(wxObject*,wxSocketBase*) = 0;
   6:     virtual void Received(wxObject*,wxSocketBase*,char*,int) = 0;
   7: };
posted @ 2012-01-23 17:35  快乐驿站  阅读(1339)  评论(0编辑  收藏  举报