当前位置: 首页 > 工具软件 > Sequel Ace > 使用案例 >

ACE 创建TCP/IP连接 小结,

柳轶
2023-12-01


ACE中TCP/IP连接小结:

TCP客户端需要做五件事:

1、  端口和IP地址绑定。ACE_INET_Addr  srv(5000,ACE_LOCALHOST);5000端口和本地地址绑定。包含在头文件 #include<INET_Addr.h>中。

构造函数有ACE_INET_Addr(void)默认的构造函数 

 

ACE_INET_Addr (const ACE_INET_Addr &)

 

Copy constructor.

 

ACE_INET_Addr (const sockaddr_in *addr, int len)

 

Creates an ACE_INET_Addr from a sockaddr_in structure.

 

ACE_INET_Addr (u_short port_number, const char host_name[], int address_family=AF_UNSPEC)//端口号,主机名,协议族

 

ACE_INET_Addr (const char address[], int address_family=AF_UNSPEC)

 

ACE_INET_Addr (u_short port_number, ACE_UINT32 ip_addr=INADDR_ANY)

 

ACE_INET_Addr (const char port_name[], const char host_name[], const char protocol[]="tcp")

 

ACE_INET_Addr (const char port_name[], ACE_UINT32 ip_addr, const char protocol[]="tcp")

可以使用set方法对其进行设置

int  set (const ACE_INET_Addr &)

  Initializes from another ACE_INET_Addr.

 

int  set (u_short port_number, const char host_name[], int encode=1, int address_family=AF_UNSPEC)

 

2、  建立数据流。ACE_SOCK_Stream  stream 包含在#include<SOCK_Stream.h>中,

ssize_t  send_urg (const void *ptr, size_tlen=sizeof(char), const ACE_Time_Value *timeout=0) const 

ssize_t  recv_urg(void *ptr, size_t len=sizeof(char), const ACE_Time_Value *timeout=0)const 

Parameters:

buf

The buffer to write from or receive into.

iov

An I/O vector containing a specified number of count/pointer pairs directing the data to be transferred.

iovcnt

The number of I/O vectors to be used from iov.

len

The number of bytes to transfer.

flags

Flags that will be passed through to the recv() system call.

timeout

Indicates how long to blocking trying to transfer data. If no timeout is supplied (timeout == 0) the method will wait indefinitely or until an error occurs for the specified number of bytes to be transferred. To avoid any waiting, specify a timeout value with 0 seconds.

bytes_transferred

If non-0, points to a location which receives the total number of bytes transferred before the method returns, even if it's less than the number requested.

Return values:

len,the

complete number of bytes transferred.

0

EOF, i.e., the peer closed the connection.

-1

an error occurred before the entire amount was transferred. Check errno for more information. If the timeout period is reached, errno is ETIME.

On partial transfers, i.e., if any data is transferred before timeout/error/EOF, *bytes_transferred will contain the number of bytes transferred.

ssize_t

recv_n (void *buf, size_t len, int flags, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

 

Try to recv exactly len bytes into buf from the connected socket.

ssize_t

recv_n (void *buf, size_t len, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

 

Try to recv exactly len bytes into buf from the connected socket.

ssize_t

recvv_n (iovec iov[], int iovcnt, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

 

Receive an iovec of size iovcnt from the connected socket.

ssize_t

send_n (const void *buf, size_t len, int flags, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

 

Try to send exactly len bytes from buf to the connection socket.

ssize_t

send_n (const void *buf, size_t len, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

 

Try to send exactly len bytes from buf to the connected socket.

ssize_t

send_n (const ACE_Message_Block *message_block, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

ssize_t

sendv_n (const iovec iov[], int iovcnt, const ACE_Time_Value *timeout=0, size_t *bytes_transferred=0) const

Send an iovec of size iovcnt to the connected socket.

 

3、  连接,使用ACE_SOCK_Connector进行连接。包含在#include<SOCK_Connector.h>

 

 

ACE_SOCK_Connector (void)

Default constructor.

 
 

ACE_SOCK_Connector (ACE_SOCK_Stream &new_stream, const ACE_Addr &remote_sap, const ACE_Time_Value *timeout=0, const ACE_Addr &local_sap=ACE_Addr::sap_any, int reuse_addr=0, int flags=0, int perms=0, int protocol=0)

 

ACE_SOCK_Connector (ACE_SOCK_Stream &new_stream, const ACE_Addr &remote_sap, ACE_QoS_Params qos_params, const ACE_Time_Value *timeout=0, const ACE_Addr &local_sap=ACE_Addr::sap_any, ACE_Protocol_Info *protocolinfo=0, ACE_SOCK_GROUP g=0, u_long flags=0, int reuse_addr=0, int perms=0)

int

connect (ACE_SOCK_Stream &new_stream, const ACE_Addr &remote_sap, const ACE_Time_Value *timeout=0, const ACE_Addr &local_sap=ACE_Addr::sap_any, int reuse_addr=0, int flags=0, int perms=0, int protocol=0)

int

connect (ACE_SOCK_Stream &new_stream, const ACE_Addr &remote_sap, ACE_QoS_Params qos_params, const ACE_Time_Value *timeout=0, const ACE_Addr &local_sap=ACE_Addr::sap_any, ACE_Protocol_Info *protocolinfo=0, ACE_SOCK_GROUP g=0, u_long flags=0, int reuse_addr=0, int perms=0)

 

4、  传送数据。连接建立成功,可以和服务端进行通信。利用流的send_n()和recv_n()方法进行交互。

5、  关闭流。

服务端:

1、  设置监听的端口号,利用ACE_INET_Addr进行设置。

2、  打开监听的端口。ACE_SOCK_Acceptoracceptor。 acceptor.open(端口);包含在#include<SOCK_Acceptor.h>

3、  监听端口,建立数据流,接受连接。acceptor.accept()

 

ACE_SOCK_Acceptor (void)

Default constructor.

 

ACE_SOCK_Acceptor (const ACE_Addr &local_sap, int reuse_addr=0, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)

 

ACE_SOCK_Acceptor (const ACE_Addr &local_sap, ACE_Protocol_Info *protocolinfo, ACE_SOCK_GROUP g, u_long flags, int reuse_addr, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)

int

open (const ACE_Addr &local_sap, int reuse_addr=0, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)

int

open (const ACE_Addr &local_sap, ACE_Protocol_Info *protocolinfo, ACE_SOCK_GROUP g, u_long flags, int reuse_addr, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)

int

close (void)

Close the socket. Returns 0 on success and -1 on failure.

 

~ACE_SOCK_Acceptor (void)

Default dtor.

int

accept (ACE_SOCK_Stream &new_stream, ACE_Addr *remote_addr=0, ACE_Time_Value *timeout=0, bool restart=true, bool reset_new_handle=false) const

int

accept (ACE_SOCK_Stream &new_stream, ACE_Accept_QoS_Params qos_params, ACE_Addr *remote_addr=0, ACE_Time_Value *timeout=0, bool restart=true, bool reset_new_handle=false) const

void

dump (void) const

Dump the state of an object. 转储对象的状态。

 

4、  开始进行交互通信。

一个简单的例子:客户端
int ACE_TMAIN(int,ACE_TCHAR *[])
{
	ACE_INET_Addr srvr(50000,"127.0.0.1");
	std::cout<<ACE_LOCALHOST<<std::endl;
	ACE_SOCK_Connector connector;
	ACE_SOCK_Stream peer;
	int i=connector.connect(peer,srvr);
	
	if(-1==i)
		ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("connect")),1);
	int bc;
	char buf[64];
	peer.send_n("uptime\n",7);
	bc=peer.recv(buf,sizeof(buf));
	std::cout<<bc;
	_write(1,buf,bc);
	peer.close();
	return 0;
}
服务端:
#define MAXHOSTNAMELEN 128
int ACE_TMAIN(int,ACE_TCHAR*[])
{
	ACE_INET_Addr port_to_listen(50000);
	ACE_SOCK_Acceptor acceptor;

	std::cout<<"TCPServer"<<std::endl;
	if(acceptor.open(port_to_listen,1)==-1)
		ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("acceptor.open")),100);
	while(true)
	{
		ACE_SOCK_Stream peer;
		ACE_INET_Addr peer_addr;
		ACE_Time_Value timeout(10,0);
		if(acceptor.accept(peer)==-1)
			ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("(%p|%t)Failed to accept")ACE_TEXT("client connection\n")),100);
		

		/*if(acceptor.accept(peer,&peer_addr,&timeout,0)==-1)
		{	   
			 std::cout<<acceptor.accept(peer);
			if(ACE_OS::last_error()==EINTR)
				ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%p|%t) Timeout while")ACE_TEXT("waiting for connection")));
		}*/
		else
		{
			ACE_TCHAR peer_name[MAXHOSTNAMELEN];
			ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%p|%t) Connection from %s\n"),peer_name));
			char buffer[4096];
			ssize_t bytes_received;
			while((bytes_received=peer.recv(buffer,sizeof(buffer))!=-1))
			{
				peer.send_n(buffer,bytes_received);
			}
			peer.close();

		}

	}
	return  0;

}



 类似资料: