• C Socket初探


    先上一些多线程需要使用的函数定义:

    DWORD WINAPI ProcessClientRequests(LPVOID lpParam)  //新线程将会执行的函数定义
    {
          return 0;
    }
    
    HANDLE handler=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL);   //这里比较简单,&clientsocket是个指针,是从主线程传入新线程的参数
    
    
    WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE);  //用来阻塞主线程,直到所有创建的子线程都完成任务为止,才继续执行后面的代码
    
    for(int i=0;i<MAXCLIENTS; i++)
    {
        CloseHandle(threads[i]);       //创建的每个子线程的HANDLE都会被保存在HANDLE数组中,这个函数用于关闭各个handle所对应的线程空间
    }

    先附上C Socket初探这篇文章的url

    改造开始,客户端程序没有任何改动,因此此处略(请看C Socket初探中代码所示)

    服务器端程序

    主线程代码如下:

    #define MAXCLIENTS 3           //宏定义,最多3个客户端连接
    
    int main()
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2, 2), &wsaData);
        HANDLE threads[MAXCLIENTS];
    
        SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
        sockaddr_in sockaddr;
        sockaddr.sin_family=PF_INET;
        sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
        sockaddr.sin_port=htons(9000);
        bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));
    
        listen(s, 1);
    
        printf("listening on port [%d].
    ", 9000);
    
        int existingClientCount=0;
        while(TRUE)
        {
            SOCKADDR clientAddr;
            int size=sizeof(SOCKADDR);
    
            SOCKET clientsocket;
            clientsocket=accept(s, &clientAddr, &size);
            printf("***SYS***    New client touched.
    ");
    
            if(existingClientCount<MAXCLIENTS)       //判断是否已经超出最大连接数了
            {
                threads[existingClientCount++]=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL);  //启动新线程,并且将socket传入
            }
            else
            {
                char* msg="Exceeded Max incoming requests, will refused this connect!
    ";
                send(clientsocket, msg, strlen(msg)+sizeof(char), NULL);       //发送拒绝连接消息给客户端
                printf("***SYS***    REFUSED.
    ");
                closesocket(clientsocket);                                     //释放资源
    break;
    } } printf(
    "Maximize clients occurred for d%. ", MAXCLIENTS); WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE); //等待所有子线程,直到完成为止 closesocket(s); for(int i=0;i<MAXCLIENTS; i++) { CloseHandle(threads[i]); //清理线程资源 } WSACleanup(); printf("Cleared all. "); getchar(); exit(0); }

    子线程函数定义

    DWORD WINAPI ProcessClientRequests(LPVOID lpParam)
    {
        SOCKET* clientsocket=(SOCKET*)lpParam;  //这里需要强制转换,注意:指针类型的
    
        char* msg="Hello, my client.
    ";
        send(*clientsocket, msg, strlen(msg)+sizeof(char), NULL);
        printf("***SYS***    HELLO.
    ");
    
        while(TRUE)
        {
            char buffer[MAXBYTE]={0};
            recv(*clientsocket, buffer, MAXBYTE, NULL);
            if(strcmp(buffer, "exit")==0)
            {
                char* msg_bye="Bye.
    ";
                send(*clientsocket, msg_bye, strlen(msg_bye)+sizeof(char), NULL);
                break;
            }
            printf("***Client***    %s
    ", buffer);
        }
        
        closesocket(*clientsocket);
    
        return 0;
    }

    运行效果图:

  • 相关阅读:
    C++ Low level performance optimize
    简单find命令的实现
    数据结构学习之栈
    随机数的生成
    数据结构学习(一)
    C复习---动态内存分配
    (转)虚拟文件系统(VFS)浅析
    (转) 中断处理程序&中断服务例程
    Gradle系列教程之依赖管理
    Gradle系列教程之依赖管理
  • 原文地址:https://www.cnblogs.com/aarond/p/SocketThread.html
Copyright © 2020-2023  润新知