ACE multi-line TCP server-client communication

Source: Internet
Author: User

The implementation of the function is the information sent by each client, through the server, can be forwarded to all others to see.

The premise is that everyone is connected to the server.


The methods adopted are:

(1) When the server detects a connection, it opens a thread to handle the connection

(2) The client opens two threads, one thread is responsible for sending the message, and the other is responsible for receiving the message.

All code is implemented in the ACE framework:


The following is the server-side code

#include "stdafx.h" #include "ace/sock_acceptor.h" #include "ace/sock_stream.h" #include "ace/inet_addr.h" #includ E "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")/************ server-side Programs **************** * * Using the ACE Framework for simple clothing  Service * Feedback client program, can only handle a connection * ACE source code saved in D:/ace_wrappers * environment variable and compiled add library * also designed * *****************************/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); /* Get the IP and port number of the client/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)//Read data sent by client {//cout<< "in Thread1" <<endl;
			Buffer[bytes_received]= ' ";
			String Str=buffer;
			cout<<str<<endl;
			int i=0;    for (i=0;i<5;i++) {//peer[i].send (Str.c_str (), str.length ());
			Data Peer[i].send (buffer,bytes_received) is sent to the client;
		}//sleep (10);  
	} peer1.close ();                                             int _tmain (int argc, _tchar* argv[]) {ace::init ();  
    /*ace initialization * * Wsadata wsadata;  
	if (WSAStartup (Makeword (2,0), &wsadata)!= 0) {ace_os::p rintf ("Start failed");                   } ace_inet_addr Port_to_listen (6000);                              /* Port Object * * Ace_sock_acceptor Acceptor1; /* Connector */if (Acceptor1.open (port_to_listen,1) ==-1)/* Open listening Port/{cout<<endl&  
        lt;< "Fail" <<endl;  
        System ("pause");  
    return-1; } cout<<endl<< "Bind Port Success "<<endl; 
	/* Open listening Port successfully/Ace_time_value TIMEOUT1 (10,0);
	int i=0;
    HANDLE Hthread; while (1) {if (Acceptor1.accept (Peer[i])!=-1)/* Connection succeeded/{cout<<endl&  
			lt;<endl<< "Client Connect" <<endl;
			Hthread= (HANDLE) _beginthreadex (0,0, unsigned (__stdcall*) (void *)) Mythread1, (LPVOID) &peer[i],0,0);
					if (hthread!=null) {cout<< "connected successfully, create thread succeeded" <<endl;
				i++;  
}} return 0; } <strong> </strong>
Here is the client code

#include "stdafx.h" #include <ace/SOCK_Stream.h> #include <ace/SOCK_Connector.h> #include <ace/inet_a ddr.h> #include <ace/Time_Value.h> #include <ace/os.h> #include <string> #include <iostream  
> Using namespace std;
#pragma comment (lib, "Aced.lib")/************ client program **************** * * Constantly requesting connections, knowing that success * then sends data and prints * programs received from the server * * Client should be two threads, one to handle receiving * one to handle sending ******************************/unsigned __stdcall CLIENTRCV (lpvoid Arg) {ACE_SOCK_STR
	EAM peer1=* (Ace_sock_stream *) arg;  
	Char buffer[1024]; 
	ssize_t rcvbytes=0;
            while (1) {if (Rcvbytes=peer1.recv (buffer,sizeof (buffer))!=-1)/received data {buffer[rcvbytes]= ';
			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<< "Please enter a paragraph of text" <<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::p rintf ("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;   }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.