LabWindowsCVITCP程序说明1本系统框图如下:2TCP函数说明2.1ClientFunctions客户端ClientTCPRead(unsignedintconversationHandle,void*dataBuffer,size_tdataSize,unsignedinttimeOut);参量1:操作柄、参量2:读入数据、参量3:读入数据长度、参量4:溢出时间。Return:读出数据的字节长度(可用来校验数据)。ClientTCPWrite(unsignedintconversationHandle,void*dataPointer,size_tdataSize,unsignedinttimeOut);参量1:操作柄、参量2:输出数据、参量3:读入数据长度、参量4:溢出时间。Return:写入数据的字节长度(可用来校验数据)。ConnectToTCPServer(unsignedint**conversationHandle,unsignedintportNumber,charserverHostName[],tcpFuncPtrcallbackFunction,void*callbackData,unsignedinttimeOut);参量1:操作柄、参量2:当前端口、参量3:服务器地址、参量4:回调函数、参量5:溢出时间Return:写入数据的字节长度(可用来校验数据)。DisconnectFromTCPServer(unsignedintconversationHandle);参量:操作柄2.2SeverFunctions服务器端DisconnectTCPClient(unsignedintconversationHandle);参量:操作柄RegisterTCPServer(unsignedintportNumber,tcpFuncPtrcallbackFunction,void*callbackData);参量1:当前端口、参量2:回调函数、参量3:返回数据(NULL)ServerTCPRead(unsignedintconversationHandle,void*dataBuffer,size_tdataSize,unsignedinttimeOut);参量1:操作柄、参量2:读入数据、参量3:读入数据长度、参量4:溢出时间。Return:读出数据的字节长度(可用来校验数据)。服务器sever终端Client路由交换机ServerTCPWrite(unsignedintconversationHandle,void*dataPointer,size_tdataSize,unsignedinttimeOut);参量1:操作柄、参量2:输出数据、参量3:读入数据长度、参量4:溢出时间。Return:写入数据的字节长度(可用来校验数据)。UnregisterTCPServer(unsignedintportNumber);UnregisterTCPServerEx(unsignedintportNumber,constcharlocalhostAddress[]);2.3SupportFunctions支持函数GetAllTCPHostAddresses(char****addresses,int**numberOfAddresses);参量1:networkinterfaceaddress、参量2:numberofaddressGetHostTCPSocketHandle(unsignedintconnectionHandle,intptr_t**socketHandle);获取当前操作柄GetTCPHostAddr(charbuffer[],size_tbufferSize);获取本机的IPAddressGetTCPHostConnectionAddr(unsignedintconnectionHandle,charbuffer[],size_tbufferSize);参量1:操作柄、参量2:输出地址、参量3:数据长度。GetTCPHostName(charbuffer[],size_tbufferSize);获取主机(本机)用户名。GetTCPPeerAddr(unsignedintconnectionHandle,charbuffer[],size_tbufferSize);参量1:操作柄、参量2:输出地址、参量3:数据长度。获取对方的IP地址GetTCPPeerName(unsignedintconnectionHandle,charbuffer[],size_tbufferSize);参量1:操作柄、参量2:输出地址、参量3:数据长度。获取对方的用户名SetTCPDisconnectMode(unsignedintconversationHandle,intdisconnectMode);参量1:操作柄、参量2:模式选择。2.4Other其它函数*GetTCPErrorString(interrorNumber);根据当前错误代码,调出当前错误类型。char*GetTCPSystemErrorString(void);获取当前错误代码。intProcessTCPEvents(void);获取当前TCP处理事件。TCPFreeMemory(void*pointerToMemory);释放内存。3例程说明3.1服务器sever/****************************************************************************function:TcpConnect连接TcpDisconnect断开intput:N/Aoutput:Tcpip.tcp_Port(600)TCP端口*****************************************************************************/intCVICALLBACKTcpConnect(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){intstatus=0;chartcp_Port[10];switch(event){caseEVENT_COMMIT:GetCtrlVal(panelHandle,PANEL_STRING_SERVER_PORT,tcp_Port);//获得TCP服务器端口号sscanf(tcp_Port,%u,&Tcpip.tcp_Port);status=RegisterTCPServer(Tcpip.tcp_Port,TcpServerCall,0);if(!status){//注册成功得到主机信息Tcpip.tcp_Using=1;SetCtrlAttribute(panelHandle,PANEL_BUTTON_CONNECT,ATTR_DIMMED,1);SetCtrlAttribute(panelHandle,PANEL_BUTTON_DISCONNECT,ATTR_DIMMED,0);GetHostInfo();//获取主机信息}break;}return0;}/****************************************************************************function:TcpServerCallTCP接收数据处理部分(主机)intput:Tcpip.client_Handle操作柄output:*****************************************************************************/intCVICALLBACKTcpServerCall(unsignedhandle,intevent,interror,void*callbackData){inti;charclient_Ip[50];charrev_Data[200];charRev_Length[50];switch(event){caseTCP_CONNECT:Tcpip.client_Handle=handle;//获得客户端句柄,赋给全局变量GetTCPPeerAddr(Tcpip.client_Handle,client_Ip,50);//获得客户端IP地址SetCtrlVal(panelHandle,PANEL_STRING_CLIENT_IP,client_Ip);//Clientled亮为绿色SetCtrlAttribute(panelHandle,PANEL_LED_CLIENT,ATTR_ON_COLOR,VAL_GREEN);SetCtrlVal(panelHandle,PANEL_LED_CLIENT,1);//Clientled亮//设置TCP断开方式为自动SetTCPDisconnectMode(Tcpip.client_Handle,TCP_DISCONNECT_AUTO);break;caseTCP_DATAREADY:memset(rev_Data,'\0',200);//数组清0Tcpip.tcp_Rev_Length=ServerTCPRead(Tcpip.client_Handle,rev_Data,200,1000);//从TCP服务器读数据//de_protocol(data,read_len,ret_data,&ret_len);//协议解析for(i=0;istrlen(rev_Data);i++){//取出数据Tcpip.tcp_Rev_Data[i]=rev_Data[i];}SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,\n接收到数据:);SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,rev_Data);SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,\n);SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,***************************************************);SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,\n);sprintf(Rev_Length,ReadfromTCPClient:%dBytes,Tcpip.tcp_Rev_Length);SetCtrlVal(panelHandle,PANEL_TEXTBOX_TCP_ASCII,Rev_Length);//显示接收数据长度break;caseTCP_DISCONNECT:if(handle==Tcpip.client_Handle){//因为断开方式被设置为自动,所以这里不需要调用DisconnectTCPClientTcpip.client_Handle=0;//Disconnect时不需要再断了SetCtrlVal(panelHandle,PANEL_LED_CLIENT,0);//Clientled灭SetCtrlVal(panelHandle,PANEL_STRING_CLIENT_IP,);//清空IP栏}break;}return0;}注:发送数据、获取信息都是一些很简单的调用,不再介绍。3.2客户端ClientintCVICALLBACKconnect(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){charsever_Addr[20];charsever_Name[2