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

📄 sample8serv.c

📁 用CVI实现的TCP/IP数据传输
💻 C
字号:

#include <cvirte.h>    /* Needed if linking in external compiler; harmless otherwise */
#include <stdio.h>
#include <stdlib.h>
#include <tcpsupp.h>
#include <string.h>
#include <utility.h>
#include <userint.h>
#include "sample8serv.h"

//服务器回调函数
int CVICALLBACK ServerCallback(unsigned handle, int event, int error, void *callbackData);


int         quit=0;
int         panelHandle;
unsigned    covHandle;

int main (int argc, char *argv[])
{
    char    portNumString[32];
    char    buffer[512],tempBuf[256];
    int     numLines, ctrl, handle, i;
    int     portNumber;

    if (InitCVIRTE (0, argv, 0) == 0)    /* Needed if linking in external compiler; harmless otherwise */
        return -1;    /* out of memory */
    
    DisableBreakOnLibraryErrors();

    //输入服务器的端口号
    PromptPopup ("端口号",
                 "请输入客户需要连接的端口号\n(example: 10000)",
                 portNumString, 31);
    portNumber = atoi(portNumString);
    
    //加载用户面板
    panelHandle = LoadPanel(0, "sample8serv.uir", PANEL);
    InstallCtrlCallback (panelHandle, PANEL_kTransmit, TxScrCallback, 0);
    DisplayPanel(panelHandle);

    //注册服务器
    if (RegisterTCPServer(portNumber, ServerCallback, NULL)<0) {
        MessagePopup ("TCP Server", "RegisterTCPServer failed !!!");
        return 0;
    }
    SetActiveCtrl(panelHandle, PANEL_STRING);
    //获取服务器的IP地址
    GetTCPHostAddr (tempBuf, 256);
    //显示服务器的IP地址
	SetCtrlVal(panelHandle, PANEL_SERVERIP, tempBuf);
	//显示服务器的端口号
	SetCtrlVal(panelHandle, PANEL_SERVERPORT, portNumString);
	//显示主界面
    DisplayPanel (panelHandle);
	RunUserInterface ();
	//注销服务器
    UnregisterTCPServer(portNumber);
    DiscardPanel(panelHandle);
    return 0;
}

 //发送数据

int CVICALLBACK TxScrCallback(int panelHandle, int controlID, int event, void *callbackData,
            int eventData1, int eventData2)
{
    int     numLines, connected;
    char    buffer[512];

    switch(event) {
        
        case EVENT_COMMIT:
            //获取数据在发送区内显示出来
            GetCtrlVal (panelHandle, PANEL_STRING, buffer);
            strcat (buffer, "\n");
            SetCtrlVal (panelHandle, PANEL_kTransmit, buffer);
            //清空STRING控件内的内容
            SetCtrlVal (panelHandle, PANEL_STRING, "");
            
            if(ServerTCPWrite(covHandle, buffer, strlen(buffer)+1, 1000)<0) {
                MessagePopup ("TCP服务器", "TCP 写错误!");
                SetCtrlVal(panelHandle, PANEL_CONNECTED, 0);
            }
        default:
            break;
    }
    return 0;
}


//服务器回调函数
int CVICALLBACK ServerCallback(unsigned handle, int event, int error, void *callbackData)
{
    char    buf[512];
    int     readSize;

    switch (event) {
        //客户程序请求连接
        case TCP_CONNECT:
            covHandle = handle;
            //通讯连接灯变亮
            SetCtrlVal(panelHandle, PANEL_CONNECTED, 1);
            break;
        case TCP_DATAREADY:
            if((readSize = ServerTCPRead(covHandle, buf, 512, 1000))==-kTCP_ConnectionClosed) {
                MessagePopup("TCP 服务器", "TCP 读错误!");
                SetCtrlVal(panelHandle, PANEL_CONNECTED, 0);
                break;
            }
            //数据在接收区内显示
            SetCtrlVal(panelHandle, PANEL_kReceive, buf);
            break;
        case TCP_DISCONNECT:
            SetCtrlVal(panelHandle, PANEL_CONNECTED, 0);
            break;
    }
    return 0;
}
//程序退出
int CVICALLBACK Exit (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:
			QuitUserInterface (0);
			break;
		}
	return 0;
}

⌨️ 快捷键说明

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