summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_2/ex01.html
blob: e702ee77078f05f05f25ef7564430947fa324f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<HTML>
<!-- $Id$ -->
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Ambreen Ilyas">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter: "IPC SAP" (Interprocess Communication
Mechanisms in ACE).</FONT>
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>

<P><FONT COLOR="#FF0000">//Example 1</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Acceptor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Stream.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_DATA
18</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_BUF
1024</FONT>

<P>class Server{

<P>public:
<BR>Server (int port):
<BR>&nbsp;server_addr_(port),peer_acceptor_(server_addr_){
<BR>&nbsp;data_buf_= new char[SIZE_BUF];
<BR>&nbsp;}

<P><FONT COLOR="#FF0000">//Handle the connection once it has been established.</FONT>
<BR><FONT COLOR="#FF0000">//Here the connection is handled by reading SIZE_DATA
amount of data</FONT>
<BR><FONT COLOR="#FF0000">//from the remote and then closing the connection</FONT>
<BR><FONT COLOR="#FF0000">//stream down.</FONT>
<BR>int handle_connection(){
<BR>&nbsp;<FONT COLOR="#FF0000"> // Read data from client</FONT>
<BR>&nbsp;if(new_stream_.recv_n (data_buf_, SIZE_DATA, 0)==-1)
<BR>&nbsp; ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));
<BR>&nbsp;

<P>&nbsp;ACE_DEBUG((LM_DEBUG,"Server recieved %s \n",data_buf_));
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">// Close new endpoint</FONT>
<BR>&nbsp;if (new_stream_.close () == -1)
<BR>&nbsp; ACE_ERROR ((LM_ERROR, "%p\n", "close"));
<BR>&nbsp;return 0;
<BR>}
<BR><FONT COLOR="#FF0000">//Use the acceptor component peer_acceptor_ to
accept the connection</FONT>
<BR><FONT COLOR="#FF0000">//into the underlying stream new_stream_. After
the connection has been</FONT>
<BR><FONT COLOR="#FF0000">//established call the handle_connenction() method.</FONT>
<BR>int accept_connections (){
<BR>&nbsp;if (peer_acceptor_.get_local_addr (server_addr_) == -1)
<BR>&nbsp; ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1);

<P>&nbsp;ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",
<BR>&nbsp; server_addr_.get_port_number ()));
<BR>&nbsp;

<P>&nbsp;<FONT COLOR="#FF0000">// Performs the iterative server activities.</FONT>
<BR>&nbsp;while(1){
<BR>&nbsp; ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
<BR>&nbsp; if (peer_acceptor_.accept
<BR>&nbsp;&nbsp;&nbsp; (new_stream_, &amp;client_addr_, &amp;timeout)==
-1){
<BR>&nbsp;&nbsp; ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
<BR>&nbsp;&nbsp; continue;
<BR>&nbsp;&nbsp; }
<BR>&nbsp; else
<BR>&nbsp;&nbsp; ACE_DEBUG((LM_DEBUG,
<BR>&nbsp;&nbsp; "Connection established with remote %s:%d\n",
<BR>&nbsp;&nbsp; client_addr_.get_host_name(),client_addr_.get_port_number()));
<BR>&nbsp;&nbsp; <FONT COLOR="#FF0000">//Handle the connection</FONT>
<BR>&nbsp;&nbsp; handle_connection();
<BR>&nbsp; }
<BR>&nbsp;}
<BR>&nbsp;
<BR>&nbsp;

<P>private:
<BR>&nbsp;char *data_buf_;
<BR>&nbsp;ACE_INET_Addr server_addr_;
<BR>&nbsp;ACE_INET_Addr client_addr_;
<BR>&nbsp;ACE_SOCK_Acceptor peer_acceptor_;
<BR>&nbsp;ACE_SOCK_Stream new_stream_;
<BR>&nbsp;ACE_HANDLE newhandle;
<BR>};

<P>int main (int argc, char *argv[]){
<BR>&nbsp;if(argc&lt;2){
<BR>&nbsp; ACE_ERROR((LM_ERROR,"Usage egX &lt;port_num>"));
<BR>&nbsp; ACE_OS::exit(1);
<BR>&nbsp; }
<BR>&nbsp;Server server(ACE_OS::atoi(argv[1]));
<BR>&nbsp;server.accept_connections();
<BR>}

<P>&nbsp;<A HREF="ex02.html">Next Example</A>
</BODY>
</HTML>