Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 1513

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>5.3 Accepting Multiple Connections</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node12.html#section.sockets.stream">&lt;&lt; Prev</A></TD><TD><A href="node10.html">- Up -</A></TD><TD><A href="node14.html#section.sockets.datagram">Next &gt;&gt;</A></TD></TR></TABLE><DIV id="section.sockets.accept"><H2><A name="section.sockets.accept">5.3 Accepting Multiple Connections</A></H2><P>A frequently occuring situation is to have a single server with a port number known to several clients and that all these clients want to connect to that server. For this purpose sockets have the possibility to accept more than a single connection. </P><P>The first step is to create the server socket <CODE>S</CODE> with a port number <CODE>P</CODE>: </P><DL class="anonymous"><DD class="code"><CODE>S={New&nbsp;Open<SPAN class="keyword">.</SPAN>socket&nbsp;init}<BR>P={S&nbsp;bind(port:$)}<BR>{S&nbsp;listen}</CODE></DD></DL><P> The clients can connect later to this socket with the port number <CODE>P</CODE>.</P><P> </P><DIV class="program" id="fig.setup.server"><HR><P><A name="fig.setup.server"></A></P></DIV><DL class="anonymous"><DD class="code"><CODE><SPAN class="keyword">class</SPAN>&nbsp;<SPAN class="type">Accepted</SPAN>&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;Open.socket</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">report</SPAN>(H&nbsp;P)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{Browse&nbsp;read(host:H&nbsp;port:P&nbsp;read:{<SPAN class="keyword">self</SPAN>&nbsp;read(list:$)})}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;report(H&nbsp;P)}<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR><SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;<BR><SPAN class="keyword">proc</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">Accept</SPAN>}<BR>&nbsp;&nbsp;&nbsp;H&nbsp;P&nbsp;A<BR><SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;{S&nbsp;accept(acceptClass:Accepted<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host:?H&nbsp;port:?P&nbsp;accepted:?A)}<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">thread</SPAN>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{A&nbsp;report(H&nbsp;P)}&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;{Accept}<BR><SPAN class="keyword">end</SPAN></CODE></DD></DL><DIV class="program"><P class="caption"><STRONG>Program&nbsp;5.1:</STRONG> Accepting multiple connections.</P><HR></DIV><P> </P><P>How to set up the server is shown in <A href="node13.html#fig.setup.server">Program&nbsp;5.1</A>. The procedure <CODE>Accept</CODE> waits until the server socket <CODE>S</CODE> accepts a connection. When a connection is accepted, the variable <CODE>A</CODE> is bound to a newly created object. The object <CODE>A</CODE> is created from the class <CODE>Accepted</CODE>, this is specified by the feature <CODE>acceptClass</CODE>. The newly created object is already connected: Applying the object to the message <CODE>report(H&nbsp;P)</CODE> waits that on the accepted connection data arrives. </P><P>The loop to accept connections is started by applying the procedure <CODE>Accept</CODE>: </P><DL class="anonymous"><DD class="code"><CODE>{Accept}</CODE></DD></DL><P> </P><P>Let us assume that we have two clients <CODE>C1</CODE> and <CODE>C2</CODE> that need to connect to the socket with port number <CODE><I>P</I></CODE>: </P><DL class="anonymous"><DD class="code"><CODE>C1={New&nbsp;Open<SPAN class="keyword">.</SPAN>socket&nbsp;client(port:P)}&nbsp;&nbsp;<BR>C2={New&nbsp;Open<SPAN class="keyword">.</SPAN>socket&nbsp;client(port:P)}</CODE></DD></DL><P> </P><P>After the clients are connected, sending data from the clients is as usual: </P><DL class="anonymous"><DD class="code"><CODE>{C1&nbsp;write(vs:<SPAN class="string">'hello&nbsp;from&nbsp;C1'</SPAN>)}</CODE></DD></DL><P> </P><P>As soon as the socket object created by the procedure <CODE>Accept</CODE> receives the data sent from a client, the data appears in the Browser.</P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node12.html#section.sockets.stream">&lt;&lt; Prev</A></TD><TD><A href="node10.html">- Up -</A></TD><TD><A href="node14.html#section.sockets.datagram">Next &gt;&gt;</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>