简体   繁体   English

在c中一次处理多个连接

[英]Handle more than one connection at a time in c

I am able to create a socket , binding it to connection , listening on it and receiving results but in all this i have just 1 connection .How do i handle multiple incoming connection in c?I went through the net got some stuff but couldn't get it working.. 我能够创建一个套接字,将其绑定到连接,监听它并接收结果,但是在所有这些中,我只有1个连接。我该如何处理c中的多个传入连接?不能正常工作..

Please help. 请帮忙。

you can use fork / threads / some poll or framework functions. 您可以使用fork / threads / some poll或框架函数。

simple fork example from google: 来自Google的简单fork示例:

#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>

void doprocessing (int sock)
{
    int n;
    char buffer[256];

    bzero(buffer,256);

    n = read(sock,buffer,255);
    if (n < 0)
    {
        perror("ERROR reading from socket");
        exit(1);
    }
    printf("Here is the message: %s\n",buffer);
    n = write(sock,"I got your message",18);
    if (n < 0)
    {
        perror("ERROR writing to socket");
        exit(1);
    }
}

int main( int argc, char *argv[] )
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr;

    /* First call to socket() function */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("ERROR opening socket");
        exit(1);
    }
    /* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5001;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    /* Now bind the host address using bind() call.*/
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
                          sizeof(serv_addr)) < 0)
    {
         perror("ERROR on binding");
         exit(1);
    }
    /* Now start listening for the clients, here
     * process will go in sleep mode and will wait
     * for the incoming connection
     */
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    while (1)
    {
        newsockfd = accept(sockfd,
                (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0)
        {
            perror("ERROR on accept");
            exit(1);
        }
        /* Create child process */
        pid_t pid = fork();
        if (pid < 0)
        {
            perror("ERROR on fork");
            exit(1);
        }
        if (pid == 0)
        {
            /* This is the client process */
            close(sockfd);
            doprocessing(newsockfd);
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
    } /* end of while */
}

I'll can give you a brief description of how that would work. 我将给您一个简短的说明。

You need to set up a system that listens for a connection, and when there is one it pushes it into a list of open connections. 您需要建立一个侦听连接的系统,当有连接时,它将把它推送到打开的连接列表中。 After it stores the connection in the list you create another listener. 在将连接存储在列表中之后,您将创建另一个侦听器。 Periodically prune your list based on the connection status. 根据连接状态定期修剪列表。

You don't mention which OS, so I'm going to assume Unix/Linux. 您没有提到哪个操作系统,因此我将假设使用Unix / Linux。

The most common method to handle multiple connections is to process each connection in a separate process via fork() or using threads using pthreads for example. 处理多个连接的最常见方法是通过fork()或使用使用pthreads线程在单独的进程中处理每个连接。 The idea is that the server waits for a connection, when one is accepted a new process is created using fork() or a new thread is created to handle the new connection. 这个想法是服务器等待连接,当接受连接时,将使用fork()创建一个新进程,或者创建一个新线程来处理新连接。 Here is a decent tutorial you might want to have a look at. 这是一个不错的教程,您可能想看看。

Here is a useful pthreads tutorial 这是一个有用的pthreads教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM