- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定我的程序在哪里/如何挂起。我非常有信心它是在发送或接收链接中......但即使在互联网上搜索一些帮助并从我的教授那里获得帮助后,我仍然迷失,至于为什么这不起作用。
#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/
这个问题已经有答案了: Can the HWND from CreateWindow/CreateDialog be GetMessage'd from another thread? (7 个回答)
作为我的应用程序的一部分,我需要阻止来电。也就是说,当我的应用程序正在运行时,我将挂断所有调用并简单地通知调用者用户正忙或类似的事情。 我想用android SDK做这个,希望能支持Android 2
我正在尝试在 Android 上使用 Asmack API 实现一个简单的 XMPP 信使。这是代码: private final String XMPP_SERVER = "jabber.o
我刚刚安装了 watir,在 exec 之后 require 'watir-webdriver' browser = Watir::Browser.new :firefox 它只是打开浏览器,但一直挂
我有一个简单的问题,我需要找到波形的峰值。现在,我在使用 scipy 库中的 find_peaks_cwt 方法之前已经完成了此操作。然而,在这种情况下,我的程序在尝试找到峰值时只是挂起。我认为这与波
我不确定我的程序在哪里/如何挂起。我非常有信心它是在发送或接收链接中......但即使在互联网上搜索一些帮助并从我的教授那里获得帮助后,我仍然迷失,至于为什么这不起作用。 #include #inc
我正在尝试通过 appcelerator studio 为 android 应用程序商店打包我的应用程序,但在处理 javascript 文件后,控制台显示此错误 2017-04-17T22:47:3
const https = require("https"); const fs = require("fs"); const options = { hostname: "en.wikipedi
我有一个使用 Twilio 运行的应用程序。这个想法是你将调用一个电话号码,twilio 会接听并为你提供菜单选项。一旦你按下一个数字,它就会提交发布数据然后挂断(这部分工作正常)答案...我在他们的
Java 解决方案不是问题: public boolean killCall(Context context) { try { // Get the boring old Te
我试图在我的服务器上运行 jenkins,但我总是收到相同的消息,然后等待,等待,什么也没有。在官方网站上他们报告了这个问题,但我想问问是否有人知道如何解决这个问题,知道吗? 最佳答案 因为对我来说采
您好,在给定大量 json 对象的情况下,我在 NodeJS 上执行 HTTP 请求时遇到问题。给定小的 json 对象数组,请求工作正常。但是,如果我尝试增加 json 数组的大小,我会收到错误:套
我真的不知道发生了什么,直到今天早上我才检查这一切是否正常。我在 IDE 中看到的唯一异常(exception)是这个 IndexNotReadyException: Please change ca
我正在制作一个 ASP.NET (C#) 应用程序,它基本上是运行 Powershell 脚本以执行日常管理任务的网关。其中一些脚本使用 ActiveDirectory RSAT 模块,我发现其中一些
我是一名优秀的程序员,十分优秀!