Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C getaddrinfo()在尝试下载流时失败_C_Sockets_Url_Getaddrinfo - Fatal编程技术网

C getaddrinfo()在尝试下载流时失败

C getaddrinfo()在尝试下载流时失败,c,sockets,url,getaddrinfo,C,Sockets,Url,Getaddrinfo,我正在尝试使用套接字从livestream下载几秒钟 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* close() */ #include <sys/socket.h> #include <netdb.h> int main(void) { int sock; char host[] = &

我正在尝试使用套接字从livestream下载几秒钟

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close() */
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
    int sock;
    char host[] = "http://141.138.89.176/fun-1-44-128";
    char port[] = "80";
    struct addrinfo hints, *res;
    char message[] = "GET / HTTP/1.1\nHost: http://141.138.89.176/fun-1-44-128";
    unsigned int i;
    char buf[1024];
    int bytes_read;
    int status;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    status = getaddrinfo(host, port, &hints, &res);
    if (status != 0) {
        perror("getaddrinfo");
        return 1;
    }
    sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sock == -1) {
        perror("socket");
        return 1;
    }
    status = connect(sock, res->ai_addr, res->ai_addrlen);
    if (status == -1) {
        perror("connect");
        return 1;
    }
    freeaddrinfo(res);
    send(sock, message, strlen(message), 0);

    do {
        bytes_read = recv(sock, buf, 1024, 0);
        if (bytes_read == -1) {
            perror("recv");
        }
        else {
            printf("%.*s", bytes_read, buf);
        }
    } while (bytes_read > 0);

    close(sock);

    return 0;
}
  
#包括
#包括
#包括
#include/*close()*/
#包括
#包括
内部主(空)
{
int袜子;
字符主机[]=”http://141.138.89.176/fun-1-44-128";
字符端口[]=“80”;
结构addrinfo提示,*res;
char message[]=“GET/HTTP/1.1\n主机:http://141.138.89.176/fun-1-44-128";
无符号整数i;
char-buf[1024];
读取整数字节;
智力状态;
memset(提示和提示,0,提示大小);
hits.ai_family=AF_INET;
hits.ai_socktype=SOCK_流;
status=getaddrinfo(主机、端口、提示和res);
如果(状态!=0){
perror(“getaddrinfo”);
返回1;
}
sock=socket(res->ai_系列,res->ai_socktype,res->ai_协议);
如果(sock==-1){
佩罗(“插座”);
返回1;
}
状态=连接(sock,res->ai_addr,res->ai_addrlen);
如果(状态==-1){
perror(“连接”);
返回1;
}
freeaddrinfo(res);
发送(sock,message,strlen(message),0);
做{
字节读取=recv(sock,buf,1024,0);
如果(字节数_读取==-1){
perror(“recv”);
}
否则{
printf(“%s”,读取字节,buf);
}
}而(字节读取>0);
关闭(袜子);
返回0;
}
但是,代码编译时,
getaddrinfo()
失败。我假设这意味着找不到主机

这是我的网址:
它在我的浏览器中工作,所以我不知道发生了什么。有人能解释一下这个问题吗?

我在我的环境中测试了你的代码,它工作得很好,所以你的代码肯定是可以的。我建议您检查编译器,或者重新安装

编辑:好的,我想我找到问题了。这是因为主机应该是ip地址,而不是完整的链接

(还为getaddrinfo的错误报告添加了修复程序)

#包括
#包括
#包括
#include/*close()*/
#包括
#包括
内部主(空)
{
int袜子;
字符主机[]=“141.138.89.176”;
字符端口[]=“80”;
结构addrinfo提示,*res;
char message[]=“GET/HTTP/1.1\n主机:141.138.89.176/fun-1-44-128”;
无符号整数i;
char-buf[1024];
读取整数字节;
智力状态;
memset(提示和提示,0,提示大小);
hits.ai_family=AF_INET;
hits.ai_socktype=SOCK_流;
status=getaddrinfo(主机、端口、提示和res);
如果(状态!=0){
printf(“代码:%d\n”,状态);
printf(“消息:%s\n”,gai_strerror(状态));
返回1;
}
...

好的,我想出了解决办法

getaddrinfo()
只接受基本主机名、域名或ip地址,不接受子目录

这意味着192.168.0.4、localhost都是有效的。192.168.0.4/Search、localhost/Search不是有效的

您也不需要包括该方案。套接字不区分http和https,而是如何处理请求才有区别

您需要更改主机,使其仅包含IP地址

你想要的是:

char host[] = "141.138.89.176";
你现在不会得到那个错误

如果你想访问一个子目录,你可以将其作为
GET
请求传递。实际上是web服务器处理文件和目录内容。基本IP保持不变

首先,您的格式是错误的。应该是
\r\n
\r\n\n
才能完成。它应该是这样的:
char message[]=“GET/HTTP/1.1\r\n host:141.138.89.176/fun-1-44-128\r\n\n”

现在,如果您尝试此操作,您将得到一个400错误。这是因为它写得不正确。很像上面的
主机
只接受基本url/ip。请参阅
/
之后的
/
?这是您请求所需子目录/文件的地方。您现在拥有的
/
表明您需要顶级词典

应该是这样的:

message[] = "GET /fun-1-44-128 HTTP/1.1\r\nHost: 141.138.89.176\r\n\n\n";

它运行正常,编译正常,但是没有打印流数据。perror(“getaddrinfo”)触发,我不明白为什么。名称或服务未知,这是状态显示的错误消息。状态运行到-2。谢谢,我发现了问题所在。我只将顶级IP传递给
getaddrinfo()
,如果我想要一个子目录,我必须在子目录中传递它
message[] = "GET /fun-1-44-128 HTTP/1.1\r\nHost: 141.138.89.176\r\n\n\n";