http://forums.devshed.com SOCKET 多線程 範例

來源:互聯網
上載者:User
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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.