ace TCP 服务端和客户端简单程序

// 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值