C: Linux Socket Programming, TCP, a simple HTTP client - 安東尼隨手記 - Yahoo!奇摩部落格
C: Linux Socket Programming, TCP, a simple HTTP client - 安東尼隨手記 - Yahoo!奇摩部落格
檢舉C: Linux Socket Programming, TCP, a simple HTTP client
分類: Linux程式設計2009/04/16 10:46分享
Plurk
YAHOO!
- 分享在我的 Facebook
- 分享在我的 Plurk
- 分享在我的即時通
Copy from http://coding.debuntu.org/c-linux-socket-programming-tcp-simple-http-client
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #include <netdb.h> #include <string.h> int create_tcp_socket ( ); char *get_ip ( char *host ); char *build_get_query ( char *host, char *page ); void usage ( ); #define HOST "coding.debuntu.org" #define PAGE "/" #define PORT 80 #define USERAGENT "HTMLGET 1.0" int main ( int argc, char **argv ) { struct sockaddr_in *remote; int sock; int tmpres; char *ip; char *get; char buf [BUFSIZ +1 ]; char *host; char *page; if (argc == 1 ) { usage ( ); exit ( 2 ); } host = argv [ 1 ]; if (argc > 2 ) { page = argv [ 2 ]; } else { page = PAGE; } sock = create_tcp_socket ( ); ip = get_ip (host ); fprintf (stderr, "IP is %s\n", ip ); remote = ( struct sockaddr_in * )malloc ( sizeof ( struct sockaddr_in * ) ); remote ->sin_family = AF_INET; tmpres = inet_pton (AF_INET, ip, ( void * ) ( & (remote ->sin_addr. s_addr ) ) ); if ( tmpres < 0 ) { perror ( "Can't set remote->sin_addr.s_addr" ); exit ( 1 ); } else if (tmpres == 0 ) { fprintf (stderr, "%s is not a valid IP address\n", ip ); exit ( 1 ); } remote ->sin_port = htons (PORT ); if (connect (sock, ( struct sockaddr * )remote, sizeof ( struct sockaddr ) ) < 0 ) { perror ( "Could not connect" ); exit ( 1 ); } get = build_get_query (host, page ); fprintf (stderr, "Query is:\n<<START>>\n%s<<END>>\n", get ); //Send the query to the server int sent = 0; while (sent < strlen (get ) ) { tmpres = send (sock, get +sent, strlen (get ) -sent, 0 ); if (tmpres == -1 ) { perror ( "Can't send query" ); exit ( 1 ); } sent += tmpres; } //now it is time to receive the page memset (buf, 0, sizeof (buf ) ); int htmlstart = 0; char * htmlcontent; while ( (tmpres = recv (sock, buf, BUFSIZ, 0 ) ) > 0 ) { if (htmlstart == 0 ) { /* Under certain conditions this will not work. * If the \r\n\r\n part is splitted into two messages * it will fail to detect the beginning of HTML content */ htmlcontent = strstr (buf, "\r\n\r\n" ); if (htmlcontent != NULL ) { htmlstart = 1; htmlcontent += 4; } } else { htmlcontent = buf; } if (htmlstart ) { fprintf (stdout, htmlcontent ); } memset (buf, 0, tmpres ); } if (tmpres < 0 ) { perror ( "Error receiving data" ); } free (get ); free (remote ); free (ip ); close (sock ); return 0; } void usage ( ) { fprintf (stderr, "USAGE: htmlget host [page]\n\ \thost: the website hostname. ex: coding.debuntu.org\n\ \tpage: the page to retrieve. ex: index.html, default: /\n" ); } int create_tcp_socket ( ) { int sock; if ( (sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) { perror ( "Can't create TCP socket" ); exit ( 1 ); } return sock; } char *get_ip ( char *host ) { struct hostent *hent; int iplen = 15; //XXX.XXX.XXX.XXX char *ip = ( char * )malloc (iplen +1 ); memset (ip, 0, iplen +1 ); if ( (hent = gethostbyname (host ) ) == NULL ) { herror ( "Can't get IP" ); exit ( 1 ); } if (inet_ntop (AF_INET, ( void * )hent ->h_addr_list [ 0 ], ip, iplen ) == NULL ) { perror ( "Can't resolve host" ); exit ( 1 ); } return ip; } char *build_get_query ( char *host, char *page ) { char *query; char *getpage = page; char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n"; if (getpage [ 0 ] == '/' ) { getpage = getpage + 1; fprintf (stderr, "Removing leading \"/\", converting %s to %s\n", page, getpage ); } // -5 is to consider the %s %s %s in tpl and the ending \0 query = ( char * )malloc (strlen (host ) +strlen (getpage ) +strlen (USERAGENT ) +strlen (tpl ) -5 ); sprintf (query, tpl, getpage, host, USERAGENT ); return query; }To compile it, run:
$ gcc -o htmlget htmlget.c $ ./htmlget USAGE: htmlget host [page]host: the website hostname. ex: coding.debuntu.orgpage: the page to retrieve. ex: index.html, default: /Informative messages and errors are printed to stderr. The content of the page is printed to stdout. Thus, to save the HTML content of a page to a file, you will need to run:
$ ./htmlget coding.debuntu.org category > /tmp/page.html
转载于:https://www.cnblogs.com/lexus/archive/2012/07/17/2594618.html
C: Linux Socket Programming, TCP, a simple HTTP client - 安東尼隨手記 - Yahoo!奇摩部落格相关推荐
- 【Linux网络】Linux Socket编程 TCP协议
话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. --有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程 ...
- linux socket通信tcp,基于TCP协议的socket通信
一.服务器端 1.创建serverSocket,即服务器端的socket,绑定指定的端口,并侦听此端口 ServerSocket server = new ServerSocket(8888); 2. ...
- linux socket 多人聊天软件,Linux Socket编程---TCP实现多客户端的网络聊天室
#include//打开文件 int fd; //客户端socket int client_sock; //文件路径 char path[100]; //段口号 char port[100]; //发 ...
- linux c socket programming
http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http://www.slideshare.net/Arbow/asyn ...
- Linux Kernel TCP/IP Stack — Socket Layer — TCP/UDP Socket 网络编程
目录 文章目录 目录 TCP/UDP Socket 逻辑架构 创建 Socket 绑定 Socket 请求建立 Socket 连接 监听 Socket 接受请求 关闭连接 数据的发送和接收 send ...
- linux下socket编程-TCP
网络字节序 发送主机通常将发送缓冲区中的数据按内存地址从低到高的顺序发出,接收主机把从网络上接到的字节依次保存在接收缓冲区中,也是按内存地址从低到高的顺序保存,因此,网络数据流的地址应这样规定:先发出 ...
- 基于Socket的TCP协议Linux聊天系统(即时聊天、传送文件)
基于Socket的TCP协议Linux聊天系统(即时聊天.传送文件)有源码!!!! 4.1 需求分析 4.1.1 系统目的 4.1.2 系统结构图 4.1.3 系统功能 4.1.4 系统数据流图 4. ...
- Linux下基于TCP的简易文件传输(socket编程)
Linux下基于TCP的简易文件传输(socket编程) OSI和TCP/IP: 关于TCP/IP协议 关于TCP协议 TCP编程的一般步骤[^2] TCP文件传输实现 功能概述 服务器编程 客户端编 ...
- UDP socket programming in php
2019独角兽企业重金招聘Python工程师标准>>> http://www.binarytides.com/udp-socket-programming-in-php/ UDP s ...
最新文章
- Struts2(1)简介
- 09、HTLM中直接写get请求和模板标签分片功能
- java 19 - 11 异常的注意事项
- JS向后台传递json数组对象
- [解读REST] 6.REST的应用经验以及教训
- inner join、 left join 、right join、full outer join之间的区别
- 局域网远程桌面无法连接到远程计算机,局域网无法远程连接桌面怎么解决
- 文献阅读(19)ISSCC 2020
- 想知道PDF转Word软件哪个好?向你推荐3个自用软件
- 移动端H5页面设计知识
- 没有进出口权,怎样接收外汇?
- 事件抽取(event extraction)
- TypeError: ufunc ‘isinf‘ not supported for the input types, and the inputs could not be safely...
- 文件下载(三):wireshark抓包文件下载整个过程
- python获取网卡名称_用Python获取计算机网卡信息
- redis指定数据库
- 【每日早报】2019/12/17
- CAN总线隔离器 插入式CAN总线隔离器
- 2023超简单解决“请在微信客户端打开链接”
- 软件测试精品在线开放课程申报书,精品在线开-放课程申报书.pdf
热门文章
- 使用canvas建立一个可以使用彩笔、橡皮檫的画板
- “巫妖王”袭来,工作游戏何去何从
- python二进制转为十进制-Python实现的十进制小数与二进制小数相互转换功能
- 杭州市安全技术防范行业协会第八届(换届)会员大会暨第八届第一次理事、监事会议顺利召开... 1
- linux分区,硬盘大于2T
- css网页屏幕自适应,css自适应宽度 如何让网页自适应所有屏幕宽度
- 两个List集合取交集、并集、差集
- 一文带你直观感受,BPM管理系统如何在低代码平台实现搭建
- Linux下开启SFTP服务
- 为什么感觉假期还没开始就快要结束了?