查看完整版本: TCP/IP通信程序设计方式

wdefu 2007-3-10 09:33

TCP/IP通信程序设计方式

[font=宋体][size=12pt]刚接触[/size][/font][size=12pt][font=Times New Roman]TCP/IP[/font][/size][font=宋体][size=12pt]通信设计的人根据范例可以很快编出一个通信程序,据此一些人可能会认为[/size][/font][size=12pt][font=Times New Roman]TCP/IP[/font][/size][font=宋体][size=12pt]编程很简单。其实不然,[/size][/font][size=12pt][font=Times New Roman]TCP/IP[/font][/size][font=宋体][size=12pt]编程具有较为丰富的内容。其编程的丰富性主要体现在通信方式和报文格式的多样性上。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][font=宋体][size=15pt]一。通信方式[/size][/font][/b][b][size=15pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]主要有以下三大类[/size][/font][size=12pt][font=Times New Roman]:[/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]([/font][/size][/b][b][font=宋体][size=12pt]一[/size][/font][/b][b][size=12pt][font=Times New Roman])SERVER/CLIENT[/font][/size][/b][b][font=宋体][size=12pt]方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman]1.[/font][/size][font=宋体][size=12pt]一个[/size][/font][size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]方连接一个[/size][/font][size=12pt][font=Times New Roman]Server[/font][/size][font=宋体][size=12pt]方,或称点对点[/size][/font][size=12pt][font=Times New Roman](peer to peer)[/font][/size][font=宋体][size=12pt]。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman]2.[/font][/size][font=宋体][size=12pt]多个[/size][/font][size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]方连接一个[/size][/font][size=12pt][font=Times New Roman]Server[/font][/size][font=宋体][size=12pt]方,这也是通常的并发服务器方式。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman]3.[/font][/size][font=宋体][size=12pt]一个[/size][/font][size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]方连接多个[/size][/font][size=12pt][font=Times New Roman]Server[/font][/size][font=宋体][size=12pt]方,这种方式很少见,主要用于一个客户向多个服务器发送请求情况。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]([/font][/size][/b][b][font=宋体][size=12pt]二[/size][/font][/b][b][size=12pt][font=Times New Roman])[/font][/size][/b][b][font=宋体][size=12pt]连接方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]1.[/font][/size][/b][b][font=宋体][size=12pt]长连接[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]方与[/size][/font][size=12pt][font=Times New Roman]Server[/font][/size][font=宋体][size=12pt]方先建立通讯连接,连接建立后不断开,然后再进行报文发送和接收。这种方式下由于通讯连接一直存在,可以用下面命令查看连接是否建立:[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[align=center][align=center][size=12pt][font=Times New Roman]netstat [/font][/size][font=宋体][size=12pt]–[/size][/font][size=12pt][font=Times New Roman]f inet|grep [/font][/size][font=宋体][size=12pt]端口号[/size][/font][size=12pt][font=Times New Roman]([/font][/size][font=宋体][size=12pt]如[/size][/font][size=12pt][font=Times New Roman]5678)[/font][/size][font=宋体][size=12pt]。[/size][/font][size=12pt][/size][/align][/align][size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]此种方式常用于点对点通讯。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]2.[/font][/size][/b][b][font=宋体][size=12pt]短连接[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]方与[/size][/font][size=12pt][font=Times New Roman]Server[/font][/size][font=宋体][size=12pt]每进行一次报文收发交易时才进行通讯连接,交易完毕后立即断开连接。此种方式常用于一点对多点通讯,比如多个[/size][/font][size=12pt][font=Times New Roman]Client[/font][/size][font=宋体][size=12pt]连接一个[/size][/font][size=12pt][font=Times New Roman]Server.[/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]([/font][/size][/b][b][font=宋体][size=12pt]三[/size][/font][/b][b][size=12pt][font=Times New Roman])[/font][/size][/b][b][font=宋体][size=12pt]发送接收方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]1.[/font][/size][/b][b][font=宋体][size=12pt]异步[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]报文发送和接收是分开的,相互独立的,互不影响。这种方式又分两种情况:[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman](1)[/font][/size][font=宋体][size=12pt]异步双工:接收和发送在同一个程序中,有两个不同的子进程分别负责发送和接收[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman](2)[/font][/size][font=宋体][size=12pt]异步单工:接收和发送是用两个不同的程序来完成。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]2.[/font][/size][/b][b][font=宋体][size=12pt]同步[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]报文发送和接收是同步进行,既报文发送后等待接收返回报文。同步方式一般需要考虑超时问题,即报文发上去后不能无限等待,需要设定超时时间,超过该时间发送方不再等待读返回报文,直接通知超时返回。[/size][/font][size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]实际通信方式是这三类通信方式的组合。比如一般书上提供的[/size][/font][size=12pt][font=Times New Roman]TCP/IP[/font][/size][font=宋体][size=12pt]范例程序大都是同步短连接的[/size][/font][size=12pt][font=Times New Roman]SERVER/CLIENT[/font][/size][font=宋体][size=12pt]程序。有的组合是基本不用的,比较常用的有价值的组合是以下几种:[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[font=Wingdings][size=12pt]l        [/size][/font][font=宋体][size=12pt]同步短连接[/size][/font][size=12pt][font=Times New Roman]Server/Client  [/font][/size]
[font=Wingdings][size=12pt]l        [/size][/font][font=宋体][size=12pt]同步长连接[/size][/font][size=12pt][font=Times New Roman]Server/Client[/font][/size]
[font=Wingdings][size=12pt]l        [/size][/font][font=宋体][size=12pt]异步短连接[/size][/font][size=12pt][font=Times New Roman]Server/Client[/font][/size]
[font=Wingdings][size=12pt]l        [/size][/font][font=宋体][size=12pt]异步长连接双工[/size][/font][size=12pt][font=Times New Roman]Server/Client[/font][/size]
[font=Wingdings][size=12pt]l        [/size][/font][font=宋体][size=12pt]异步长连接单工[/size][/font][size=12pt][font=Times New Roman]Server/Client[/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]其中异步长连接双工是最为复杂的一种通信方式,有时候经常会出现在不同银行或不同城市之间的两套系统之间的通信。比如金卡工程。由于这几种通信方式比较固定,所以可以预先编制这几种通信方式的模板程序。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][font=宋体][size=15pt]二[/size][/font][/b][b][size=15pt][font=Times New Roman].[/font][/size][/b][b][font=宋体][size=15pt]报文格式[/size][/font][/b][b][size=15pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]通信报文格式多样性更多,相应地就必须设计对应的读写报文的接收和发送报文函数。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]([/font][/size][/b][b][font=宋体][size=12pt]一[/size][/font][/b][b][size=12pt][font=Times New Roman])[/font][/size][/b][b][font=宋体][size=12pt]阻塞与非阻塞方式 [/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]1.[/font][/size][/b][b][font=宋体][size=12pt]非阻塞方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]读函数不停地进行读动作,如果没有报文接收到,等待一段时间后超时返回,这种情况一般需要指定超时时间。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]2.[/font][/size][/b][b][font=宋体][size=12pt]阻塞方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]如果没有报文接收到,则读函数一直处于等待状态,直到有报文到达。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]([/font][/size][/b][b][font=宋体][size=12pt]二[/size][/font][/b][b][size=12pt][font=Times New Roman])[/font][/size][/b][b][font=宋体][size=12pt]循环读写方式[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]1.[/font][/size][/b][b][font=宋体][size=12pt]一次直接读写报文[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]在一次接收或发送报文动作中一次性不加分别地全部读取或全部发送报文字节。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]2.[/font][/size][/b][b][font=宋体][size=12pt]不指定长度循环读写[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]这一般发生在短连接进程中,受网络路由等限制,一次较长的报文可能在网络传输过程中被分解成了好几个包。一次读取可能不能全部读完一次报文,这就需要循环读报文,直到读完为止。[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[b][size=12pt][font=Times New Roman]3.[/font][/size][/b][b][font=宋体][size=12pt]带长度报文头循环读写[/size][/font][/b][b][size=12pt][/size][/b]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]这种情况一般是在长连接进程中,由于在长连接中没有条件能够判断循环读写什么时候结束,所以必须要加长度报文头。读函数先是读取报文头的长度,再根据这个长度去读报文[/size][/font][size=12pt][font=Times New Roman].[/font][/size][font=宋体][size=12pt]实际情况中,报头的码制格式还经常不一样,如果是非[/size][/font][size=12pt][font=Times New Roman]ASCII[/font][/size][font=宋体][size=12pt]码的报文头,还必须转换成[/size][/font][size=12pt][font=Times New Roman]ASCII,[/font][/size][font=宋体][size=12pt]常见的报文头码制有:[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman](1)n[/font][/size][font=宋体][size=12pt]个字节的[/size][/font][size=12pt][font=Times New Roman]ASCII[/font][/size][font=宋体][size=12pt]码[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman](2)n[/font][/size][font=宋体][size=12pt]个字节的[/size][/font][size=12pt][font=Times New Roman]BCD[/font][/size][font=宋体][size=12pt]码[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman](3)n[/font][/size][font=宋体][size=12pt]个字节的网络整型码[/size][/font][size=12pt][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]以上是几种比较典型的读写报文方式,可以与通信方式模板一起预先提供一些典型的[/size][/font][size=12pt][font=Times New Roman]API[/font][/size][font=宋体][size=12pt]读写函数。当然在实际问题中,可能还必须编写与对方报文格式配套的读写[/size][/font][size=12pt][font=Times New Roman]API.[/font][/size]
[size=12pt][font=Times New Roman] [/font][/size]
[font=宋体][size=12pt]在实际情况中,往往需要把我们自己的系统与别人的系统进行连接,有了以上模板与[/size][/font][size=12pt][font=Times New Roman]API,[/font][/size][font=宋体][size=12pt]可以说连接任何方式的通信程序都不存在问题。[/size][/font][size=12pt][/size]
页: [1]
查看完整版本: TCP/IP通信程序设计方式