ACE Tutorial [翻译] 03 :: 风之谷
未识别博客 · jnn · 2004-10-29
ACE Tutorial [翻译] 03
时间:2004-10-29
创建一个简单的客户端
我们已经知道如何创建一个服务器,在这里我们将介绍一下如何创建一个客户端。由于这部分比较简单,所有我只打算用一页的篇幅来进行介绍。
Kirthika
说,“这里有一小段概述来描述这个客户端应用”。
在
server
中有一个
ACE_SOCK_Stream
类型的流对象。
ACE_Sock_Connector
的职责就是向监听的
Server
创建连接。它使用的是存储在
ACE_INET_Addr
对象中的
server_host_address
和端口号来建立连接的。一旦连接建立,客户端将回集中与服务器进行交互,并且不断地向服务器发送消息。
注意:在这里调用了
send_n()
方法,该方法提供了网络缓冲以及将数据稳定发送到服务器的功能。当然,为了提供容错功能这里采用了超时处理机制,保证传输事务能在服务结束之前完成。在服务器端如果接收到了零个字节将在
Event_Handler::handle_input()
中调用
close()
方法进行结束操作。这样就给客户端断开连接的机会。之后服务器端中止。
// page01.html,v 1.12 2000/11/27 17:56:42 othman Exp
/* To establish a socket connection to a server, we'll need an
ACE_SOCK_Connector.
为了获得与服务器的socket连接,我们需要使用ACE_SOCK_Connector。
*/
#include
"
ace/SOCK_Connector.h
"
#include
"
ace/Log_Msg.h
"
/* Unlike the previous two tutorials, we're going to allow the user to
provide command line options this time.
Still, we need defaults in
case that isn't done.
与前面两个教程不同,我们在这里打算为用户提供命令行的参数选择。不过之前
我们还是需要为这些参数设置缺省值。
*/
static u_short SERVER_PORT = ACE_DEFAULT_SERVER_PORT;
static const char *const SERVER_HOST = ACE_DEFAULT_SERVER_HOST;
static const int MAX_ITERATIONS = 4;
int
main (int argc, char *argv[])
{
/* Accept the users's choice of hosts or use the default.
Then do
the same for the TCP/IP port at which the server is listening as
well as the number of iterations to perform.
选择是用户定义的主机还是选择缺省值。同时也需要选择
TCP/IP服务器
进程所监听的端口号,以及循环的迭代次数。
*/
const char *server_host = argc > 1 ? argv[1] : SERVER_HOST;
u_short server_port = argc > 2 ?
ACE_OS::atoi
(argv[2]) : SERVER_PORT;
int max_iterations = argc > 3 ?
ACE_OS::atoi
(argv[3]) : MAX_ITERATIONS;
/* Build ourselves a Stream socket. This is a connected socket that
provides reliable end-to-end communications. We will use the
server object to send data to the server we connect to.
建立我们自己的socket流。这是个能够保证可靠的端到端通讯功能的
可连接的
socket。我们将采用server对象来负责向服务器传输数据。
*/
ACE_SOCK_Stream server;
/* And we need a connector object to establish that connection. The
ACE_SOCK_Connector object provides all of the tools we need to
establish a connection once we know the server's network
address...
现在我们需要一个
connector对象来负责建立连接。ACE_SOCK_Connector
对象向我们提供了一个向指定的服务器地址建立连接的工具
*/
ACE_SOCK_Connector connector;
/* Which we create with an ACE_INET_Addr object. This object is
given the TCP/IP port and hostname of the server we want to
connect to.
创建一个
ACE_INET_Addr对象。这个对象需要指定我们想要连接的服务器主机名
以及
TCP/IP端口号。
*/
ACE_INET_Addr addr (server_port,
server_host);
/* So, we feed the Addr object and the Stream object to the
connector's connect() member function. Given this information, it
will establish the network connection to the server and attach
that connection to the server object.
现在我们想connetor的connect方法中传入地址对象以及流对象。它根据
这些给定的信息向指定的服务器建立连接并且与服务器对象建立连接。
*/
if (connector.connect (server, addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"
%p\n
",
"
open
"),
-1);
/* Just for grins, we'll send the server several messages.
呵呵,现在我们可以向服务器发送几条消息了。
*/
for (int i = 0; i < max_iterations; i++)
{
char buf[BUFSIZ];
/* Create our message with the message number
创建消息号
*/
ACE_OS::sprintf
(buf,
"
message = %d\n
",
i + 1);
/* Send the message to the server.
We use the server object's
send_n() function to send all of the data at once. There is
also a send() function but it may not send all of the
data. That is due to network buffer availability and such. If
the send() doesn't send all of the data, it is up to you to
program things such that it will keep trying until all of the
data is sent or simply give up. The send_n() function already
does the "
keep trying
" option for us, so we use it.
向服务器发送消息。我们通过调用server的send_n()方法一次发送
所有的数据。这里我们可以选择使用
send()方法,但是这个方法并不
一次发送所有的数据,你需要负责进行不断尝试发送数据或者执行放弃
操作的内容进行编程。而
send_n()方法已经为我们提供了这样的不断
尝试发送的选择,所以我们使用这个方法。
Like the send() method used in the servers we've seen, there
are two additional parameters you can use on the send() and
send_n() method calls.
The timeout parameter limits the
amount of time the system will attempt to send the data to the
peer.
The flags parameter is passed directly to the OS send()
system call.
See send(2) for the valid flags values.
和我们以前常见的
send()方法使用一样,在send_n()方法中,我们需要
使用两个传统的参数。在这里省略了系统尝试向对方发送数据的超时参数。
这个标识参数是直接被定义到
OS的send()系统调用中的。你可以通过查找
send(2)获取更详细的信息。
*/
if (server.send_n (buf,
ACE_OS::strlen
(buf)) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"
%p\n
",
"
send
"),
-1);
else
/* Pause for a second.
暂停一秒
*/
ACE_OS::sleep
(1);
}
/* Close the connection to the server.
The servers we've created so
far all are based on the ACE_Reactor.
When we close(), the
server's reactor will see activity for the registered event
handler and invoke handle_input().
That, in turn, will try to
read from the socket but get back zero bytes.
At that point, the
server will know that we've closed from our side.
关闭与服务器之间的连接。 由于我们创建的服务器都是建立在
ACE_Reactor
基础之上的。当我们调用
close()方法,服务器的reactor将会激发注册的句柄,
并调用
handle_input()方法。这样,在处理方法中将会从指定的socket中获取
零个字节。至此,服务器端就能够检测到我们关掉连接了。
*/
if (server.close () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"
%p\n
",
"
close
"),
-1);
return 0;
}
这是不是很简单啊。如果将所有与连接相关的内容封装在对象中,通过重载这些简单的操作方法以降低其对网络的依赖,就能使的这部分程序更加简单了。这部分的内容将在另一个教程出现。
jnn
引用Trackback(0)
|
编辑
评论
发表评论
用户名:
Email:
主页:
最后更新
ACE Tutorial [翻译] 04
ACE Tutorial [翻译] 03
ACE Tutorial [翻译] 02-page03
ACE Tutorial [翻译] 02-page02
ACE Tutorial [翻译] 02-page01
ACE Tutorial [翻译] 01-page04
ACE Tutorial [翻译] 01-page03
ACE Tutorial [翻译] 01-page02
ACE Tutorial [翻译]01-page01
ACE 教程 [翻译] (前言)
原始快照:web.archive.org