• FINS/TCP C++版本Demo


    char CMDData[100] = { 0 };
    char CNTData[100] = { 0 };
    void initialization();
    void InitFinsData() {
        CNTData[0] = 0x46;
        CNTData[1] = 0x49;
        CNTData[2] = 0x4E;
        CNTData[3] = 0x53;
        CNTData[4] = 0x00;
        CNTData[5] = 0x00;
        CNTData[6] = 0x00;
        CNTData[7] = 0x0C;
        CNTData[8] = 0x00;
        CNTData[9] = 0x00;
        CNTData[10] = 0x00;
        CNTData[11] = 0x00;
        CNTData[12] = 0x00;
        CNTData[13] = 0x00;
        CNTData[14] = 0x00;
        CNTData[15] = 0x00;
        CNTData[16] = 0x00;
        CNTData[17] = 0x00;
        CNTData[18] = 0x00;
        CNTData[19] = 0x00;
    
        CMDData[0] = 0x46;
        CMDData[1] = 0x49;
        CMDData[2] = 0x4E;
        CMDData[3] = 0x53;
        CMDData[4] = 0x00;
        CMDData[5] = 0x00;
        CMDData[6] = 0x00;
        CMDData[7] = 0x1E;
        CMDData[8] = 0x00;
        CMDData[9] = 0x00;
        CMDData[10] = 0x00;
        CMDData[11] = 0x02;
        CMDData[12] = 0x00;
        CMDData[13] = 0x00;
        CMDData[14] = 0x00;
        CMDData[15] = 0x00;
        CMDData[16] = 0x80;
        CMDData[17] = 0x00;
        CMDData[18] = 0x02;
        CMDData[19] = 0x00;
        CMDData[20] = 0x01;
        CMDData[21] = 0x00;
        CMDData[22] = 0x00;
        CMDData[23] = 0xEF;
        CMDData[24] = 0x00;
        CMDData[25] = 0x05;
        CMDData[26] = 0x01;
        CMDData[27] = 0x02;
        CMDData[28] = 0x82;
        CMDData[29] = 0x00;
        CMDData[30] = 0x01;
        CMDData[31] = 0x00;
        CMDData[32] = 0x00;
        CMDData[33] = 0x02;
        CMDData[34] = 0xAA;
        CMDData[35] = 0xAA;
        CMDData[36] = 0xBB;
        CMDData[37] = 0xBB;
    }
    
    int main() {
        InitFinsData();
        //定义长度变量
        int send_len = 0;
        int recv_len = 0;
        //定义发送缓冲区和接受缓冲区
        char send_buf[100];
        //定义服务端套接字,接受请求套接字
        SOCKET s_server;
        //服务端地址客户端地址
        SOCKADDR_IN server_addr;
        initialization();
        //填充服务端信息
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.S_un.S_addr = inet_addr("192.168.250.1");
        server_addr.sin_port = htons(9600);
        //创建套接字
        s_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (connect(s_server, (struct sockaddr*) & server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
    
            cout << "服务器连接失败!" << endl;
            WSACleanup();
        }
        else {
            cout << "服务器连接成功!" << endl;
        }
    
        UCHAR recv_buf[100] = { 0 };
    
        cout << "请输入发送信息:";
        send_buf;
        send_len = send(s_server, CNTData, 20, 0);
        if (send_len < 0) {
            cout << "发送失败!" << endl;
        }
        recv_len = recv(s_server, (char*)recv_buf, 100, 0);
        if (recv_len < 0) {
            cout << "接受失败!" << endl;
        }
        else {
            cout << "服务端信息:" << recv_buf << endl;
        }
    
        UCHAR recv_buf2[100] = { 0 };
    
        cout << "请输入发送信息:";
        send_len = send(s_server, CMDData, 38, 0);
        if (send_len < 0) {
            cout << "发送失败!" << endl;
        }
        recv_len = recv(s_server, (char*)recv_buf2, 100, 0);
        if (recv_len < 0) {
            cout << "接受失败!" << endl;
        }
        else {
            cout << "服务端信息:" << recv_buf2 << endl;
        }
    
    
        closesocket(s_server);
        WSACleanup();
        return 0;
    }
    
    void initialization() {
        //初始化套接字库
        WORD w_req = MAKEWORD(2, 2);//版本号
        WSADATA wsadata;
        int err;
        err = WSAStartup(w_req, &wsadata);
        if (err != 0) {
            cout << "初始化套接字库失败!" << endl;
        }
        else {
            cout << "初始化套接字库成功!" << endl;
        }
        //检测版本号
        if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
            cout << "套接字库版本号不符!" << endl;
            WSACleanup();
        }
        else {
            cout << "套接字库版本正确!" << endl;
        }
        //填充服务端地址信息
    }

    注意:传参必须按照实际长度传递,多/少一个参数都不可以!!!!!!!!!!!!!!!!!!!!!!!!!!!

    附:C++/C#/VB版本的Fins通信代码,可直接使用

  • 相关阅读:
    python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
    JVM内存模型、指令重排、内存屏障概念解析
    图解JVM的Class文件格式(详细版)
    图解JVM执行引擎之方法调用
    为何JAVA虚函数(虚方法)会造成父类可以"访问"子类的假象?
    小乖上学第一天
    FLEX RIA快速添加图标
    1,2,3,5,7,8,10,11,12,13,14,15,16,21,22 》1~3,5,7~8,10~16,21~22
    ABAP 函数编写
    ABAP子进程(字符串分割定位)
  • 原文地址:https://www.cnblogs.com/Khan-Sadas/p/12698127.html
Copyright © 2020-2023  润新知