并发服务器线程 - 给出绑定错误
我正在尝试创建两个可以在两个不同端口侦听的线程。
我的逻辑是:
#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上搜索此问题后,现在
:我可以找到两个原因:
- 端口已经在使用。 (在这种情况下,意味着使用相同端口) 如您所见,情况并非如此。
- 服务器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:
- Ports are already in use. (Means using same ports in this case)
which is not the case here as you can see. - server IP address is same.
I think this is the issue here, but I can't think of any way to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个
server_arg
结构。您将指针传递给此单个server_arg
结构的指针到两个线程。分配的可能性很大
在第一个线程复制该值之前,
,这就是所谓的a data-race 。作为任何数据竞赛,唯一宽松的是您作为程序员。
您需要两个结构,每个线程一个。例如,作为数组:
然后将
&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 singleserver_arg
structure to both threads.There's a very significantly large chance that the assignment
will happen before the first thread have copied the value with
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:
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.