C套接字:客户端之间的connect()和/或accept()问题。 111:连接被拒绝
女士们先生们大家好,我在 accept()
方面遇到了一些问题。我有多个客户端和一台服务器。客户端可以与服务器正常连接和通信。但在某一时刻,我需要一些客户端直接相互连接,而我在那里遇到了一些困难。客户端有一堆线程正在运行,其中一个是 handle_connection()
并且它有一个 while(1)
,永远循环到 listen()< /code> 和
accept()
无论传入连接。每当客户端尝试 connect()
到其他客户端时,connect()
都会返回错误,111:连接被拒绝。我知道我有正确的 IP 地址和正确的端口(我已经为客户端之间的连接指定了一个端口)。等待连接的客户端没有注意到任何事情,没有新的连接,什么也没有。我复制了部分代码,希望有人能指出我做错了什么。感谢您的任何意见!
这是所有客户端代码。 void * handle_connections(void * arg)
是一个永远循环到 accept()
任何传入连接的线程。我的服务器上也有一个非常相似的事情,而且运行得很好。 (不知道为什么它在这里不起作用......)这是客户端等待新传入连接的部分。 int handle_request(void * arg, struct message * msg)
在程序期间的某个时刻被调用,并尝试连接到 struct message * msg
中指定的客户端,其中包括struct sockaddr_in
包含 IP 地址和端口号等。
#define SERVER_PORT 10000
#define CLIENT_PORT 3456
#define MAX_CONNECTION 20
#define MAX_MSG 50
void * handle_connections(void * arg)
{
struct fd_info * info;
struct sockaddr_in client_address;
struct timeval timeout;
fd_set readset, copyset;
bzero((char * ) &client_address, sizeof(client_address)); // copy zeroes into string
client_address.sin_family = AF_INET;
client_address.sin_addr.s_addr = htonl(INADDR_ANY);
client_address.sin_port = htons(CLIENT_PORT);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
rv = listen(sockfd,MAX_CONNECTION);
while(1)
{
new_sockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_addr_len); //blocks
if (new_sockfd < 0) {
printf("C: ERROR accept() %i: %s \n", errno, strerror(errno));
sleep(2);
} else {
printf("C: accepted\n");
FD_SET(new_sockfd, &readset); // sets bit for new_sockfd to list of sockets to watch out for
if (maxfd < new_sockfd)
maxfd = new_sockfd;
if (minfd > new_sockfd)
minfd = new_sockfd;
} //end if else (new_sockfd)
} // end of the forever while loop
}
int handle_request(void * arg, struct message * msg)
{
char * cname, gname, payload;
char * command[3];
int i, rv, sockfd, client_addr_len;
struct sockaddr_in client_address;
struct fd_info * info;
info = (struct fd_info *) arg;
sockfd = info->sock_fd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("HR: ERROR socket() %i: %s \n", errno, strerror(errno));
break;
}
else if (sockfd > 0)
{
printf("HR: new socks is %i \n", sockfd);
printf("HR: sin_family is %i: %i\n", msg->peer.client_address.sin_family, msg->peer.client_address.sin_port);
//*************************************************************
//this is the part that returns error 111: Connection refused!!!
//*************************************************************
rv = connect(sockfd, (struct sockaddr *) &msg->peer.client_address, sizeof(struct sockaddr));
if (rv == -1)
{
printf("HR: ERROR: connect() %i: %s \n", errno, strerror(errno));
printf("HR: at %li \n", msg->peer.client_address.sin_addr.s_addr);
break;
}
else if (rv > 0)
{
info->max_fd = sockfd;
printf("HR: connected successfully!! \n");
}
}
}
Hello ladies and gents, I'm having a bit of problem with accept()
. I have a multiple clients and one server. The clients can connect and communicate just fine with server. But at one point, I need some clients to be directly connected to each other and I'm having a bit of difficulty there. The clients have bunch of threads going on, where one of them is handle_connection()
and it has a while(1)
, looping forever to listen()
and accept()
whatever incoming connections. Whenever a client tries to connect()
to other client, connect()
returns an error, 111: Connection Refused. I know I have the right IP address and right port (I have specified a port just for between-client connections). The client that is waiting for connection doesn't notice anything, no new connection, nada. I copied some parts of the code, in hopes that someone can point out what I'm doing wrong. Thanks for any inputs!
This is all client side code.void * handle_connections(void * arg)
is a thread that loops forever to accept()
any incoming connections. My server has a very similar thang going on and it works very well. (not sure why it doesn't work here..) This is the part of client that is waiting for a new incoming connection.int handle_request(void * arg, struct message * msg)
is called at one point during program and tries to connect to a client that is specified in struct message * msg
which includes struct sockaddr_in
with IP address and port number and whatever.
#define SERVER_PORT 10000
#define CLIENT_PORT 3456
#define MAX_CONNECTION 20
#define MAX_MSG 50
void * handle_connections(void * arg)
{
struct fd_info * info;
struct sockaddr_in client_address;
struct timeval timeout;
fd_set readset, copyset;
bzero((char * ) &client_address, sizeof(client_address)); // copy zeroes into string
client_address.sin_family = AF_INET;
client_address.sin_addr.s_addr = htonl(INADDR_ANY);
client_address.sin_port = htons(CLIENT_PORT);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
rv = listen(sockfd,MAX_CONNECTION);
while(1)
{
new_sockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_addr_len); //blocks
if (new_sockfd < 0) {
printf("C: ERROR accept() %i: %s \n", errno, strerror(errno));
sleep(2);
} else {
printf("C: accepted\n");
FD_SET(new_sockfd, &readset); // sets bit for new_sockfd to list of sockets to watch out for
if (maxfd < new_sockfd)
maxfd = new_sockfd;
if (minfd > new_sockfd)
minfd = new_sockfd;
} //end if else (new_sockfd)
} // end of the forever while loop
}
int handle_request(void * arg, struct message * msg)
{
char * cname, gname, payload;
char * command[3];
int i, rv, sockfd, client_addr_len;
struct sockaddr_in client_address;
struct fd_info * info;
info = (struct fd_info *) arg;
sockfd = info->sock_fd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("HR: ERROR socket() %i: %s \n", errno, strerror(errno));
break;
}
else if (sockfd > 0)
{
printf("HR: new socks is %i \n", sockfd);
printf("HR: sin_family is %i: %i\n", msg->peer.client_address.sin_family, msg->peer.client_address.sin_port);
//*************************************************************
//this is the part that returns error 111: Connection refused!!!
//*************************************************************
rv = connect(sockfd, (struct sockaddr *) &msg->peer.client_address, sizeof(struct sockaddr));
if (rv == -1)
{
printf("HR: ERROR: connect() %i: %s \n", errno, strerror(errno));
printf("HR: at %li \n", msg->peer.client_address.sin_addr.s_addr);
break;
}
else if (rv > 0)
{
info->max_fd = sockfd;
printf("HR: connected successfully!! \n");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您没有在
listen()
之前调用bind()
,因此您正在监听随机选择的端口上的连接,而不是您想要监听的端口在。您需要:
在
listen()
之前。请注意,传递给
accept()
的sockaddr
结构不是要侦听的地址 - 它是accept()
存储的输出参数accept()
返回时已连接的客户端的地址。Your problem is that you are not calling
bind()
beforelisten()
, so you are listening for connections on a randomly-chosen port, not the port you want to listen on.You need:
before the
listen()
.Note that the
sockaddr
structure passed toaccept()
isn't the address to listen on - it's an output parameter foraccept()
to store the address of the client that has connected whenaccept()
returns.