📄 网络协议.htm
字号:
<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> 服务器</STRONG></P></TD>
<TD align=middle width=99 height=29> </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 *) &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 *)
&clientAddr, &sockAddrSize)) </P></TD></TR>
<TR>
<TD width="100%" height=86>接受客户机发来的信息
<P><STRONG>fioRead</STRONG> (sFd, (char *) &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 *)
&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> 服务器</STRONG></P></TD>
<TD width=99 height=29> </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 *) &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 *) &clientRequest,
sizeof (clientRequest), 0, (struct sockaddr *) &clientAddr,
&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><----------</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) &myRequest, sizeof
(myRequest), 0, (struct sockaddr *) &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() <BR>{ <BR>WSADATA wsaData; <BR>SOCKET
sockListener;<BR>SOCKADDR_IN saUdpCli,saUdpServ;<BR>BOOL fBroadcast =
TRUE;<BR><BR>char sendBuff[40]=
//SNMP查询包内容<BR>{ <BR>'\x30','\x26','\x02','\x01','\x00', <BR>'\x04','\x06','\x70','\x75','\x62', <BR>'\x6c','\x69','\x63','\xa0','\x19', <BR>'\x02','\x01','\x03','\x02','\x01', <BR>'\x00','\x02','\x01','\x00','\x30', <BR>'\x0e','\x30','\x0c','\x06','\x08', <BR>'\x2b','\x06','\x01','\x02','\x01', <BR>'\x01','\x01','\x00','\x05','\x00' <BR>}; <BR>char
recvBuff[600]; <BR>int nSize,err,timeoutnum = 1;;<BR>ULONG
HostIpAddr;<BR>int rv; <BR>int
ncount=0; <BR><BR>if(WSAStartup(MAKEWORD( 2,
2),&wsaData)!=0) <BR>{<BR>printf("Winsocket start
error!\n");<BR>return; <BR>}<BR><BR>sockListener=socket(AF_INET,SOCK_DGRAM,0); <BR>err
= setsockopt ( sockListener,<BR>SOL_SOCKET,<BR>SO_BROADCAST, <BR>(CHAR
*)&fBroadcast,<BR>sizeof ( BOOL )); <BR>if ( SOCKET_ERROR == err )
return ;<BR><BR>int TimeOut=10000;<BR><BR>err = setsockopt (
sockListener, <BR>SOL_SOCKET,<BR>SO_RCVTIMEO,<BR>(CHAR *)
&TimeOut,<BR>sizeof (TimeOut)<BR>);<BR>if ( SOCKET_ERROR == err ) return
;<BR><BR>saUdpCli.sin_family = AF_INET; <BR>saUdpCli.sin_port =
htons(0); <BR>saUdpCli.sin_addr.s_addr =
htonl(INADDR_ANY); <BR>if(bind( sockListener, (SOCKADDR
*)&saUdpCli, sizeof(SOCKADDR_IN))!=0) <BR>{ <BR>printf("Can't
bind socket to local port!Program
stop.\n");<BR>return; <BR>} <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; <BR><BR>memcpy(&(saUdpServ.sin_addr),hp->h_addr,hp->h_length);<BR>HostIpAddr=ntohl(saUdpServ.sin_addr.s_addr);<BR><BR>saUdpServ.sin_addr.s_addr
= htonl ( INADDR_BROADCAST ); <BR>saUdpServ.sin_port = htons
(SNMP_PORT);<BR>nSize = sizeof ( SOCKADDR_IN ); <BR>if((rv = sendto (
sockListener,<BR>sendBuff,<BR>sizeof(sendBuff),0,<BR>(SOCKADDR *)
&saUdpServ,<BR>sizeof ( SOCKADDR_IN
)))==SOCKET_ERROR) <BR>{ <BR>printf("Send
error!\n");<BR>closesocket(*((SOCKET*)sockListener)); <BR>return; <BR>} <BR>printf("Send
successful!\n");<BR>do // waiting to receive<BR>{<BR>if((rv=recvfrom (
sockListener,<BR>recvBuff,sizeof(recvBuff),0,<BR>(SOCKADDR *)
&saUdpCli,&nSize))==SOCKET_ERROR) <BR>{ <BR>if(WSAGetLastError()==WSAETIMEDOUT)<BR>{<BR>if
(timeoutnum > 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-> %s
",Ip);<BR>} <BR><BR>}while(TRUE);<BR>closesocket(*((SOCKET*)sockListener)); <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>
<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><=============><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><=============></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 + -