⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-netapi2.html

📁 vxWorks网络协议驱动开发用户手册写协议栈时的好指引
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title>    Networking APIs     </title></head><body bgcolor="FFFFFF"><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="c-netapi.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-netapi.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-netapi1.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-netapi3.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="88632">7.2  &nbsp;&nbsp;BSD Sockets</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="88634"> </a>A socket is a communications end-point that is <i class="term">bound</i> to a UDP or TCP port within the node. Under VxWorks, your application can use the <i class="emphasis">sockets</i> interface to access features of the Internet Protocol suite (features such as multicasting). Depending on the bound port type, a socket is referred to either as a stream socket or a datagram socket. VxWorks sockets are UNIX BSD 4.4 compatible.  However, VxWorks does not support signal functionality for sockets.</p><dd><p class="Body"><a name="88637"> </a>S<i class="term">tream sockets </i>use TCP to bind to a particular port number.  Another process, on any host in the network, can then create another stream socket and request that it be connected to the first socket by specifying its host Internet address and port number.  After the two TCP sockets are connected, there is a <i class="term">virtual circuit</i> set up between them, allowing reliable socket-to-socket communications. This style of communication is conversational. </p><dd><p class="Body"><a name="88639"> </a><i class="term">Datagram socket</i>s use UDP to bind to a particular port number.  Other processes, on any host in the network, can then send messages to that socket by specifying the host Internet address and the port number. Compared to TCP, UDP provides a simpler but less robust communication method.  In a UDP communication, data is sent between sockets in separate, unconnected, individually addressed packets called <i class="term">datagrams</i>. There is no sense of conversation with a datagram socket. The communication is in the style of a letter. Each packet carries the address of both the destination and the sender.  Compared to TCP, UDP is unreliable. Like the mail, packets that are lost or out-of-sequence are not reported. </p><dd><p class="Body"><a name="88640"> </a>There are a number of complex network programming issues that are beyond the scope of this guide.  For additional information, consult a socket-programming book, such as one of the following:</p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="88641"> </a><i class="title">Internetworking with TCP/IP Volume III </i>by Douglas Comer and David Stevens</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="88642"> </a><i class="title">UNIX Network Programming</i> by Richard Stevens</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="88643"> </a><i class="title">The Design and Implementation of the 4.3 BSD UNIX Operating System</i> by Leffler, McKusick, Karels and Quarterman</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="88644"> </a><i class="title">TCP/IP Illustrated, Vol. 1</i>, by Richard Stevens</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="88645"> </a><i class="title">TCP/IP Illustrated, Vol. 2</i>, by Gary Wright and Richard Stevens</li></ul></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="88646">7.2.1  &nbsp;&nbsp;Stream Sockets (TCP)</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="88648"> </a>The Transmission Control Protocol (TCP) provides reliable, two-way transmission of data.  In a TCP communication, two sockets are <i class="term">connected</i>, allowing a reliable byte-stream to flow between them in either direction.  TCP is referred to as a <i class="term">virtual circuit</i> protocol, because it behaves as though a circuit is created between the two sockets.</p><dd><p class="Body"><a name="88649"> </a>A good analogy for TCP communications is a telephone system.  Connecting two sockets is similar to calling from one phone to another.  After the connection is established, you can write and read data (talk and listen).</p><dd><p class="Body"><a name="88653"> </a><a href="c-netapi2.html#88657">Table&nbsp;7-1</a> shows the steps in establishing socket communications with TCP, and the analogy of each step with telephone communications.<p class="table"><h4 class="EntityTitle"><a name="88657"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 7-1:&nbsp;&nbsp;TCP Analogy to Telephone Communication</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88665"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Task 1 <br>Waits</font></b></div></th><td width="10">&nbsp;</td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88667"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Task 2 <br>Calls</font></b></div></th><td width="10">&nbsp;</td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88669"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Function</font></b></div></th><td width="10">&nbsp;</td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88671"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Analogy</font></b></div></th><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88673"> </a><b class="routine"><i class="routine">socket</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88675"> </a><b class="routine"><i class="routine">socket</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88677"> </a>Create sockets.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88679"> </a>Hook up telephones.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88681"> </a><b class="routine"><i class="routine">bind</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88683"> </a></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88685"> </a>Assign address to socket.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88687"> </a>Assign phone numbers.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88689"> </a><b class="routine"><i class="routine">listen</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88691"> </a></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88693"> </a>Allow others to connect to socket.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88695"> </a>Allow others to call.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88697"> </a></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88699"> </a><b class="routine"><i class="routine">connect</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88701"> </a>Request connection to another socket.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88703"> </a>Dial another phone's number.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88705"> </a><b class="routine"><i class="routine">accept</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88707"> </a></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88709"> </a>Complete connection between sockets.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88711"> </a>Answer phone and establish connection.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88713"> </a><b class="routine"><i class="routine">write</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88715"> </a><b class="routine"><i class="routine">write</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88717"> </a>Send data to other socket.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88719"> </a>Talk.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88721"> </a><b class="routine"><i class="routine">read</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88723"> </a><b class="routine"><i class="routine">read</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88725"> </a>Receive data from other socket.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88727"> </a>Listen.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="88729"> </a><b class="routine"><i class="routine">close</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88731"> </a><b class="routine"><i class="routine">close</i></b><b>(</b>&nbsp;<b>)</b></div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88733"> </a>Close sockets.</div></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="88735"> </a>Hang up.</div></td><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><h4 class="EntityTitle"><a name="88737"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 7-1:&nbsp;&nbsp;Stream Sockets (TCP)</font></a></h4><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="88739"> </a>The following code example uses a client-server communication model.  The server communicates with clients using stream-oriented (TCP) sockets.  The main server loop, in <b class="routine"><i class="routine">tcpServerWorkTask</i></b><b>(</b>&nbsp;<b>)</b>, reads requests, prints the client's message to the console, and, if requested, sends a reply back to the client.  The client builds the request by prompting for input.  It sends a message to the server and, optionally, waits for a reply to be sent back.  To simplify the  example, we assume that the code is executed on machines that have the same data sizes and alignment.</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="92189">/* tcpExample.h - header used by both TCP server and client examples */   /* defines */ #define SERVER_PORT_NUM         5001   /* server's port number for bind() */ #define SERVER_WORK_PRIORITY    100    /* priority of server's work task */ #define SERVER_STACK_SIZE       10000  /* stack size of server's work task */ #define SERVER_MAX_CONNECTIONS  4      /* max clients connected at a time */ #define REQUEST_MSG_SIZE        1024   /* max size of request message */ #define REPLY_MSG_SIZE          500    /* max size of reply message */  /* structure for requests from clients to server */ struct request     {     int reply;                         /* TRUE = request reply from server */      int msgLen;                        /* length of message text */     char message[REQUEST_MSG_SIZE];    /* message buffer */      };</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="92194">/* tcpClient.c - TCP client example */  /* DESCRIPTION This file contains the client-side of the VxWorks TCP example code. The example code demonstrates the usage of several BSD 4.4-style socket routine calls. */  /* includes */  #include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "stdioLib.h" #include "strLib.h" #include "hostLib.h" #include "ioLib.h" #include "tcpExample.h"  /**************************************************************************** * * tcpClient - send requests to server over a TCP socket * * This routine connects over a TCP socket to a server, and sends a * user-provided message to the server.  Optionally, this routine * waits for the server's reply message. * * This routine may be invoked as follows: *       -&gt; tcpClient "remoteSystem" *       Message to send: *       Hello out there *       Would you like a reply (Y or N): *       y *       value = 0 = 0x0 *       -&gt; MESSAGE FROM SERVER: *       Server received your message 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -