135 lines
8.9 KiB
HTML
135 lines
8.9 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Create a connection-oriented socket" />
|
|
<meta name="abstract" content="These server and client examples illustrate socket APIs written for a connection-oriented protocol such as Transmission Control Protocol (TCP)." />
|
|
<meta name="description" content="These server and client examples illustrate socket APIs written for a connection-oriented protocol such as Transmission Control Protocol (TCP)." />
|
|
<meta name="DC.Relation" scheme="URI" content="bdesign.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="xconoserver.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="xconoclient.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="xconoserver.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/listen.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/bind.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/accept.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/send.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/recv.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/close.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/ssocko.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/sselect.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/ghostnm.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../apis/connec.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2001, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2001, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="connectionor" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Create a connection-oriented socket</title>
|
|
</head>
|
|
<body id="connectionor"><a name="connectionor"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Create a connection-oriented socket</h1>
|
|
<div><p>These server and client examples illustrate socket APIs written
|
|
for a connection-oriented protocol such as Transmission Control Protocol (TCP).</p>
|
|
<div class="section"><p>The following figure illustrates the client/server relationship
|
|
of the sockets API for a connection-oriented protocol.</p>
|
|
<br /><img src="rxab6502.gif" alt="The client/server relationship of the sockets API for a connection-oriented design" /><br /></div>
|
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Connection-oriented server</h4><p>The
|
|
following sequence of the socket calls provide a description of the figure.
|
|
It also describes the relationship between the server and client application
|
|
in a connection-oriented design. Each set of flows contain links to usage
|
|
notes on specific APIs. </p>
|
|
<ol><li>The <span class="apiname">socket()</span> function returns a socket descriptor representing
|
|
an endpoint. The statement also identifies that the INET (Internet Protocol)
|
|
address family with the TCP transport (SOCK_STREAM) is used for this socket.</li>
|
|
<li>The <span class="apiname">setsockopt()</span> function allows the local address
|
|
to be reused when the server is restarted before the required wait time expires. </li>
|
|
<li>After the socket descriptor is created, the <span class="apiname">bind()</span> function
|
|
gets a unique name for the socket. In this example, the user sets the s_addr
|
|
to zero, which allows connections to be established from any IPv4 client that
|
|
specifies port 3005.</li>
|
|
<li> The <span class="apiname">listen()</span> allows the server to accept incoming
|
|
client connections. In this example, the backlog is set to 10. This means
|
|
that the system queues 10 incoming connection requests before the system starts
|
|
rejecting the incoming requests.</li>
|
|
<li>The server uses the <span class="apiname">accept()</span> function to accept an
|
|
incoming connection request. The <span class="apiname">accept()</span> call blocks indefinitely
|
|
waiting for the incoming connection to arrive.</li>
|
|
<li>The <span class="apiname">select()</span> function allows the process to wait for
|
|
an event to occur and to wake up the process when the event occurs. In this
|
|
example, the system notifies the process only when data is available to be
|
|
read. A 30-second timeout is used on this <span class="apiname">select()</span> call.</li>
|
|
<li>The <span class="apiname">recv()</span> function receives data
|
|
from the client application. In this example, the client sends 250 bytes of
|
|
data. Thus the SO_RCVLOWAT socket option can be used, specifying that <span class="apiname">recv()</span> does
|
|
not wake up until all 250 bytes of data have arrived.</li>
|
|
<li>The <span class="apiname">send() </span> function echoes the data back to the client.</li>
|
|
<li>The <span class="apiname">close()</span> function closes any open socket descriptors.</li>
|
|
</ol>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Connection-oriented client</h4><p>The
|
|
Example: A connection-orientated client uses the following sequence of function
|
|
calls: </p>
|
|
<ol><li>The <span class="apiname">socket()</span> function returns a socket descriptor representing
|
|
an endpoint. The statement also identifies that the INET (Internet Protocol)
|
|
address family with the TCP transport (SOCK_STREAM) is used for this socket.</li>
|
|
<li>In the client example program, if the server string that was passed into
|
|
the <span class="apiname">inet_addr()</span> function was not a dotted decimal IP address,
|
|
then it is assumed to be the host name of the server. In that case, use the <span class="apiname">gethostbyname()</span> function
|
|
to retrieve the IP address of the server.</li>
|
|
<li>After the socket descriptor is received, the <span class="apiname">connect()</span> function
|
|
is used to establish a connection to the server.</li>
|
|
<li>The <span class="apiname">send() </span> function sends 250 bytes of data to the
|
|
server.</li>
|
|
<li>The <span class="apiname">recv()</span> function waits for the server
|
|
to echo the 250 bytes of data back. In this example, the server responds with
|
|
the same 250 bytes that was just sent. In the client example, the 250 bytes
|
|
of the data might arrive in separate packets, so the <span class="apiname">recv()</span> function
|
|
can be used over and over until all 250 bytes have arrived. </li>
|
|
<li>The <span class="apiname">close()</span> function closes any open socket descriptors.</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<ul class="ullinks">
|
|
<li class="ulchildlink"><strong><a href="xconoserver.htm">Example: A connection-oriented server</a></strong><br />
|
|
This code example shows how a connection-oriented server can be created.</li>
|
|
<li class="ulchildlink"><strong><a href="xconoclient.htm">Example: A connection-oriented client</a></strong><br />
|
|
This example shows how to create a socket client program to connect to a connection-oriented server in a connection-oriented design.</li>
|
|
</ul>
|
|
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="bdesign.htm" title="This topic provides examples of sockets programs that use the most basic design.">Basic socket design</a></div>
|
|
</div>
|
|
<div class="relref"><strong>Related reference</strong><br />
|
|
<div><a href="xconoserver.htm" title="This code example shows how a connection-oriented server can be created.">Example: A connection-oriented server</a></div>
|
|
</div>
|
|
<div class="relinfo"><strong>Related information</strong><br />
|
|
<div><a href="../apis/listen.htm">listen()</a></div>
|
|
<div><a href="../apis/bind.htm">bind()</a></div>
|
|
<div><a href="../apis/accept.htm">accept()</a></div>
|
|
<div><a href="../apis/send.htm">send()</a></div>
|
|
<div><a href="../apis/recv.htm">recv()</a></div>
|
|
<div><a href="../apis/close.htm">close()</a></div>
|
|
<div><a href="../apis/socket.htm">socket()</a></div>
|
|
<div><a href="../apis/ssocko.htm">setsockopt()</a></div>
|
|
<div><a href="../apis/sselect.htm">select()</a></div>
|
|
<div><a href="../apis/ghostnm.htm">gethostbyname()</a></div>
|
|
<div><a href="../apis/connec.htm">connect()</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |