服务器停止创建线程

时间:2019-01-24 19:48:46

标签: c linux sockets pthreads

我有一个非常简单的客户端-服务器代码。每当服务器收到一个数据包时,我都会创建一个处理它的线程。代码如下所示。我不明白的是,一段时间后我的服务器停止接收任何数据。它只是在听,什么也没收到。我不知道为什么。有谁知道原因。

我正在Lenovo T470s Fedora 29,Linux用户4.19.15-300.fc29.x86_64#1 SMP上构建我的代码Mon Jan 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;
}

这是我的客户代码:

/* 
  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;
}

2 个答案:

答案 0 :(得分:6)

根据ulimit -u的报告,您只能同时运行多个线程。

您的服务器从不加入线程,一旦达到限制,它就开始无法创建线程。

答案 1 :(得分:0)

在您的服务器中, main 线程和创建的线程访问同一套接字( fd ),而没有排他节之类的任何保护,因此执行同时)执行> poll 和 recvfrom ,这很可能破坏套接字的内部数据。

您可以例如:

from copy import deepcopy
from SubstringDict import SubstringDict

dict_2 = {"#1 city, zip, whatever" : ["item 1", "item 5487", ...],
"#2 city, zip, whatever" : ["item 2", "item 1654654", ...] }

def find_substring(sub_str_dict, substring, big_o):

    for k,v in dict_2.items():
        lookup = v[substring]
        if  lookup:
            big_o += 1
            yield lookup
        if i>= n:
            return StopIteration

def make_sub_str_dict():
    d = deepcopy(dict_2)
    for k,v in dict_2.items():
        d[k] = SubstringDict()
        for inner_k,inner_v in v: d[k][inner_k]=inner_v
    return d

dict_2_search = make_sub_str_dict(dict2)

#now you can search for a substring
print(next(find_substring('whatever you are looking for')))

这既保护了套接字的使用,又避免了user58697在另一个答案中提到的 zombie线程(默认情况下,线程未分离)。

当然,这样做会限制在其他线程中读取套接字的兴趣,但是由于多种原因,您的代码实际上与多线程不兼容