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

📄 网络协议.htm

📁 Vxwork的学习资料,对初学者有帮助 有关于BSP的说明介绍
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<BLOCKQUOTE>
  <P>BSD Sockets 使用的最广泛的网络程序编程方法,主要用于应用程序的编写,用于网络上主机与主机之间的相互通信.</P>
  <P>UNIX,Linux,VxWorks均支持BSD Sockets,Windows的Winsock基本上是来自BSD Sockets</P>
  <P>Socket分为Stream Sockets和Data Sockets.</P>
  <P>Stream Sockets是可靠性的双向数据传输用TCP,如HTTP,FTP等</P>
  <P>Data Sockets是不可靠连接用UDP,如多点广播,DHCP,SNMP,RIP等.</P>
  <P> </P></BLOCKQUOTE>
<BLOCKQUOTE>
  <UL>
    <LI><BIG><STRONG>TCP服务器端和一个TCP客户端通信的程序过程 </STRONG></BIG></LI></UL></BLOCKQUOTE>
<DIV align=center>
<CENTER>
<TABLE height=29 width=568 border=0>
  <TBODY>
  <TR>
    <TD align=middle width=182 height=29>
      <P align=center><STRONG>&nbsp; 服务器</STRONG></P></TD>
    <TD align=middle width=99 height=29>&nbsp; </TD>
    <TD align=middle width=179 height=29>
      <P 
align=center><STRONG>客户机</STRONG></P></TD></TR></TBODY></TABLE></CENTER></DIV>
<DIV align=center>
<CENTER>
<TABLE height=118 width=588 border=0>
  <TBODY>
  <TR>
    <TD width=221 height=222> 
      <TABLE height=416 width="100%" bgColor=#c0c0c0 border=1>
        <TBODY>
        <TR>
          <TD width="100%" height=37>创建一个<STRONG>Socket</STRONG>
            <P>sFd =<STRONG>socket</STRONG> (AF_INET, SOCK_STREAM, 0) </P></TD></TR>
        <TR>
          <TD width="100%" height=86>把Socket和本机的IP,TCP口绑定
            <P><STRONG>bind</STRONG> (sFd, (struct sockaddr *) &amp;serverAddr, 
            sockAddrSize) </P></TD></TR>
        <TR>
          <TD width="100%" height=5>为客户连接创建等待队列
            <P><STRONG>listen </STRONG>(sFd, SERVER_MAX_CONNECTIONS)</P></TD></TR>
        <TR>
          <TD width="100%" bgColor=#00ffff 
height=38>这里循环等待,如有客户连接请求,则接受客户机连接要求
            <P><STRONG>accept</STRONG> (sFd, (struct sockaddr *) 
            &amp;clientAddr, &amp;sockAddrSize)) </P></TD></TR>
        <TR>
          <TD width="100%" height=86>接受客户机发来的信息
            <P><STRONG>fioRead</STRONG> (sFd, (char *) &amp;clientRequest, 
            sizeof (clientRequest))) </P></TD></TR>
        <TR>
          <TD width="100%" height=64>给客户机发信息
            <P><STRONG>write </STRONG>(sFd, replyMsg, sizeof (replyMsg)) 
        </P></TD></TR>
        <TR>
          <TD width="100%" height=64>关闭Socket,通信终止
            <P><STRONG>close</STRONG> (sFd)</P>
            <P> </P></TD></TR></TBODY></TABLE></TD>
    <TD width=103 height=118>两个浅蓝色的函数是两个Sockets的握手函数,即在这里建立连接</TD>
    <TD width=246 height=118>
      <TABLE height=540 width="100%" bgColor=#c0c0c0 border=1>
        <TBODY>
        <TR>
          <TD width="100%" height=53>创建一个<STRONG>Socket</STRONG>
            <P>sFd =<STRONG>socket </STRONG>(AF_INET, SOCK_STREAM, 0) </P></TD></TR>
        <TR>
          <TD width="100%" 
          height=136>注意:客户端不需要绑定,因为<STRONG>TCP</STRONG>口是动态的.</TD></TR>
        <TR>
          <TD width="100%" bgColor=#00ffff 
            height=119>向服务器端发送连接请求,并在这里等待回应,如收到回应则向下执行
            <P><STRONG>connect</STRONG> (sFd, (struct sockaddr *) 
            &amp;serverAddr, sockAddrSize) </P></TD></TR>
        <TR>
          <TD width="100%" height=97>服务器接受连接请求后,读服务器发过来的信息
            <P><STRONG>read</STRONG>(sFd,buffer,20)</P></TD></TR>
        <TR>
          <TD width="100%" height=87>向服务器发信息
            <P><STRONG>write</STRONG>(sFd, buffer, 20);</P></TD></TR>
        <TR>
          <TD width="100%" height=12>关闭Sockets
            <P><STRONG>close</STRONG> 
(sFd)</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></CENTER></DIV>
<P>  </P>
<BLOCKQUOTE>
  <UL>
    <LI><BIG><STRONG>UDP服务器端和一个UDP客户端通信的程序过程 </STRONG></BIG></LI></UL></BLOCKQUOTE>
<DIV align=center>
<CENTER>
<TABLE height=29 width=568 border=0>
  <TBODY>
  <TR>
    <TD width=182 height=29>
      <P align=center><STRONG>&nbsp; 服务器</STRONG></P></TD>
    <TD width=99 height=29>&nbsp; </TD>
    <TD width=179 height=29>
      <P 
align=center><STRONG>客户机</STRONG></P></TD></TR></TBODY></TABLE></CENTER></DIV>
<DIV align=center>
<CENTER>
<TABLE height=118 width=588 border=0>
  <TBODY>
  <TR>
    <TD width=221 height=222> 
      <TABLE height=327 width="100%" bgColor=#c0c0c0 border=1>
        <TBODY>
        <TR>
          <TD width="100%" height=37>创建一个<STRONG>Socket</STRONG>
            <P>sFd =<STRONG>socket</STRONG> (AF_INET, SOCK_DGRAM, 0) </P></TD></TR>
        <TR>
          <TD width="100%" height=86>把Socket和本机的IP,TCP口绑定
            <P><STRONG>bind</STRONG> (sFd, (struct sockaddr *) &amp;serverAddr, 
            sockAddrSize) </P></TD></TR>
        <TR>
          <TD width="100%" height=5>不需要创建等待队列
            <P> </P></TD></TR>
        <TR>
          <TD width="100%" height=1>这里循环等待,如有客户连接则接受,并读取客户发来的信息
            <P><STRONG>recvfrom</STRONG> (sFd, (char *) &amp;clientRequest, 
            sizeof (clientRequest), 0, (struct sockaddr *) &amp;clientAddr, 
            &amp;sockAddrSize)</P></TD></TR>
        <TR>
          <TD width="100%" height=64>关闭Socket,通信终止
            <P><STRONG>close</STRONG> (sFd)</P>
            <P> </P></TD></TR></TBODY></TABLE></TD>
    <TD width=103 height=118> 
      <P> </P>
      <P> </P>
      <P> </P>
      <P> </P>
      <P>这里不建立连接,只是数据收发</P>
      <P>&lt;----------</P>
      <P> </P></TD>
    <TD width=246 height=118>
      <TABLE height=487 width="100%" bgColor=#c0c0c0 border=1>
        <TBODY>
        <TR>
          <TD width="100%" height=53>创建一个<STRONG>Socket</STRONG>
            <P>sFd =<STRONG>socket </STRONG>(AF_INET, SOCK_DGRAM, 0) </P></TD></TR>
        <TR>
          <TD width="100%" 
          height=104>注意:客户端不需要绑定,因为<STRONG>UCP</STRONG>口是动态的.</TD></TR>
        <TR>
          <TD width="100%" height=98>向服务器端发送连接请求和信息,这里不等待服务器的响应
            <P><STRONG>sendto</STRONG> (sFd, (caddr_t) &amp;myRequest, sizeof 
            (myRequest), 0, (struct sockaddr *) &amp;serverAddr, sockAddrSize) 
            </P></TD></TR>
        <TR>
          <TD width="100%" height=12>关闭Sockets
            <P><STRONG>close</STRONG> 
(sFd)</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></CENTER></DIV>
<P></P>
<P><B><FONT size=4>UDP广播包程序示例</FONT></B></P>
<BLOCKQUOTE>
  <P><B><FONT 
  size=4><BR></FONT></B>发送UDP广播包,查找本地网络中所有运行SNMP服务设备的IP信息。<BR>winsock, 
  win32控制台程序。<BR></P>
  <BLOCKQUOTE>
    <P>#define SNMP_PORT 161<BR>void 
    SearchIP_In_LAN()&nbsp;<BR>{&nbsp;<BR>WSADATA wsaData;&nbsp;<BR>SOCKET 
    sockListener;<BR>SOCKADDR_IN saUdpCli,saUdpServ;<BR>BOOL fBroadcast = 
    TRUE;<BR><BR>char sendBuff[40]= 
    //SNMP查询包内容<BR>{&nbsp;<BR>'\x30','\x26','\x02','\x01','\x00',&nbsp;<BR>'\x04','\x06','\x70','\x75','\x62',&nbsp;<BR>'\x6c','\x69','\x63','\xa0','\x19',&nbsp;<BR>'\x02','\x01','\x03','\x02','\x01',&nbsp;<BR>'\x00','\x02','\x01','\x00','\x30',&nbsp;<BR>'\x0e','\x30','\x0c','\x06','\x08',&nbsp;<BR>'\x2b','\x06','\x01','\x02','\x01',&nbsp;<BR>'\x01','\x01','\x00','\x05','\x00'&nbsp;<BR>};&nbsp;<BR>char 
    recvBuff[600];&nbsp;<BR>int nSize,err,timeoutnum = 1;;<BR>ULONG 
    HostIpAddr;<BR>int rv;&nbsp;<BR>int 
    ncount=0;&nbsp;<BR><BR>if(WSAStartup(MAKEWORD( 2, 
    2),&amp;wsaData)!=0)&nbsp;<BR>{<BR>printf("Winsocket start 
    error!\n");<BR>return;&nbsp;<BR>}<BR><BR>sockListener=socket(AF_INET,SOCK_DGRAM,0);&nbsp;<BR>err 
    = setsockopt ( sockListener,<BR>SOL_SOCKET,<BR>SO_BROADCAST,&nbsp;<BR>(CHAR 
    *)&amp;fBroadcast,<BR>sizeof ( BOOL ));&nbsp;<BR>if ( SOCKET_ERROR == err ) 
    return ;<BR><BR>int TimeOut=10000;<BR><BR>err = setsockopt ( 
    sockListener,&nbsp;<BR>SOL_SOCKET,<BR>SO_RCVTIMEO,<BR>(CHAR *) 
    &amp;TimeOut,<BR>sizeof (TimeOut)<BR>);<BR>if ( SOCKET_ERROR == err ) return 
    ;<BR><BR>saUdpCli.sin_family = AF_INET;&nbsp;<BR>saUdpCli.sin_port = 
    htons(0);&nbsp;<BR>saUdpCli.sin_addr.s_addr = 
    htonl(INADDR_ANY);&nbsp;<BR>if(bind( sockListener, (SOCKADDR 
    *)&amp;saUdpCli, sizeof(SOCKADDR_IN))!=0)&nbsp;<BR>{&nbsp;<BR>printf("Can't 
    bind socket to local port!Program 
    stop.\n");<BR>return;&nbsp;<BR>}&nbsp;<BR><BR>err = 
    gethostname(recvBuff,sizeof(recvBuff));<BR>if ( SOCKET_ERROR == err 
    )<BR>return ;<BR><BR>struct hostent *hp = gethostbyname(recvBuff);<BR>if 
    (!hp) return ;<BR><BR>saUdpServ.sin_family = 
    AF_INET;&nbsp;<BR><BR>memcpy(&amp;(saUdpServ.sin_addr),hp-&gt;h_addr,hp-&gt;h_length);<BR>HostIpAddr=ntohl(saUdpServ.sin_addr.s_addr);<BR><BR>saUdpServ.sin_addr.s_addr 
    = htonl ( INADDR_BROADCAST );&nbsp;<BR>saUdpServ.sin_port = htons 
    (SNMP_PORT);<BR>nSize = sizeof ( SOCKADDR_IN );&nbsp;<BR>if((rv = sendto ( 
    sockListener,<BR>sendBuff,<BR>sizeof(sendBuff),0,<BR>(SOCKADDR *) 
    &amp;saUdpServ,<BR>sizeof ( SOCKADDR_IN 
    )))==SOCKET_ERROR)&nbsp;<BR>{&nbsp;<BR>printf("Send 
    error!\n");<BR>closesocket(*((SOCKET*)sockListener));&nbsp;<BR>return;&nbsp;<BR>}&nbsp;<BR>printf("Send 
    successful!\n");<BR>do // waiting to receive<BR>{<BR>if((rv=recvfrom ( 
    sockListener,<BR>recvBuff,sizeof(recvBuff),0,<BR>(SOCKADDR *) 
    &amp;saUdpCli,&amp;nSize))==SOCKET_ERROR)&nbsp;<BR>{&nbsp;<BR>if(WSAGetLastError()==WSAETIMEDOUT)<BR>{<BR>if 
    (timeoutnum &gt; 6 ) //Wait for 1 
    minute.<BR>break;<BR>else<BR>timeoutnum++;<BR>}<BR>}<BR>else<BR>{<BR>CString 
    Ip(inet_ntoa ( saUdpCli.sin_addr ));<BR>printf("\t IP Adress-&gt; %s 
    ",Ip);<BR>}&nbsp;<BR><BR>}while(TRUE);<BR>closesocket(*((SOCKET*)sockListener));&nbsp;<BR>return 
    ;<BR>}</P></BLOCKQUOTE></BLOCKQUOTE>
<P> </P>
<P><FONT 
color=#0000ff><STRONG><BIG><BIG>网络软件设计框图(个人理解,仅供参考)</BIG></BIG></STRONG></FONT></P>
<P><SMALL><SMALL><SMALL><SMALL><SMALL><SMALL><FONT 
color=#0000ff><STRONG> </STRONG></FONT></SMALL></SMALL></SMALL></SMALL></SMALL></SMALL> 

<UL>
  <LI><STRONG><BIG><BIG>设备管理服务软件架构</BIG></BIG></STRONG> </LI></UL>
<DIV align=center>
<CENTER>
<TABLE height=117 width=700 border=0>
  <TBODY>
  <TR>
    <TD width=696 height=117>&nbsp; 
      <DIV align=center>
      <CENTER>
      <TABLE height=278 cellSpacing=0 cellPadding=0 width="97%" border=0>
        <TBODY>
        <TR>
          <TD width="54%" height=126>
            <DIV align=center>
            <CENTER>
            <TABLE width="100%" bgColor=#c0c0c0 border=1>
              <TBODY>
              <TR>
                <TD width="100%" colSpan=2>
                  <P align=center><STRONG>PC</STRONG>端(Windows 
                  <STRONG>NT</STRONG>,<STRONG>UNIX</STRONG>,Novell...平台)<BR>管理服务器(HTTP,FTP...Server)</P></TD></TR>
              <TR>
                <TD width="100%" colSpan=2> </TD></TR>
              <TR>
                <TD width="100%" colSpan=2>
                  <P align=center>服务器程序</P></TD></TR>
              <TR>
                <TD width="49%">
                  <P align=center>程序接收用户请求,向下发送</P></TD>
                <TD width="51%">
                  <P 
            align=center>从下接收处理结果,发送给用户</P></TD></TR></TBODY></TABLE></CENTER></DIV>
            <DIV align=center>
            <CENTER>
            <TABLE height=68 width="100%" border=0>
              <TBODY>
              <TR>
                <TD width="50%" height=62>
                  <P align=center>|<BR>|<BR>|<BR>v</P></TD>
                <TD width="50%" height=62>
                  <P 
            align=center>^<BR>|<BR>|<BR>|</P></TD></TR></TBODY></TABLE></CENTER></DIV>
            <DIV align=center>
            <CENTER>
            <TABLE height=59 width="57%" border=1>
              <TBODY>
              <TR>
                <TD width="100%" height=1>
                  <P align=center>通信方式:Socket, 
            Namedpipe等</P></TD></TR></TBODY></TABLE></CENTER></DIV>
            <P align=center>||<BR>||<BR>||</P>
            <DIV align=center>
            <CENTER>
            <TABLE height=62 width="100%" bgColor=#c0c0c0 border=1>
              <TBODY>
              <TR>
                <TD width="100%" height=16>
                  <P align=center>后台核心程序</P></TD></TR>
              <TR>
                <TD width="100%" height=16>
                  <P align=center>处理请求,接收发送数据</P></TD></TR>
              <TR>
                <TD width="100%" height=8> </TD></TR>
              <TR>
                <TD width="100%" height=1>
                  <P 
            align=center><STRONG>SNMP</STRONG>客户</P></TD></TR></TBODY></TABLE></CENTER></DIV></TD>
          <TD width="24%" height=423>
            <DIV align=center>
            <CENTER>
            <TABLE width="100%" border=0>
              <TBODY>
              <TR>
                <TD width="100%">
                  <P 
                  align=center><STRONG>&lt;=============&gt;<BR>Internet(HTTP,..协议)</STRONG></P></TD></TR>
              <TR>
                <TD width="100%">
                  <P align=center>如用户PC缺少相应驱动,服务器会动态为用户PC加载安装</P>
                  <P> </P>
                  <P> </P></TD></TR>
              <TR>
                <TD width="100%"></TD></TR>
              <TR>
                <TD width="100%"></TD></TR>
              <TR>
                <TD width="100%"></TD></TR>
              <TR>
                <TD width="100%"> 
                  <P> </P>
                  <P> </P>
                  <P> </P></TD></TR>
              <TR>
                <TD width="100%"></TD></TR>
              <TR>
                <TD width="100%"></TD></TR>
              <TR>
                <TD width="100%">
                  <P 
align=center><STRONG>Internet<BR>(SNMP协议)</STRONG></P></TD></TR>
              <TR>
                <TD 
              width="100%"><STRONG>&lt;=============&gt;</STRONG></TD></TR></TBODY></TABLE></CENTER></DIV>
            <P align=center> </P></TD>
          <TD width="26%" height=423>
            <DIV align=center>
            <CENTER>
            <TABLE width="100%" border=1>
              <TBODY>
              <TR>

⌨️ 快捷键说明

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