• 使用TCP


    1.基本流程:

      服务端: 创建socket->bind()地址和端口->listen()设置为监听模式->accept()接受客户端请求,收到了就返回新socket,这个

        socket包含了客户端的信息->recv()接受数据,send()发送数据->shutdown()通知对方不再发送或接受数据.然后closesocket()关闭客户端套接字

        ->继续监听或者closesocket()关闭本地socket.

      客户端:socket()创建本地socket,connect()将本地socket连接到服务器->send()发送,recv()接受数据->完了后shutdown()通知对方不再继续通信,

    closesocket()关闭socket.

    简单实例:

    服务端:

    #include <WinSock2.h>
    #include<stdio.h>
    #include<windows.h>
    #include<string.h>
    #pragma  comment(lib,"ws2_32.lib")
    
    void main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData))
        {
            printf("版本错误
    ");
            return;
        }
        int status;
        int len=sizeof(SOCKADDR);
        char recvBuffer[1024];
        char sendBuffer[1024];
        SOCKET server_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        SOCKET client_socket;
        SOCKADDR_IN bind_addr;
        SOCKADDR_IN client_addr;
    
        bind_addr.sin_family = AF_INET;
        bind_addr.sin_port = htons(8888);
        bind_addr.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
        status = bind(server_socket, (const sockaddr*)&bind_addr, sizeof(sockaddr));
        if (status==SOCKET_ERROR)
        {
            printf("bind error code is %d
    ", WSAGetLastError());
            WSACleanup();
            return;
        }
        status = listen(server_socket, 1);
        if (status== SOCKET_ERROR)
        {
            printf("listen error code is %d
    ", WSAGetLastError());
            WSACleanup();
            return;
        }
        client_socket = accept(server_socket, (sockaddr*)&client_addr, &len);
        if (client_socket == INVALID_SOCKET)
        {
            printf("accept error code is %d
    ", WSAGetLastError());
            closesocket(server_socket);
            WSACleanup();
            
            return;
        }
        printf("info: connected by %s %d
    ", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        printf("info: type quit to quit
    ");
        while (1)
        {
            memset(recvBuffer, 0, sizeof(recvBuffer));
            memset(sendBuffer, 0, sizeof(sendBuffer));
            recv(client_socket, recvBuffer, sizeof(recvBuffer), 0);
            if (strcmp(recvBuffer, "quit")==0)
            {
                printf("客户端关闭连接,服务端将关闭
    ");
                closesocket(server_socket);
                closesocket(client_socket);
                WSACleanup();
                return;
            }
            printf("收到:%s
    ", recvBuffer);
            printf("输入要发送的内容
    ");
            scanf("%s", sendBuffer);
            if (strcmp(sendBuffer, "quit") == 0)
            {
                printf("退出!
    ");
                send(server_socket, "quit", 5, 0);
                closesocket(server_socket);
                closesocket(client_socket);
                WSACleanup();
                return;
            }
            send(client_socket, sendBuffer, sizeof(sendBuffer), 0);
        }
    
    }

     客户端:

    #include <WinSock2.h>
    #include<stdio.h>
    #include<windows.h>
    #include<string.h>
    #pragma  comment(lib,"ws2_32.lib")
    
    
    void main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData))
        {
            printf("版本错误
    ");
            return;
        }
    
        int len = 0;
        int status;
        char recvBuffer[1024];
        char sendBuffer[1024];
        SOCKET server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        SOCKADDR_IN addr;
        addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
        addr.sin_port = htons(8888);
        addr.sin_family = AF_INET;
    
        status = connect(server_socket, (sockaddr*)&addr, sizeof(sockaddr));
        if (status==SOCKET_ERROR)
        {
            printf("连接失败,错误码:%d
    ", WSAGetLastError());
            closesocket(server_socket);
            WSACleanup();
        }
        printf("info: 连接成功
    ");
        printf("发送quit退出
    ");
        while (1)
        {
            memset(recvBuffer, 0, sizeof(recvBuffer));
            memset(sendBuffer, 0, sizeof(sendBuffer));
            printf("输入发送的消息
    ");
            scanf("%s", sendBuffer);
            if (strcmp(sendBuffer, "quit")==0)
            {
                printf("退出!
    ");
                send(server_socket, "quit", 5, 0);
                closesocket(server_socket);
                WSACleanup();
                return;
            }
            send(server_socket, sendBuffer, sizeof(sendBuffer), 0);
            recv(server_socket, recvBuffer, sizeof(recvBuffer), 0);
            if (strcmp(recvBuffer, "quit") == 0)
            {
                printf("服务端关闭连接,客户端将关闭
    ");
                closesocket(server_socket);
                WSACleanup();
                return;
            }
            printf("接收到: %s
    ", recvBuffer);
        }
    }
  • 相关阅读:
    innobackupex备份命令输出
    Percona XtraBackup原理详解
    MongoDB性能分析工具mongostat
    MongoDB查看当前连接数
    事务、拦截器
    HttpServletResponse和HttpServletRequest的简单实用
    Maven环境配置
    SQL Server 时间戳与时间格式互相转换
    虚拟机、云主机、VPS 三者之间的区别
    Elasticsearch 空值过滤
  • 原文地址:https://www.cnblogs.com/freesec/p/6188183.html
Copyright © 2020-2023  润新知