ACE 多線TCP服務端-用戶端通訊

來源:互聯網
上載者:User

實現的功能是每一個用戶端發送的資訊,通過伺服器,都可以轉寄給其它所有人看到。

前提是所有人都串連到伺服器。


採取的方法是:

(1)伺服器檢測到串連,就開啟一個線程,應對這個串連

(2)用戶端開啟兩個線程,一個線程負責發送訊息,另外一個負責接收訊息。

所有代碼在ACE架構中實現:


下面是伺服器端的代碼

#include "stdafx.h"  #include  "ace/SOCK_Acceptor.h" #include  "ace/SOCK_Stream.h"#include  "ace/INET_Addr.h"  #include  "ace/OS.H" #include "ace/Thread.h" #include "ace/Synch.h" #include <string>  #include <iostream>  using namespace std;    #pragma comment(lib,"aced.lib")  #pragma comment(lib,"ace.lib") /************ 服務端程式  ****************** 使用ACE架構進行簡單的伺服器* 反饋用戶端程式,只能處理一個串連* ACE源碼儲存在D:/ACE_wrappers* 環境變數以及編譯的添加庫* 也據此設計******************************/  ACE_SOCK_Stream peer[5];  unsigned __stdcall mythread1(LPVOID arg){char buffer[1024];  SSIZE_T bytes_received;  ACE_INET_Addr addr; ACE_SOCK_Stream peer1=*((ACE_SOCK_Stream*)arg);peer1.get_remote_addr(addr);                                                          /* 獲得用戶端的ip以及連接埠號碼   */cout<<endl<<"mythread one\t"<<addr.get_host_name()<<"\t"<<addr.get_port_number()<<endl;  while(1){while ((bytes_received =peer1.recv (buffer, sizeof(buffer))) != -1)                  //讀取用戶端發送的資料  {  //cout<<"in thread1"<<endl;buffer[bytes_received]='\0';string str=buffer;cout<<str<<endl;int i=0;for(i=0;i<5;i++){//peer[i].send(str.c_str(), str.length());    //對用戶端發資料  peer[i].send(buffer,bytes_received);}//Sleep(10);} peer1.close ();  }}int _tmain(int argc, _TCHAR* argv[])  {    ACE::init();                                             /*ACE 初始化*/    WSADATA   wsaData;      if(WSAStartup( MAKEWORD(2,0), &wsaData) != 0){        ACE_OS::printf("start failed");  }    ACE_INET_Addr       port_to_listen(6000);                   /* 連接埠對象 */    ACE_SOCK_Acceptor   acceptor1;                              /* 連接器 */    if (acceptor1.open(port_to_listen,1)==-1)                   /* 開啟監聽連接埠   */    {          cout<<endl<<"first bind port fail"<<endl;          system("pause");          return -1;      }  cout<<endl<<"bind port success"<<endl;                     /* 開啟監聽連接埠成功  */ACE_Time_Value timeout1(10,0); int i=0;HANDLE hthread;    while (1)      {  if(acceptor1.accept(peer[i])!=-1)                            /*  串連成功  */{  cout<<endl<<endl<<"client connect"<<endl;  hthread=(HANDLE)_beginthreadex(0,0,(unsigned(__stdcall*)(void *))mythread1,(LPVOID)&peer[i],0,0);if(hthread!=NULL){cout<<"串連成功,建立線程成功"<<endl;i++;}}} return 0;  } <strong></strong>
下面是用戶端代碼

#include "stdafx.h"  #include <ace/SOCK_Stream.h>  #include <ace/SOCK_Connector.h>  #include <ace/INET_Addr.h>  #include <ace/Time_Value.h>  #include <ace/os.h> #include <string>  #include <iostream>  using namespace std;  #pragma comment(lib,"aced.lib")    /************ 用戶端程式  ***************** * 不斷請求串連,知道成功* 然後發送資料,並且列印* 從服務端接收到的程式** 用戶端應該是兩個線程,一個用來處理接收* 一個用來處理髮送******************************/unsigned __stdcall ClientRcv(LPVOID arg){ACE_SOCK_Stream peer1=*(ACE_SOCK_Stream *)arg;char buffer[1024];  SSIZE_T rcvbytes=0; while(1){if((rcvbytes=peer1.recv(buffer,sizeof(buffer)))!=-1) //接收到資料{buffer[rcvbytes]='\0';            string str=buffer;cout<<str<<endl;system("gnome-terminal"); }}}unsigned __stdcall ClientSend(LPVOID arg){ACE_SOCK_Stream peer1=*(ACE_SOCK_Stream *)arg;char buffer[1024];  SSIZE_T rcvbytes; while(1){          cout<<"請輸入一段文字"<<endl;  string str;  cin>>str;  peer1.send(str.c_str(),str.length()); }}int _tmain(int argc, _TCHAR* argv[])  {      WSADATA wsaData;      if( WSAStartup( MAKEWORD(2,0), &wsaData) != 0)  {        ACE_OS::printf("start failed");  }      ACE_INET_Addr addr(6000,"127.0.0.1");      ACE_SOCK_Connector connector;      ACE_Time_Value timeout(5,0);      ACE_SOCK_Stream peer;  char buffer[1024];  SSIZE_T bytes_received;  while(connector.connect(peer,addr,&timeout)!=0){cout<<"connection failed!"<<endl;  }cout<<"connected"<<endl;  HANDLE sendhandle=(HANDLE)_beginthreadex(0,0,(unsigned(__stdcall *)(void *))ClientRcv,&peer,0,0);if(sendhandle==0){cout<<"create sendthread fail"<<endl;}sendhandle=(HANDLE)_beginthreadex(0,0,(unsigned(__stdcall *)(void *))ClientSend,&peer,0,0);if(sendhandle==0){cout<<"create sendthread fail"<<endl;}while(1);  system("pause");    return 0;  }  



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.