并发服务器线程 - 给出绑定错误

发布于 2025-01-31 08:57:37 字数 2136 浏览 1 评论 0原文

我正在尝试创建两个可以在两个不同端口侦听的线程。

我的逻辑是:

#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdbool.h>

typedef struct server_arg {
    int portNum;
} server_arg; 

void  *server_socket_creation(void *arg) {

server_arg *s = (server_arg*)arg;
int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    
 
    // Creating socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
        == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
        int enable = 1;
        if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
          perror("error");
        }
 
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(s->portNum);
 

    if (bind(server_fd, (struct sockaddr*)&address,
             sizeof(address))
        < 0) {
        perror("bind failed");
        
    }
    if (listen(server_fd, 3) < 0) {
        perror("listen");
        
    }
    if ((new_socket
         = accept(server_fd, (struct sockaddr*)&address,
                  (socklen_t*)&addrlen))
        < 0) {
        perror("accept");
    }
   
    printf("Server Connected\n");
    

}

int main(int argc, char const* argv[])
{
  

  server_arg *s = (server_arg*)malloc(sizeof(server_arg));
  pthread_t id_1;
  pthread_t id_2;
  s->portNum = htons(9191);
  pthread_create(&id_1,NULL,(void *)server_socket_creation,(void *)s);
  s->portNum = htons(6123);
  pthread_create(&id_2,NULL,(void *)server_socket_creation,(void *)s);
  pthread_detach(id_1);
  pthread_detach(id_2);
  
  
  
  
  pthread_exit(0);
}

已经使用的地址

错误:绑定错误:在stackoverflow上搜索此问题后,现在

:我可以找到两个原因:

  1. 端口已经在使用。 (在这种情况下,意味着使用相同端口) 如您所见,情况并非如此。
  2. 服务器IP地址相同。 我认为这是这里的问题,但我想不出任何解决这个问题的方法。

I am trying to create two threads that can listen at two different ports.

My logic is:

#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdbool.h>

typedef struct server_arg {
    int portNum;
} server_arg; 

void  *server_socket_creation(void *arg) {

server_arg *s = (server_arg*)arg;
int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    
 
    // Creating socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
        == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
        int enable = 1;
        if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
          perror("error");
        }
 
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(s->portNum);
 

    if (bind(server_fd, (struct sockaddr*)&address,
             sizeof(address))
        < 0) {
        perror("bind failed");
        
    }
    if (listen(server_fd, 3) < 0) {
        perror("listen");
        
    }
    if ((new_socket
         = accept(server_fd, (struct sockaddr*)&address,
                  (socklen_t*)&addrlen))
        < 0) {
        perror("accept");
    }
   
    printf("Server Connected\n");
    

}

int main(int argc, char const* argv[])
{
  

  server_arg *s = (server_arg*)malloc(sizeof(server_arg));
  pthread_t id_1;
  pthread_t id_2;
  s->portNum = htons(9191);
  pthread_create(&id_1,NULL,(void *)server_socket_creation,(void *)s);
  s->portNum = htons(6123);
  pthread_create(&id_2,NULL,(void *)server_socket_creation,(void *)s);
  pthread_detach(id_1);
  pthread_detach(id_2);
  
  
  
  
  pthread_exit(0);
}

error: binding error: Address Already in use

Now, after searching about this issue on stackoverflow:

I could find 2 reasons:

  1. Ports are already in use. (Means using same ports in this case)
    which is not the case here as you can see.
  2. server IP address is same.
    I think this is the issue here, but I can't think of any way to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2025-02-07 08:57:37

您有一个server_arg结构。您将指针传递给此单个server_arg结构的指针到两个线程。

分配的可能性很大

s->portNum = htons(6123);

在第一个线程复制该值之前,

address.sin_port = htons(s->portNum);

,这就是所谓的a data-race 。作为任何数据竞赛,唯一宽松的是您作为程序员。

您需要两个结构,每个线程一个。例如,作为数组:

server_arg s[2[ = {
    { 9191 },
    { 6123 }
};

然后将&amp&amp; s [0]传递给第一个线程,然后将&amp&amp&amp&amp&amp; s [1]转到第二个线程。


端口号也有另一个问题,当您像上面的端口分配相近时,应该很容易地看到这一点:您调用htons 两次

You have a single server_arg structure. You pass a pointer to this single server_arg structure to both threads.

There's a very significantly large chance that the assignment

s->portNum = htons(6123);

will happen before the first thread have copied the value with

address.sin_port = htons(s->portNum);

This is what is called a data-race. And as any data-race, the only looser is you as the programmer.

You need two structures, one for each thread. For example as an array:

server_arg s[2[ = {
    { 9191 },
    { 6123 }
};

Then pass &s[0] to the first thread, and &s[1] to the second.


There's also another problem with the port number, which should be quite easily seen when you put both the port assignments close to each other like I did above: You call htons twice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文