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");
}
}
}
Your problem is that you are not calling bind()
before listen()
, so you are listening for connections on a randomly-chosen port, not the port you want to listen on.
You need:
if (bind(sockfd, &client_address, sizeof client_address)))
{
perror("bind");
/* Handle this error somehow */
}
before the listen()
.
Note that the sockaddr
structure passed to accept()
isn't the address to listen on - it's an output parameter for accept()
to store the address of the client that has connected when accept()
returns.
精彩评论