中文3078字使用LabVIEW中的TCP/IP和UDP协议前言互联网络协议(IP),用户数据报协议(UDP)和传输控制协议(TCP)是网络通信的基本的工具。
TCP与IP的名称来自于一组最著名的因特网协议中的两个--传输控制协议和互联网络协议。
你能使用TCP/IP来进行单一网络或者互连网络间的通信。
单独的网络会被大的地理距离分隔。
TCP/IP把数据从一个子网网络或者因特网连接的计算机发送到另一个上。
因为TCP/IP 在大多数计算机上是可用的,它能在多样化的系统中间传送信息。
LabVIEW和TCP/IP你能在所有平台上的LabVIEW中使用TCP/IP。
LabVIEW包含了TCP和UDP程序还有能让你建立客户端或者服务器程序的功能。
IPIP执行低层次的计算机间的数据传送。
在组成部分里的IP数据包称为数据报。
一个数据报包含表明来源和目的地地址的数据和报头字。
IP为通过网络或者因特网把数据发送到指定的目的地的数据报确定正确的路径。
IP协议并不能保证发送。
事实上,如果数据报在传输中被复制,IP可能多次传送一个单独的数据报。
所以,程序很少用IP而是用TCP或者UDP代替。
UDPUDP在计算机进程中提供简单而低层次的通信。
进程通过把数据报发送到一个目的地计算机或者端口进行通信。
一个端口是你发送数据的位置。
IP处理计算机对计算机的发送。
在数据报到达目的地计算机后,UDP把数据报移动到其目的端口。
如果目的端口不是开放的,UDP 将删除数据报。
UDP将发生IP的同样的发送问题。
应用程序的UDP的可靠性不强。
例如,一项应用程序能经常把大量信息的数据传送到目的地而丢失少量的数据是肯定的。
在LabVIEW中使用UDP协议因为UDP不是一个TCP似的一个以连接为基础的协议,在你发送或者收到数据之前,你不需要和目的地建立一种连接。
相反,当你每发送一个数据报时,由你指定数据目的地。
操作系统不会报告传输差错使用UDP打开功能在一个端口上打开一个UDP插口。
同时打开的UDP端口的数量仍依赖于操作系统。
UDP的打开的功能返回到网络连接时唯一识别UDP套接字。
使用这种连接在子VI程序中就是靠这个套接字。
用UDP编写功能发送数据到目的地,然后使用UDP阅读功能阅读那个数据。
每写一个操作要求一个目的地地址和端口。
每阅读一个操作包含来源地址和端口。
UDP保存你所发送的每一个命令所指定的数据报字节。
在理论上,你能发送任何大小的数据报。
然而,你最好不使用UDP来发送大的数据报,因为它不像TCP一样可靠。
当你在一个端口上结束所有通信时,使用UDP结束功能去释放系统资源。
UDP多路广播你能使用UDP的功能来与一个单独的客户端通信或者通过广播对于所有局域网上的计算机通信。
如果你想要传送成倍增加具体的计算机信息,你必须通过客户端列表构建UDP功能反复执行。
因为LabVIEW能把数据的作为一个单独的拷贝发送到每个客户端并且保存对收到数据感兴趣的客户端列表,所以运用这种技术建立完全相同的网络交换。
使用多路广播可以在网络上的进行单个的发送端和多倍的客户端之间的通信而不需要保存客户列表或者要求发送端把数据多重拷贝后发送给每个客户端。
为了通过多路广播收到数据广播,所有客户端必须加入一个多路广播组。
而发送端不需要加入该组。
发送端只需规定一个多路广播的IP地址来定义这个多路广播组。
多路广播的IP地址范围在224.0.0.0到239.255.255.255中。
当一个客户端想要加入一个多路广播组时,它只需预定这个组的多路广播的IP地址。
在客户端预定到一个多路广播组之后,客户端收到从多路广播的IP 地址发送的数据。
对于LabVIEW中的多路广播,使用UDP多路广播的打开程序有能力读、写或者读和写UDP 数据。
规定TTL为写数据,多路广播地址为读数据,多路广播端口号为读写数据。
默认TTL是1,这意味着LabVIEW仅仅把数据报发送到本地的网络上。
当一个发送方收到一个多路广播数据报时,它消耗数据报的生存时间。
如果TTL大于1,发送方把数据报转发到其它数据报。
下面的表格说明了当你规定一个TTL的值时,一个多路广播数据报的运行。
0 主机上还存留数据报1 数据报发送到每个在同一本地网络上已预定那个IP地址的客户端。
中枢/转发器和桥路/开关转发数据报。
发送方不转发数据报如果TTL为1。
如果你规定一个值大于1,数据报通过TTL-1层被发送,并且发送方转发它。
参阅在LabVIEW\例子\comm\UDP.llb中的UDP多路广播接受方程序和UDP多路广播发送方程序VI中使用UDP多路广播的例程。
TCPTCP保证在网络中的可靠的通信,按顺序发送而没有差错,损失或者重发。
TCP直到收到一个确认才转发。
系统要求在使用TCP/IP之前,确认你有必需的要求,这变化依赖于你使用的平台。
(Windows和UNIX系统)TCP/IP。
你不需要使用第三种产品连接使用TCP/IP。
如果你的网络适当地被构成,LabVIEW没有要求附加的设置。
(Mac OS系统) LabVIEW网络要求打开传送,包括Mac OS 7.5和更新的版本。
在LabVIEW中使用TCPTCP是一个以连接为基础的协议,这意味着站点必须在传送数据之前建立一种连接。
TCP许可多重、同时的连接。
你可以通过等待一种输入的连接或者通过积极地寻求一种与指定的地址连接建立连接。
在建立TCP连接时,你必须指定一个地址和该地址的端口。
端口范围在0到65,535点之间。
UNIX系统为特殊的应用程序保留端口数少于1,024。
在一个给定的地址中不同的端口能识别不同的服务。
使用TCP的打开连接功能来与一个指定地址进行积极的连接。
如果连接成功,功能返回网络连接的返回数唯一识别那个连接。
使用这种连接refnum来访问子VI程序。
你能使用如下技术来等输入连接:用TCP听程序创建立一个听者并且在一个指定的端口等待一种可接受的TCP连接。
如果连接成功,VI程序返回连接数,地址和远程TCP客户端口。
TCP创建听者功创建一个听者然后用TCP的听者功能上等待的一个听者接受新的连接。
在听者功能上的TCP等待返回你所发送的功能和返回数的同样的听者ID。
当你结束等候新的连接时,用TCP关闭连接功能区关闭一个听者。
你不能对一个听者进行读写。
用第二种技术的优势是你能使用TCP关闭连接功能来取消听操作,这在你想要为了一种连接听而没有超时,但是你想要取消当另一个条件变真时的听时是很有用的。
你能在任何时间关闭听程序。
当你建立一种连接时,用TCP读功能和TCP写功能能在远程应用中队数据进行读写。
用TCP关闭连接功能区关闭远程连接。
如果没被读的仍保留着而关闭连接,你可能失去数据。
当关闭连接时,在你的计算机上使用较高层次协议。
在连接被关闭后,你不能再读写它。
TCP和UDP之间的区别在你想要可靠的数据传输时,TCP是最好的协议。
UDP是高性能的无需连接的协议,但是它不保证可靠性。
建立TCP客户端注意:请参考最近的版本LabVIEW帮助功能对这些指令和功能的细节描述。
完成如下步骤使用TCP功能建立一个TCP顾客。
1.用TCP的打开连接功能来打开一个服务器的连接。
你必须规定服务器的因特网地址和服务器的端口。
地址在网络上识别计算机。
远程端口在服务器使用听的计算机上识别一个通信通道。
当你建立一个TCP服务器时,你要规定你想要服务器为通信使用的端口。
2.用TCP的写功能把信息发送到服务器上。
3.用TCP的读功能来读发自服务器的信息。
你必须规定你想读的字节数。
4.用TCP的关闭功能来关闭与服务器的连接。
请参阅labview\examples\comm\TCP.llb中对单一数据客户端的TCP例程。
超时和差错当你设计一种网络应用时,应细心考虑失败是如何发生的。
例如,如果服务器被毁,确定如何使每个客户VI程序掌控它。
一种解决方式是确保每个客户VI程序有超时功能。
如果发生没能在一定量的时间之后生产结果,客户端继续执行。
在继续时,客户端能试图重建连接或者报告出错。
如果必要的话,客户端VI程序能关闭应用程序。
建立TCP服务器注意:请参考最近的版本LabVIEW帮助功能对这些指令和功能的细节描述。
完全如下步骤用TCP功能来建立一个TCP服务器。
1.使用TCP听程序等待连接。
你必须规定端口。
这个端口必须是客户端试图访问的同一个端口。
2.如果连接被建立,用TCP读功能从接受到信息的端口读信息。
3.用TCP写功能返回结果。
数据必须是一种客户端能接受的形式。
4.用TCP关连接功能来关闭连接。
请参阅labview\examples\comm\TCP.llb中对单一数据客户端的TCP例程。
TCP和UDP例子请参阅labview\examples\comm\TCP.llb和labview\examples\comm\UDP.llb中用TCP和UDP的VI程序和功能。
Application Note 160 Using LabVIEW™ with TCP/IP and UDP IntroductionInternet Protocol (IP), User Datagram Protocol (UDP), and Transmission Control Protocol (TCP) are basic tools for network communication. The name TCP/IP comes from two of the best-known protocols of the Internet protocol suite, the Transmission Control Protocol and the Internet Protocol.You can use TCP/IP to communicate over single networks or interconnected networks. The individual networks can be separated by large geographical distances. TCP/IP routes data from one network or Internet-connected computer to another. Because TCP/IP is available on most computers, it can transfer information among diverse systems.LabVIEW and TCP/IPYou can use the TCP/IP protocols with LabVIEW on all platforms. LabVIEW includes TCP and UDP VIs and functions you can use to create client or server VIs.IPIP performs the low-level service of moving data between computers. IP packages data into components called datagrams.A datagram contains the data and a header that indicates the source and destination addresses. IP determines the correct path for the datagram to take across the network or Internet and sends the data to the specified destination.IP cannot guarantee delivery. In fact, IP might deliver a single datagram more than once if the datagram is duplicated in transmission. Programs rarely use IP but use TCP or UDP instead.UDPUDP provides simple, low-level communication among processes on computers. Processes communicate by sending datagrams to a destination computer or port. A port is the location where you send data. IP handles thecomputer-to-computer delivery. After the datagram reaches the destination computer, UDP moves the datagram to its destination port. If the destination port is not open, UDP discards the datagram. UDP shares the same delivery problems of IP.Use UDP in applications in which reliability is not critical. For example, an application might transmit informative data to a destination frequently enough that a few lost segments of data are not problematic.LabVIEW™, National Instruments™, and ™ are trademarks of National Instruments Corporation. Product and company names mentioned herein are trademarks or trade names of their respective companies. For patents covering National Instruments products, refer to the appropriate location: Help»Patents in your software, thepatents.txt file on your CD, or /patents.342028C-01 © 2000–2004 National Instruments Corporation. All rights reserved. March 2004Using UDP in LabVIEWBecause UDP is not a connection-based protocol such as TCP, you do not need to establish a connection with a destination before you send or receive data. Instead, you specify the destination for the data when you send each datagram. Operating systems do not report transmission errors.Use the UDP Open function to open a UDP socket on a port. The number of simultaneously open UDP ports dependson the operating system. The UDP Open function returns a network connection refnum that uniquely identifies theUDP socket. Use this connection refnum to refer to this socket in subsequent VI calls.Use the UDP Write function to send data to a destination, and use the UDP Read function to read that data. Each write operation requires a destination address and port. Each read operation contains the source address and port. UDP preserves the datagram bytes that you specified for each command you send.In theory, you can send datagrams of any size. However, you typically would not use UDP to send large datagrams because it is not as reliable as TCP.When you finish all communications on a port, use the UDP Close function to free system resources.UDP MulticastYou can use the UDP functions to communicate to a single client (single-cast) or to all computers on the subnet througha broadcast. If you want to communicate to multiple specific computers, you must configure the UDP functions to iterate through a list of clients. Using this technique creates duplicate network traffic because LabVIEW sends a separate copy of the data to each client and maintains a list of clients interested in receiving the data.Use multicasting to communicate between a single sender and multiple clients on a network without requiring the sender to maintain a list of clients or send multiple copies of the data to each client. To receive data broadcast by a multicast sender, all clients must join a multicast group. The sender does not have to join a group to send data. The sender specifies a multicast IP address, which defines a multicast group. Multicast IP addresses are in the 224.0.0.0to 239.255.255.255 range. When a client wants to join a multicast group, it subscribes to the multicast IP addressof the group. After subscribing to a multicast group, the client receives data sent to the multicast IP address.To multicast in LabVIEW, use the UDP Multicast Open VI to open connections capable of reading, writing, or reading and writing UDP data. Specify the time-to-live (TTL) for writing data, the multicast address for reading data, and the multicast port number for reading and writing data. The default TTL is 1, which means LabVIEW sends the datagram only to the local subnet. When a router receives a multicast datagram, it decrements the datagram TTL. If the TTL is greater than 1, the router forwards the datagram to other routers. The following table lists what action occurs to a multicast datagram when you specify a value for the time-to-live parameter.Datagram remains on the host computer.1 Datagram sent to every client on the same local subnet that subscribes to that IP address. Hubs/repeatersand bridges/switches forward the datagram. Routers do not forward the datagram if the TTL is 1.Application Note 160 2 If you specify a value greater than 1, the datagram is sent and routers forward it through TTL-1 layers.Refer to the UDP Multicast Receiver VI and the UDP Multicast Sender VI in the labview\examples\comm\UDP.llb for examples of using UDP multicasting.TCPTCP ensures reliable transmission across networks, delivering data in sequence without errors, loss, or duplication.TCP retransmits the datagram until it receives an acknowledgment.System RequirementsBefore using TCP/IP, confirm that you have the necessary requirements, which vary depending on the platform you use.(Windows and UNIX) TCP/IP is built in. You do not need to use a third-party product to communicate using TCP/IP.If your network is configured properly, LabVIEW requires no additional setup.(Mac OS) LabVIEW networking requires Open Transport, included in Mac OS 7.5 and later.Using TCP in LabVIEWTCP is a connection-based protocol, which means that sites must establish a connection before transferring data. TCP permits multiple, simultaneous connections.You initiate a connection by waiting for an incoming connection or by actively seeking a connection with a specified address. In establishing TCP connections, you have to specify the address and a port at that address. A number between 0 and 65,535 represents a port. UNIX reserves port numbers less than 1,024 for privileged applications. Different ports at a given address identify different services at that address.Use the TCP Open Connection function to actively establish a connection with a specific address and port. If the connection is successful, the function returns a network connection refnum that uniquely identifies that connection.Use this connection refnum to refer to the connection in subsequent VI calls.You can use the following techniques to wait for an incoming connection:Use the TCP Listen VI to create a listener and wait for an accepted TCP connection at a specified port. If theconnection is successful, the VI returns a connection refnum, the address, and the port of the remote TCP client.Use the TCP Create Listener function to create a listener and use the TCP Wait on Listener function to listen for and accept new connections. The TCP Wait on Listener function returns the same listener ID you wired to the function and the connection refnum for a connection. When you finish waiting for new connections, use the TCP Close Connection function to close a listener. You cannot read from or write to a listener.The advantage of using the second technique is that you can use the TCP Close Connection function to cancel a listen operation, which is useful when you want to listen for a connection without using a timeout, but you want to cancel the listen when another condition becomes true. You can close the listen VI at any time.When you establish a connection, use the TCP Read function and the TCP Write function to read and write data to the remote application.Use the TCP Close Connection function to close the connection to the remote application. If unread data remains and the connection closes, you might lose data. Use a higher level protocol for your computer to determine when to close the connection. After a connection is closed, you cannot read from it or write to it again.© National Instruments Corporation 3 Application Note 160机械专业中英文文献翻译Deciding between TCP and UDPTCP is the best protocol to use if you want reliable data transmission. UDP is a connectionless protocol with higher performance, but it does not ensure reliable data transmission.11机械专业中英文文献翻译Creating a TCP ClientNote Refer to the LabVIEW Help for the most recent version of these instructions and details for each of thefunctions.Complete the following steps to create a TCP client using the TCP functions.1. Use the TCP Open Connection function to open a connection to a server. Y ou must specify the Internet address ofthe server and the port for the server.The address identifies a computer on the network. The remote port identifies a communication channel on the computer that the server uses to listen for communication requests. When you create a TCP server, you specify the port that you want the server to use for communication.2. Use the TCP Write function to send a message to a server.3. Use the TCP Read function to read a message from the server. Y ou must specify the number of bytes you want toread.4. Use the TCP Close Connection function to close the connection to the server.Refer to the Simple Data Client VI in the labview\examples\comm\TCP.llb for an example of a TCP client. Timeouts and ErrorsWhen you design a network application, consider carefully what should happen if something fails. For example, if the server crashes, determine how each client VI handles it.One solution is to make sure that each client VI has a timeout. If something fails to produce results after a certain amount of time, the client continues execution. In continuing, the client can try to reestablish the connection or report the error. If necessary, the client VI can shut down the application.Creating a TCP ServerNote Refer to the LabVIEW Help for the most recent version of these instructions and details for each of thefunctions.Complete the following steps to create a TCP server using the TCP functions.1. Use the TCP Listen VI to wait for a connection. You must specify the port. This port must be the same port that theclient attempts to access.2. If a connection is established, use the TCP Read function to read from that port to retrieve a message.3.Use the TCP Write function to return results. The data must be in a form that the client can accept. 4. Use theTCP Close Connection function to close the connection.Refer to the Simple Data Server VI in the labview\examples\comm\TCP.llb for an example of a TCP server. TCP and UDP ExamplesRefer to the labview\examples\comm\TCP.llb and the labview\examples\comm\UDP.llb for examples of using the TCP and UDP VIs and functions.第12 页共12 页。