• http://forums.devshed.com SOCKET 多线程 样例


    http://forums.devshed.com/c-programming-42/i-need-a-multi-thread-socket-server-source-to-refer-to-91703.html
    #include <my_sock.h>
    #include <errno.h>

    #define PORT 6969
    #define BUFSIZE 8192
    #define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
    #define MAXTHREADS 25

    void * f00(void *data)
    {
            char    buf[BUFSIZE];
            int     asock = (int) data,     nread = 0, fd = 0;

            //open the file
            if( (fd = open("tmp", O_CREAT | O_TRUNC | O_WRONLY, PERM)) < 0)
                    err_quit("open");

            //read from socket, write to file
            while( (nread = read(asock, buf, BUFSIZE)) > 0)
                    if(write(fd, buf, nread) != nread)
                            err_quit("write");

            close(fd);
            return;
    };

    int main(int argc, char **argv)
    {
            int     on = 1, fd = 0, lsock = 0, asock = 0, nread = 0, nthreads = 0, x = 0;
            char    buf[BUFSIZE];
            struct  sockaddr_in     sa;
            socklen_t len = sizeof(sa);
            pthread_t       threadid[MAXTHREADS];

            //initialize the socket -- returns < 0 on error
            if ( (lsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                    err_quit("socket");

            if(setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
                    err_quit("setsockopt");

            //setup the sock_addrin structure
            memset(&sa, 0, sizeof(sa));
            sa.sin_family = AF_INET;
            sa.sin_port   = htons(PORT);
            sa.sin_addr.s_addr = htonl(INADDR_ANY);

            //bind socket
            if( (bind(lsock, (struct sockaddr *) &sa, sizeof(sa))) < 0)
                    err_quit("bind");

            //go into listen mode
            if( (listen(lsock, 5)) < 0)
                    err_quit("listen");
           
            while(1){
                    //accept a connection
                    if( (asock = accept(lsock, NULL, NULL)) < 0)
                            err_quit("accept");
                   
                    //spawn a thread for each clien
                    if(pthread_create(&threadid[x++], NULL, (void *)&f00, (int)asock) != 0)
                            perr_quit("pthread");
                   
                    if(nthreads++ >= MAXTHREADS)
                            break;
            }
           
            //wait for all threads
            for(x = 0; x < nthreads; x++)
                    if(pthread_join(threadid[x], NULL) < 0)
                            perr_quit("pthread join");
           
            //cleanup
            close(lsock);
           
            return 0;
    }
  • 相关阅读:
    兼容所有浏览器的CSS3圆角效果
    浏览器兼容 IE6、IE7、IE8、Firefox、Opera CSS hack区分
    分享30个优秀的网站导航设计案例
    解析如何改善和优化 Web 服务器性能
    分享40款效果非常漂亮的 HTML5 & CSS3 网站模板,模板免费下载
    分享20个非常有创意的蓝色风格网页作品触发你的设计灵感
    分享20个效果非常绚丽的 CSS3 特性应用演示
    推荐12个优秀的 HTML5 网站设计案例欣赏
    网站设计风格及色彩搭配技巧
    舞动你的文字,巧用CSS中marquee属性
  • 原文地址:https://www.cnblogs.com/cy163/p/1209576.html
Copyright © 2020-2023  润新知