zoukankan      html  css  js  c++  java
  • windows socket 数据交互

    client

    //ip地址 端口
    SOCKET init(const char *IP, u_short port) {
        WSADATA wd;
        int ret = WSAStartup(MAKEWORD(2, 2), &wd);
        if (ret != 0) {
            cout << "WSAStartup error ..." << endl;
            return INVALID_SOCKET;
        }
        cout << "WSAStartup success ..." << endl;
    
        //创建套接字
        SOCKET skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (skt == SOCKET_ERROR) {
            cout << "socket error ..." << endl;
            return INVALID_SOCKET;
        }
        cout << "socket success ..." << endl;
    
        //服务端信息
        sockaddr_in addrServ;
        addrServ.sin_family = AF_INET;//IPV4协议
        addrServ.sin_port = htons(port);//端口
        addrServ.sin_addr.s_addr = inet_addr(IP);//IP地址
    
        //连接服务器
        if (connect(skt, (sockaddr *)&addrServ, sizeof(addrServ)) == SOCKET_ERROR) {
            cout << "connect error ..." << endl;
            return -1;
        }
        cout << "connect success ..." << endl;
    
        return skt;
    }
    
    void destroy(SOCKET skt) {
        closesocket(skt);
        WSACleanup();
    }
    
    void printMenu() {
        printf("*************************
    ");
        printf("*getName                *
    ");
        printf("*getAge                 *
    ");
        printf("*exit                   *
    ");
        printf("*************************
    ");
    }
    
    //socket客户端代码 客户端发送命令 接受服务器发送的消息
    int function06() {
        SOCKET sktCli = init("192.168.3.18", 8080);
        if (sktCli == INVALID_SOCKET) {
            return -1;
        }
    
        while (true) {
            printMenu();
            char szCmd[64] = {0};
            scanf("%s", szCmd);
            if (strcmp(szCmd, "exit") == 0) {
                printf("[client] exit ...
    ");
                break;
            }
            send(sktCli, szCmd, strlen(szCmd)+1, 0);
    
            char szRecv[1024] = {0};
            int ret = recv(sktCli, szRecv, sizeof(szRecv), 0);
            if (ret <= 0) {
                printf("[client] server exit ...
    ");
                break;
            }
            printf("[server] msg:%s
    ", szRecv);
        }
    
        destroy(sktCli);
        return 0;
    }

    server

    //初始化
    SOCKET init(const char *IP, u_short port) {
        WSADATA wd;
        int ret = WSAStartup(MAKEWORD(2, 2), &wd);
        if (ret != 0) {
            cout << "WSAStartup error ..." << endl;
            return -1;
        }
        cout << "WSAStartup success ..." << endl;
    
        //创建套接字
        SOCKET skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (skt == INVALID_SOCKET) {
            cout << "socket error ..." << endl;
            return -1;
        }
        cout << "socket success ..." << endl;
        //服务器信息
        sockaddr_in addrServ;
        addrServ.sin_family = AF_INET;
        addrServ.sin_port = htons(port);
        addrServ.sin_addr.s_addr = inet_addr(IP);
    
        //套接字绑定端口和ip
        if (bind(skt, (sockaddr *)&addrServ, sizeof(addrServ)) == SOCKET_ERROR) {
            cout << "bind error ..." << endl;
            return -1;
        }
        cout << "bind success ..." << endl;
    
        //开始监听套接字
        if (listen(skt, SOMAXCONN) == SOCKET_ERROR) {
            cout << "listen error ..." << endl;
            return -1;
        }
        cout << "listen success ..." << endl;
    
        return skt;
    }
    
    void destroy(SOCKET skt) {
        closesocket(skt);
        WSACleanup();
    }
    
    //接受客户端发送的命令 发送消息给客户端
    int function06() {
        SOCKET sktServ = init("0.0.0.0", 8080);
        if (sktServ == INVALID_SOCKET) {
            return -1;
        }
    
        SOCKET sktCli = accept(sktServ, NULL, NULL);
        while (true) {
            char szCmd[64] = {0};
            int ret = recv(sktCli, szCmd, sizeof(szCmd), 0);
            if (ret == SOCKET_ERROR) {
                printf("[server] recv error ...
    ");
                break;
            }
            if (ret == 0) {
                printf("[client] exit ...
    ");
                break;
            }
            printf("[client] %s
    ", szCmd);
            char szMsg[1024];
            if (strcmp(szCmd, "getName") == 0) {
                sprintf(szMsg, "%s", "爱白菜的小昆虫.");
            }
            else if (strcmp(szCmd, "getAge") == 0) {
                sprintf(szMsg, "%s", "1000.");
            }
            else {
                sprintf(szMsg, "%s", "???.");
            }
            send(sktCli, szMsg, strlen(szMsg)+1, 0);
        }
    
        destroy(sktServ);
        return 0;
    }
  • 相关阅读:
    centos6 Cacti部署文档
    nginx 3.nginx+fastcgi
    nginx 2.基本配置
    nginx 1.安装
    mongodb入门教程二
    mongodb入门教程
    一款jQuery立体感动态下拉导航菜单特效
    一款jQuery仿海尔官网全屏焦点图特效代码
    一款非常炫酷的jQuery动态随机背景滚动特效
    一款jquery编写图文下拉二级导航菜单特效
  • 原文地址:https://www.cnblogs.com/xuqiulin/p/11432890.html
Copyright © 2011-2022 走看看