美文网首页
linux socket服务端、客户端、类

linux socket服务端、客户端、类

作者: YanyZhao | 来源:发表于2019-10-15 09:49 被阅读0次

    服务端

    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[])
    {
        std::cout << "This is server" << std::endl;
        // socket
        int listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (listenfd == -1) {
            std::cout << "Error: socket" << std::endl;
            return 0;
        }
        // bind
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8000);
        addr.sin_addr.s_addr = INADDR_ANY;
        if (bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
            std::cout << "Error: bind" << std::endl;
            return 0;
        }
        // listen
        if(listen(listenfd, 5) == -1) {
            std::cout << "Error: listen" << std::endl;
            return 0;
        }
        // accept
        int conn;
        char clientIP[INET_ADDRSTRLEN] = "";
        struct sockaddr_in clientAddr;
        socklen_t clientAddrLen = sizeof(clientAddr);
        while (true) {
            std::cout << "...listening" << std::endl;
            conn = accept(listenfd, (struct sockaddr*)&clientAddr, &clientAddrLen);
            if (conn < 0) {
                std::cout << "Error: accept" << std::endl;
                continue;
            }
            inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
            std::cout << "...connect " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl;
    
            //char buf[255];
            while (true) {
    //            memset(buf, 0, sizeof(buf));
    //            int len = recv(conn, buf, sizeof(buf), 0);
    //            buf[len] = '\0';
    //            if (strcmp(buf, "exit") == 0) {
    //                std::cout << "...disconnect " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl;
    //                break;
    //            }
    //            std::cout << buf << std::endl;
                char buf[] = "b 1 1 2 2 2 e ";
    
                send(conn, buf, sizeof(buf), 0);
                //sleep(1);
            }
    
            close(conn);
        }
        close(listenfd);
        return 0;
    }
    
    

    客户端

    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[])
    {
        std::cout << "This is client" << std::endl;
        // socket
        int client = socket(AF_INET, SOCK_STREAM, 0);
        if (client == -1) {
            std::cout << "Error: socket" << std::endl;
            return 0;
        }
        // connect
        struct sockaddr_in serverAddr;
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(8000);
        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        if (connect(client, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
            std::cout << "Error: connect" << std::endl;
            return 0;
        }
        std::cout << "...connect" << std::endl;
        //char data[255];
        char buf[1024];
        while (true) {
    //        printf("ssss");
    //        std::cin >> data;
    //        send(client, data, strlen(data), 0);
    //        if (strcmp(data, "exit") == 0) {
    //            std::cout << "...disconnect" << std::endl;
    //            break;
    //        }
            memset(buf, 0, sizeof(buf));
            int len = recv(client, buf, sizeof(buf), 0);
            buf[len] = '\0';
            std::cout << buf << std::endl;
        }
        close(client);
        return 0;
    }
    

    mysocket.h

    class MySocket //: public socket
    {
    public:
        //MySocket();
        char buf[255];
    
    public:
        void ServerInit();
        void SendMessage();
        void ClientInit();
        void RecvMessage();
    //    void HandleString();
    
    private:
        //server
        struct sockaddr_in addr,clientAddr;
        char clientIP[INET_ADDRSTRLEN] = "";
        int listenfd,conn;
        socklen_t clientAddrLen ;
        //client
        int client;
        struct sockaddr_in serverAddr;
        #define ServerAddr "127.0.0.1"
    };
    

    Mysocket.cpp

    void MySocket::ServerInit()
    {
        // socket
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        // bind
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8000);
        addr.sin_addr.s_addr = INADDR_ANY;
        bind(listenfd, (struct sockaddr*)&addr, sizeof(addr));
        // listen
        if(listen(listenfd, 5) == -1) {
            std::cout << "Error: listen" << std::endl;
            //return 0;
        }
        // accept
        clientAddrLen = sizeof(clientAddr);
        while(true)
        {
            std::cout << "...listening" << std::endl;
            conn = accept(listenfd, (struct sockaddr*)&clientAddr, &clientAddrLen);
            if (conn < 0) {
                std::cout << "Error: accept" << std::endl;
                continue;
            }
            inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
            std::cout << "...connect " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl;
            break;
        }
    
    }
    
    void MySocket::ClientInit()
    {
        std::cout << "This is client" << std::endl;
        // socket
        client = socket(AF_INET, SOCK_STREAM, 0);
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(8000);
        //serverAddr.sin_addr.s_addr = inet_addr(ServerAddr);
        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        //connect
        connect(client,(struct sockaddr*)&serverAddr, sizeof(serverAddr));
        std::cout << "...connect" << std::endl;
        std::cout <<"client:"<< client << std::endl;
    }
    
    void MySocket::SendMessage()
    {
        while(true)
        {
            strcpy(buf ,"111111");
            send(conn, buf, sizeof(buf),0);
            sleep(1);
        }
    }
    
    void MySocket::RecvMessage()
    {
        while (true)
        {
            memset(buf, 0, sizeof(buf));
            int len = recv(client, buf, sizeof(buf), 0);
            buf[len] = '\0';
            std::cout << buf << std::endl;
        }
        close(client);
    }
    

    相关文章

      网友评论

          本文标题:linux socket服务端、客户端、类

          本文链接:https://www.haomeiwen.com/subject/gaejmctx.html