服务器停止创建线程

fla*_*urn 0 c sockets linux pthreads

我有一个非常简单的客户端 - 服务器代码.每次服务器收到数据包时,我都会创建一个处理它的线程.代码如下所示.我无法理解的是,有一段时间后我的服务器停止接收任何数据.它只是倾听并且没有收到任何东西.我无法弄清楚为什么.有谁知道原因.

我正在联想T470s上构建我的代码Fedora 29,Linux用户4.19.15-300.fc29.x86_64#1 SMP Mon 1月14日16:32:35 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

任何帮助表示赞赏.

/* 
  server.c
  cc -std=gnu11 -pedantic  -lpthread  server.c   -o server.c 
 */

#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUF_SIZE_B 1024

static int fd;

static void *handlePacketThreadWrapper(void *arg);

int main(void)
{
    pthread_t t;
    struct pollfd pollfd;
    struct sockaddr_in addr;
    uint16_t port = 9500;

    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (fd < 0)
    {
        printf("errno: %d. %s. Failed to create a socket",
                errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    addr.sin_family      = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port        = htons(port);

    while(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        printf("errno: %d. %s. Failed to bind socket. Will attempt again.", errno,
                strerror(errno));
        sleep(1);
    }

    memset(&addr, 0, sizeof(addr));

    pollfd.fd = fd;
    pollfd.events = POLLIN;

    while(true)
    {

        if (poll(&pollfd, 1, -1) < 0)
        {
            printf("errno: %d. %s", errno, strerror(errno));
        }
        else
        {
            pthread_create(&t, NULL, handlePacketThreadWrapper, NULL);
        }
    }

    return 0;
}

static void *handlePacketThreadWrapper(void *arg)
{
    uint8_t buf[BUF_SIZE_B];
    size_t strLen, fullIPLen;
    ssize_t
        i,
        n
        ;
    struct sockaddr_in addr;
    socklen_t addrLen = sizeof(addr);
    char *str, *fullIP;


    n = recvfrom(fd, buf, sizeof(buf), 0,
            (struct sockaddr *)&addr, (socklen_t *)&addrLen);
    if (n < 0)
    {
        printf("errno: %d. %s. Failed to create a socket",
                errno, strerror(errno));
    }
    else
    {
        for (i = 0; i < n; i++)
        {
            printf("0x%02X ", buf[i]);
        }
        printf("\n");
    }
    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

这是我的客户端代码:

/* 
  client.c
  cc -std=gnu11 -pedantic  client.c   -o client.c  
 */
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

#define BUF_SIZE_B 1024

int main(void)
{
    ssize_t size, i;
    struct sockaddr_in dest;
    int fd;
    char *toIP = "127.0.0.1";
    uint16_t toPort = 9500;
    uint8_t buf[BUF_SIZE_B];

    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (fd < 0)
    {
        printf("errno: %d, %s", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    memset(&dest, 0, sizeof(dest));
    dest.sin_family      = AF_INET;
    dest.sin_addr.s_addr = inet_addr(toIP);
    dest.sin_port        = htons(toPort);

    while(true)
    {
        size = sendto(fd, buf, sizeof(buf), 0, (struct sockaddr*)&dest, sizeof(dest));
        if (size < 0)
        {
            printf("errno: %d. %s. Failed to send bytes to %s:%hu.", 
                    errno, strerror(errno), toIP, toPort);
        }
        else
        {
            for (i = 0; i < size; i++)
                printf("0x%02X ", buf[i]);
            printf("\n");
        }
        usleep(1000);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

use*_*697 6

作为ulimit -u报告,您只能同时运行多个线程.

您的服务器永远不会加入线程,一旦达到限制,它就会开始无法创建它们.