// ace_tcp_server.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <ace/SOCK_Acceptor.h>
#include <ace/SOCK_Stream.h>
#include <ace/INET_Addr.h>
#include <ace/OS.H>
#include <string>
#include <iostream>
using namespace std;
#pragma comment(lib,"aced.lib")
#pragma comment(lib,"ace.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if( WSAStartup( MAKEWORD(2,0), &wsaData) != 0)
ACE_OS::printf("start failed");
ACE_INET_Addr port_to_listen(6000);
ACE_SOCK_Acceptor acceptor;
if (acceptor.open(port_to_listen,1)==-1)
{
cout<<endl<<"bind port fail"<<endl;
system("pause");
return -1;
}
cout<<endl<<"bind port success"<<endl;
while (true)
{
ACE_SOCK_Stream peer;
ACE_Time_Value timeout(10,0);
if (acceptor.accept(peer)!=-1)
{
cout<<endl<<endl<<"client connect"<<endl;
char buffer[1024];
SSIZE_T bytes_received;
ACE_INET_Addr addr;
peer.get_local_addr(addr);
cout<<endl<<"local port\t"<<addr.get_host_name()<<"\t"<<addr.get_port_number()<<endl;
while ((bytes_received =
peer.recv (buffer, sizeof(buffer))) != -1) //读取客户端发送的数据
{
peer.send(buffer, bytes_received); //对客户端发数据
}
peer.close ();
}
}
return 0;
}
// ace_tcp.cpp : 定义控制台应用程序的入口点。
//
#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")
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;
if (connector.connect(peer,addr,&timeout)!=0)
{
cout<<"connection failed!"<<endl;
system("pause");
return -1;
}
cout<<"connected"<<endl;
string str="hello world";
peer.send(str.c_str(),str.length());
cout<<endl<<"send:\t"<<str<<endl;
SSIZE_T bc=0;
char buff[1024];
bc=peer.recv(buff,1024,&timeout);
if (bc>=0)
{
buff[bc]='\0';
cout<<endl<<"recv:\t"<<buff<<endl;
}
peer.close();
system("pause");
return 0;
}