gpt4 book ai didi

html - 挂断 HTTP Get/Request

转载 作者:行者123 更新时间:2023-11-30 17:22:02 26 4
gpt4 key购买 nike

我不确定我的程序在哪里/如何挂起。我非常有信心它是在发送或接收链接中......但即使在互联网上搜索一些帮助并从我的教授那里获得帮助后,我仍然迷失,至于为什么这不起作用。

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define LENGTH 500

int main(int argc, char** argv)
{
// holds the value of the command-line argument
char link[LENGTH];
strcpy (link , argv[1]);
printf ("%s\n", link);

// break up link into the host and path -- use the "/"
char _host[LENGTH];
char _path[LENGTH];
int slashCount = 0;
int i;

for (i=0 ; i<strlen(link); i++)
{
printf ("%c , slashcount: %d\n", link[i], slashCount);
if (link[i] == '/') slashCount ++;
if (slashCount > 2)
{
// i is at the end of the host
strncpy (_host , link , i);
_host[i] = '\0';
break;
}

} // end for

if (slashCount == 2)
{
// given only a host, copy the entire string into the host
strncpy(_host, link, i);
_host[i] = '\0';
}

else // get the path for slashCount of 3
{
for (i ; strlen(link); i++ )
{
strcat (_path, &link[i]);
// if slash at the end of host name
if (strlen(_path) == 1)
{
// set the file field to be index.html
char t[11] = {'i','n','d','e','x','.','h','t','m','l','\0'};
for (int c=0; c< 10 ; c++)
strncpy(_path, t, 11);
}
break;
}
} // end if/else

printf ("%s\n" , _host); /*success*/
printf ("%s\n" , _path); /*success*/

struct addrinfo hints;
struct addrinfo * result, * rp;
int sock_fd, s;

hints.ai_socktype = SOCK_STREAM;
memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0

s = getaddrinfo("::1","http", &hints,&result); ///////////////
if (0 != s)
{
perror("error populating address structure");
exit(1);
}
printf ("entering tcp setup connections");

// check the host name’s IP address — has a dynamic configuration(DNS)
// but does not change very frequently
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sock_fd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock_fd == -1)
continue;

if (connect(sock_fd, rp->ai_addr, rp->ai_addrlen) != -1)
break;

close(sock_fd);
}

if (rp == NULL)
{
// you didn’t find a good ip address to connect with
fprintf(stderr, "could not connect\n");
exit(1);
} // end for

freeaddrinfo(result);

// sending the request
sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path, _host);
if (send (sock_fd, link, strlen(link), 0 )< 0)
fprintf (stderr, "Error with send\n");
else
fprintf (stderr, "Successfully send the fetch request\n");

// responding to the send
char buf[500];
memset(&buf,0,sizeof(buf));
/* int recv_count = recv(sock_fd, buf, 500, 0);

if(recv_count<0)
{
perror("Receive failed");
exit(1);
}

printf("%s",buf); */
/* while (0 > recv (sock_fd, buf, 500, 0))
{
printf ("%s", buf);
}
shutdown(sock_fd,SHUT_RDWR); */
exit (1);
}

我知道我必须发送一个 get 请求,我注释掉了响应部分,并看到已成功进入 tcp 连接设置。我只是很困惑应该如何设置响应。我了解如何使用读/写,但我也想尝试发送/接收。

非常感谢任何帮助。

我当前得到的输出:

http://google.com/
h , slashcount: 0
t , slashcount: 0
t , slashcount: 0
p , slashcount: 0
: , slashcount: 0
/ , slashcount: 0
/ , slashcount: 1
g , slashcount: 2
o , slashcount: 2
o , slashcount: 2
g , slashcount: 2
l , slashcount: 2
e , slashcount: 2
. , slashcount: 2
c , slashcount: 2
o , slashcount: 2
m , slashcount: 2
/ , slashcount: 2
http://google.com
index.html
Successfully send the fetch request
entering tcp setup connections

最佳答案

recv() 的返回值表示连接被拒绝,这表明套接字设置存在问题。

您需要颠倒两行的顺序:

hints.ai_socktype = SOCK_STREAM;
memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0

以便您在将内存设置为零后分配ai_socktype

getaddrinfo() 调用中,将从 cli 接收到的字符串 link 作为第一个参数传递(::1 的别名>本地主机)。

s = getaddrinfo(link,"http", &hints,&result); ///////////////

更改代码结果:

woof@fth ~ % ./test.o google.com        
google.com
sock_fd=3
socket opened
Successfully sent the fetch request
HTTP/1.0 404 Not Found
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sun, 25 Jan 2015 11:07:09 GMT
Server: sffe
Content-Length: 1425
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 80:quic,p=0.02

<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}%

这意味着服务器已响应。

找不到该文件,因为在行中

sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path,    _host);  

您请求一个不存在的路径;如果你尝试使用

sprintf (link, "GET / HTTP/1.0\n\n");  

您应该看到不同的东西(可能是另一个错误)。如果您想获得不同的路径,则需要将其正确插入到请求中(努力从输入字符串中识别它)。

<小时/>

我编译的代码和你的几乎一样:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


#define LENGTH 500

int main(int argc, char** argv) {
// holds the value of the command-line argument
char link[LENGTH];
strcpy (link , argv[1]);
printf ("%s\n", link);

// break up link into the host and path -- use the "/"
char _host[LENGTH];
char _path[LENGTH];
int slashCount = 0;
int i,c;

for (i=0 ; i<strlen(link); i++) {

if (link[i] == '/')
slashCount ++;

if (slashCount > 2) {
// i is at the end of the host
strncpy (_host , link , i);
_host[i] = '\0';
break;
}
}

if (slashCount == 2) {
// given only a host, copy the entire string into the host
strncpy(_host, link, i);
_host[i] = '\0';
}
else {
// get the path for slashCount of 3
for (i ; strlen(link); i++ ) {
strcat (_path, &link[i]);
// if slash at the end of host name
if (strlen(_path) == 1)
{
// set the file field to be index.html
char t[11] = {'i','n','d','e','x','.','h','t','m','l','\0'};
strncpy(_path, t, 11);
}
break;
}
} // end if/else

struct addrinfo hints;
struct addrinfo *result, *rp;
int sock_fd, s;

memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0
hints.ai_socktype = SOCK_STREAM; // tcp socket

// create addrinfo structure with parameters to initialize a socket
s = getaddrinfo("::1","8080", &hints,&result); ///////////////

if( s!=0 ) {
perror("error populating address structure");
exit(1);
}

char temp[5000];

// check the host name’s IP address — has a dynamic configuration(DNS)
// but does not change very frequently
for( rp=result; rp!=NULL; rp=rp->ai_next ) {
sock_fd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol);
fprintf(stderr,"sock_fd=%d\n",sock_fd);

if (sock_fd == -1)
continue;

// exit loop on success
if( connect(sock_fd, rp->ai_addr, rp->ai_addrlen)==0 )
break;
else
close(sock_fd);
}

if( rp==NULL ) {
// you didn’t find a good ip address to connect with
fprintf(stderr, "could not connect\n");
exit(1);
}
else
printf("socket opened\n");

freeaddrinfo(result);


// formatting the request string
sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path, _host);
// sending the request
if( send(sock_fd, link, strlen(link), 0)<0 )
fprintf(stderr, "Error with send\n");
else
fprintf (stderr, "Successfully sent the fetch request\n");

// response buffer
char buf[500];
memset(&buf,0,sizeof(buf));

int recv_count = recv(sock_fd, buf, 500, 0);

if( recv_count<0 ){
perror("Receive failed");
exit(1);
}

printf("%s",buf);
/* while (0 > recv (sock_fd, buf, 500, 0))
{
printf ("%s", buf);
}
shutdown(sock_fd,SHUT_RDWR); */
exit (0);
}

更改了函数调用并恢复了 GET 请求的 sprintf 格式。您应该在单独的终端上启动命令

echo "Here is a message" | nc -l -6 8080

它会暂停执行,直到您在端口上收到请求(标志-l,有关更多信息,请参阅netcat manual)。

请求打印在 netcat 终端上,并且进程终止,而在启动编译代码的终端上,您应该看到消息在 netcat 内回显和通过管道传输。

关于html - 挂断 HTTP Get/Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28133501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com