插座回报“没有这样的文件或目录" [英] socket return 'No such file or directory"
问题描述
Linux的GCC 4.4.2
我做了一些socket编程。
不过,我不断收到这个错误,当我试着从socket函数分配的sockfd。
非插槽Socket操作
非常感谢任何建议,
#如果定义(Linux版)
#包括LT&;&pthreads.h中GT;
/ *套接字特定函数和常量* /
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&;&netdb.h中GT;
#包括LT&;&errno.h中GT;
#万一#包括server.h
#包括cltsvr_ults.h/ *侦听指定端口上的连接* /
无效wait_client()
{
结构addrinfo中ADD_INFO,* add_res;
INT的sockfd; / *使用的getaddrinfo填补结构addrinfo中最多可加载的地址信息* /
memset的(安培; ADD_INFO,0,sizeof的(ADD_INFO));
/ *使用IPv4或IPv6 * /
add_info.ai_family = AF_UNSPEC;
add_info.ai_socktype = SOCK_STREAM;
/ *在我的IP地址填写* /
add_info.ai_flags = AI_PASSIVE; / *填充结构addrinfo中的* /
int32_t状态= 0;
如果(状态=的getaddrinfo(NULL,6000,&放大器; ADD_INFO,&放大器;!add_res)= 0)
{
fprintf中(标准错误的getaddrinfo [%S] \\ n,gai_strerror(状态)); 返回;
} 如果((的sockfd =(插座(add_res-> ai_family,add_res-> ai_socktype,add_res-> ai_protocol))== -1))
{
fprintf中(标准错误,插座不合格[%S] \\ n字符串错误(错误)); 返回;
} / *绑定到已由的getaddrinfo()指定的端口* /
如果(绑定(的sockfd,add_res-> ai_addr,add_res->!ai_addrlen)= 0)
{
fprintf中(标准错误,绑定失败[%S] \\ n字符串错误(错误)); 返回;
} 的printf(聆听客户端\\ n);
}
编辑==老派的方法编码
int32_t的sockfd = 0;
结构SOCKADDR_IN my_addr; memset的(安培; my_addr,0,sizeof的(my_addr)); my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(6000);
my_addr.sin_addr.s_addr = inet_addr(127.0.0.1); 的sockfd =插座(PF_INET,SOCK_STREAM,0);
如果(的sockfd == -1)
{
fprintf中(标准错误,插座不合格[%S] \\ n字符串错误(错误));
返回;
} 如果(绑定(的sockfd,(结构sockaddr *)及my_addr,sizeof的(my_addr))== -1)
{
fprintf中(标准错误,绑定失败[%S] \\ n字符串错误(错误));
返回;
}
您的主要问题是,你有当插座()有错误错误检查。插座()将返回-1错误,而不是0成功。你可以得到良好的插座值(2,3等)和治疗它像一个错误。
还有一个在你的圆括号您code方式的第二个问题。当你写:
如果(=的sockfd插座(add_res-> ai_family,add_res-> ai_socktype,add_res->!ai_protocol)= 0)
这被视为
如果(=的sockfd(套接字(add_res-> ai_family,add_res-> ai_socktype,add_res->!ai_protocol)= 0))
所以的sockfd将不会分配插座的返回值,但它比较反对0修复这两个问题的价值,你应该写:
如果((的sockfd =插座(add_res-> ai_family,add_res-> ai_socktype,add_res-> ai_protocol))== -1)
Linux GCC 4.4.2
I am doing some socket programming.
However, I keep getting this error when I try and assign the sockfd from the socket function.
" Socket operation on non-socket"
Many thanks for any advice,
#if defined(linux)
#include <pthread.h>
/* Socket specific functions and constants */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#endif
#include "server.h"
#include "cltsvr_ults.h"
/* Listens for a connection on the designated port */
void wait_client()
{
struct addrinfo add_info, *add_res;
int sockfd;
/* Load up the address information using getaddrinfo to fill the struct addrinfo */
memset(&add_info, 0, sizeof(add_info));
/* Use either IPv4 or IPv6 */
add_info.ai_family = AF_UNSPEC;
add_info.ai_socktype = SOCK_STREAM;
/* Fill in my IP address */
add_info.ai_flags = AI_PASSIVE;
/* Fill the struct addrinfo */
int32_t status = 0;
if(status = getaddrinfo(NULL, "6000", &add_info, &add_res) != 0)
{
fprintf(stderr, "getaddrinfo [ %s ]\n", gai_strerror(status));
return;
}
if((sockfd = (socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol)) == -1))
{
fprintf(stderr, "Socket failed [ %s ]\n", strerror(errno));
return;
}
/* Bind to the port that has been assigned by getaddrinfo() */
if(bind(sockfd, add_res->ai_addr, add_res->ai_addrlen) != 0)
{
fprintf(stderr, "Bind failed [ %s ]\n", strerror(errno));
return;
}
printf("Listening for clients\n");
}
Edit == coding in the old-school method
int32_t sockfd = 0;
struct sockaddr_in my_addr;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(6000);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
{
fprintf(stderr, "Socket failed [ %s ]\n", strerror(errno));
return;
}
if(bind(sockfd, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1)
{
fprintf(stderr, "Bind failed [ %s ]\n", strerror(errno));
return;
}
Your main problem is that you have the wrong check for when socket() has an error. socket() will return -1 on error, not 0 on success. You may be getting a good socket value (2, 3, etc.) and treating it like an error.
There is also a second problem in the way you are parenthesizing your code. When you write:
if (sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0)
That is being treated as:
if (sockfd = (socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0))
So sockfd will not be assigned the return value of socket, but the value of comparing it against 0. Fixing both problems, you should be writing:
if ((sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol)) == -1)
这篇关于插座回报“没有这样的文件或目录&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!