我正在尝试创建一个网络服务器,该服务器将接受来自sockets
(以不同的ports
表示)的请求和命令。我有一些用于简单套接字通信的演示code
,可以按预期方式编译和运行,但是当我运行我的演示(使用demo作为引用编写,这就是为什么有一些printf's
)时,accept()
失败,并显示以下内容:
任何建议/纠正表示赞赏。
这是我的代码:
#include <iostream>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstring>
#include <cstdlib>
#include <cstddef>
void perror_exit(char *message);
void thread_run();
int main(int argc, char const *argv[])
{
int i, filepos, serving_port=0, command_port=0, thread_num=0;
char *root_dir = (char *)malloc(sizeof(char *));
int serv_sock=0, command_sock=0, new_sock_c, new_sock_s=0;
struct sockaddr_in server,commands;
struct sockaddr_in server_client,commands_client;
struct sockaddr *serverptr=(struct sockaddr *)&server;
struct sockaddr *commandsptr=(struct sockaddr *)&commands;
struct sockaddr *server_clientptr=(struct sockaddr *)&server_client;
struct sockaddr *commands_clientptr=(struct sockaddr *)&commands_client;
socklen_t clientlen_s, clientlen_c;
struct hostent *rem_s, *rem_c;
socklen_t clientlen;
if(argc != 9){
perror("Wrong number of arguments, webServer");
exit(1);
}else{
for (i=0;i<argc;i++){
if (strcmp(argv[i],"-p") == 0){
serving_port = atoi(argv[i+1]);
}
}
for (i=0;i<argc;i++){
if (strcmp(argv[i],"-c") == 0){
command_port = atoi(argv[i+1]);
}
}
for (i=0;i<argc;i++){
if (strcmp(argv[i],"-t") == 0){
thread_num = atoi(argv[i+1]);
}
}
for (i=0;i<argc;i++){
if (strcmp(argv[i],"-d") == 0){
filepos = i+1;
strncpy(root_dir, argv[i+1], strlen(argv[i+1])+1); // +1 to get "\n" too, without +1 seg fault appears
}
}
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(serving_port);
commands.sin_family = AF_INET;
commands.sin_addr.s_addr = htonl(INADDR_ANY);
commands.sin_port = htons(command_port);
if ((serv_sock = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror_exit("Failed to create serving socket");
// return -1;
}
if ((command_sock = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror_exit("Failed to create command socket");
// return -1;
}
if (bind(serv_sock, serverptr, sizeof(server)) < 0)
perror_exit("bind");
printf("Listening for connections to port %d\n", serving_port);
if (bind(command_sock, commandsptr, sizeof(server)) < 0)
perror_exit("bind");
printf("Listening for commands to port %d\n", command_port);
while(true)
{
clientlen_s = sizeof(server_client);
clientlen_c = sizeof(commands_client);
if ((new_sock_s = accept(serv_sock, server_clientptr, &clientlen_s)) < 0)
perror_exit("accept (server)");
if ((new_sock_c = accept(command_sock, commands_clientptr, &clientlen_c)) < 0)
perror_exit("accept (commands)");
if ((rem_s = gethostbyaddr((char *) &server_client.sin_addr.s_addr, sizeof(server_client.sin_addr.s_addr), server_client.sin_family)) == NULL) {
herror("gethostbyaddr"); exit(1);
}
printf("Accepted serving connection from %s\n", rem_s->h_name);
if ((rem_c = gethostbyaddr((char *) &commands_client.sin_addr.s_addr, sizeof(commands_client.sin_addr.s_addr), commands_client.sin_family)) == NULL) {
herror("gethostbyaddr"); exit(1);
}
printf("Accepted command connection from %s\n", rem_c->h_name);
}
/* code */
return 0;
}
void perror_exit(char *message)
{
perror(message);
exit(EXIT_FAILURE);
}
void thread_run()
{
}
最佳答案
在致电接受之前,您应该致电监听。
从accept manual:
您应该在循环之前添加:
listen(serv_sock,5);
listen(command_sock,5);
但是,为了在没有连接挂起时不阻止
accept
调用,您应该考虑使用select或poll。这将允许在调用
accept
之前检查连接是否挂起关于c++ - 套接字接受无效的参数(C++),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50293158/