C 接收网站的简单套接字程序(断言错误)
我是socketprogramming新手,想编写一个小控制台应用程序来接收网页并打印出来 这是我的代码:C 接收网站的简单套接字程序(断言错误),c,sockets,network-programming,C,Sockets,Network Programming,我是socketprogramming新手,想编写一个小控制台应用程序来接收网页并打印出来 这是我的代码: #include <stdio.h> #include <winsock2.h> #include <winsock.h> #include <Ws2tcpip.h> #pragma comment(lib, "ws2_32.lib" ) #define BUFFERSIZE 5000 int main() { WSADATA
#include <stdio.h>
#include <winsock2.h>
#include <winsock.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib" )
#define BUFFERSIZE 5000
int main()
{
WSADATA wsaData;
char buffer[BUFFERSIZE];
char *getMsg = {"GET / HTTP/1.0\nUser-Agent: HTTPTool/1.0\n\n"};
char * url = (char*)malloc(BUFFERSIZE);
int socket_fd = 0;
int receivingContent = 1;
int errorValue;
int numbytes;
struct addrinfo hints;
struct addrinfo *servinfo, *p;
socklen_t addr_size;
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
memset(&hints, 0, sizeof(hints)); // empty struct
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // using TCP
printf("Give a website like for example: www.google.com \n");
scanf("%s",url);
//Error testing
if ((errorValue = getaddrinfo(url, "80", &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(errorValue));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((socket_fd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
perror("Failed to create socket");
continue;
}
break;
}
//sending the http get message to the server
send(socket_fd, getMsg, strlen(getMsg), 0);
//While the full content isn't downloaded keep receiving
while(receivingContent)
{
numbytes = recv(socket_fd, buffer,BUFFERSIZE , 0);
if(numbytes > 0)
{
buffer[numbytes]='\0';
printf("%s", buffer);
}
else
receivingContent = 0; //stop receiving
}
free(url);
free(getMsg);
freeaddrinfo(servinfo);
closesocket(socket_fd); //close the socket
WSACleanup();
return 0;
}
#包括
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)
#定义缓冲区大小5000
int main()
{
WSADATA WSADATA;
字符缓冲区[BUFFERSIZE];
char*getMsg={“GET/HTTP/1.0\n用户代理:HTTPTool/1.0\n\n”};
char*url=(char*)malloc(BUFFERSIZE);
int socket_fd=0;
int receivingContent=1;
错误值;
整数单位;
结构addrinfo提示;
结构addrinfo*servinfo,*p;
袜子尺寸;
if(WSAStartup(MAKEWORD(2,0),&wsaData)!=0)
{
fprintf(stderr,“WSAStartup失败。\n”);
出口(1);
}
memset(&hints,0,sizeof(hints));//空结构
hints.ai_family=AF_unsec;//IPv4或IPv6
hints.ai_socktype=SOCK_STREAM;//使用TCP
printf(“给出一个网站,例如:www.google.com\n”);
scanf(“%s”,url);
//错误测试
如果((errorValue=getaddrinfo(url,“80”&提示和服务信息))!=0){
fprintf(标准,“getaddrinfo:%s\n”,gai_strerror(errorValue));
返回1;
}
//循环遍历所有结果并绑定到第一个结果
for(p=servinfo;p!=NULL;p=p->ai_next){
if((socket\u fd=socket(p->ai\u族,p->ai\u socktype,p->ai\u协议))=-1){
perror(“未能创建套接字”);
继续;
}
打破
}
//将http get消息发送到服务器
发送(socket_fd,getMsg,strlen(getMsg),0);
//当未下载完整内容时,请继续接收
while(接收内容)
{
numbytes=recv(socket_fd,buffer,BUFFERSIZE,0);
如果(字节数>0)
{
缓冲区[numbytes]='\0';
printf(“%s”,缓冲区);
}
其他的
receivingContent=0;//停止接收
}
免费(网址);
免费(getMsg);
freeaddrinfo(servinfo);
closesocket(socket_fd);//关闭套接字
WSACleanup();
返回0;
}
它会编译,然后当我输入例如www.google.com时,它崩溃了,它给了我一个C文件。我在寻找错误,但我认为我做得对
有人能帮我吗
向您问好,我在您的程序中没有找到
connect
。由于Marcus,您可能需要在执行recv
之前执行connect
,需要在执行recv之前执行connect。还有一个小问题是,我在连接之前发送了一个消息。改变这一点,一切都会成功
谢谢你的帮助。
文件的C
?这是什么意思?我在send命令connect(socket\u fd,servinfo->ai\u addr,servinfo->ai\u addrlen)之后添加了这一行;程序不再崩溃,但它处于一个无休止的循环中,不打印任何内容:DIt不超过这一行=“numbytes=recv(socket\u fd,buffer,BUFFERSIZE,0);”