linux-http小程序
来源:互联网 发布:淘宝老七家 编辑:程序博客网 时间:2024/06/05 00:08
本程序比较简单,主要是使用htpp协议的GET命令来获取web根目录的信息。
程序接收到web服务端返回的程序后,在终端中显示,并且在当前目录创建一个文本来记录信息。
上图:
源码:
#include <signal.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/time.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <netdb.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h>#include <errno.h>#include <fcntl.h>int main(int argc, char *argv[]){int sockfd,conn,filefd;int port = 80;char buffer[1024];char MsgBuf[2048];struct hostent *host;static struct sockaddr_in mysock;if(argc < 2){printf("usage:%s hostname/IP address\n",argv[0]);exit(1);}if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0){perror("socket error!\n");exit(1);}if((host = gethostbyname(argv[1])) == NULL){perror("gethostbyname error");exit(1);}mysock.sin_family = AF_INET;mysock.sin_addr = *((struct in_addr *)host->h_addr);mysock.sin_port = htons(port);printf("Msg: %s(%s)\n",argv[1],inet_ntoa(mysock.sin_addr));conn = connect(sockfd,(struct sockaddr*)&mysock,sizeof(mysock));if(conn < 0){perror("connect error!\n");close(sockfd);exit(1);}memset(&buffer,0,1024);memset(&MsgBuf,0,sizeof(MsgBuf));printf("send packeting.. \n");//sprintf(buffer,"GET / HTTP/1.1\r\nHost:%s\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686; rv:22.0) Gecko/20100101 Firefox/22.0 Iceweasel/22.0\r\n\r\n",argv[1]);sprintf(buffer,"GET / HTTP/1.1\r\nHost:%s\r\nUser-Agent: Mozilla/5.0\r\n\r\n",argv[1]);if(write(sockfd,buffer,sizeof(buffer))<0){perror("write error!\n");close(sockfd);exit(1);}//printf("send success!\n");if((read(sockfd,MsgBuf,sizeof(MsgBuf)))<0){perror("read error!\n");exit(1);}if((filefd = creat("http.txt",S_IRUSR|S_IWUSR))<0){perror("creat file errno\n");}if((write(filefd,MsgBuf,sizeof(MsgBuf)))<0){perror("write file errno\n");}printf("%s",MsgBuf);close(sockfd);return 0;}
- linux-http小程序
- linux小程序
- LINUX 网络小程序
- linux小程序
- 关于小程序http请求的问题
- 小程序-tomcat http 转 https
- LINUX同步聊天小程序
- linux异步聊天小程序
- linux 流量统计小程序
- linux下的小程序
- Linux之小程序进度条
- Linux下进度条小程序
- Linux脚本小程序 时钟
- Linux实现进度条小程序
- Linux入门:进度条小程序
- Linux简单小程序---进度条
- c linux服务器简单http server程序
- Linux下扬声器发声的小程序
- 23种设计模式--代理模式
- SqlServer数据库性能优化详解
- JXL 颜色使用方式
- Qt文件操作
- POJ 3087 Shuffle'm Up
- linux-http小程序
- ifconfig: command not found
- Vim插件C.Vim之%lf及%lli报错问题
- llibsvm-svdd-3.1的编译make
- 使用HibernateTemplate()获取整个数据库表的记录数
- pthread编程基础
- 精确到时分秒的jQuery插件例子
- 数据库--函数--Oracle
- flex 、blazeds、j2ee生成不了html