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

分享

Facebook
Plurk
YAHOO!

  • 分享在我的 Facebook
  • 分享在我的 Plurk
  • 分享在我的即時通

Copy from http://coding.debuntu.org/c-linux-socket-programming-tcp-simple-http-client

  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h>
  4. #include <stdlib.h>
  5. #include <netdb.h>
  6. #include <string.h>
  7. int create_tcp_socket ( );
  8. char *get_ip ( char *host );
  9. char *build_get_query ( char *host, char *page );
  10. void usage ( );
  11. #define HOST "coding.debuntu.org"
  12. #define PAGE "/"
  13. #define PORT 80
  14. #define USERAGENT "HTMLGET 1.0"
  15. int main ( int argc, char **argv )
  16. {
  17. struct sockaddr_in *remote;
  18. int sock;
  19. int tmpres;
  20. char *ip;
  21. char *get;
  22. char buf [BUFSIZ +1 ];
  23. char *host;
  24. char *page;
  25. if (argc == 1 ) {
  26. usage ( );
  27. exit ( 2 );
  28. }  
  29. host = argv [ 1 ];
  30. if (argc > 2 ) {
  31. page = argv [ 2 ];
  32. } else {
  33. page = PAGE;
  34. }
  35. sock = create_tcp_socket ( );
  36. ip = get_ip (host );
  37. fprintf (stderr, "IP is %s\n", ip );
  38. remote = ( struct sockaddr_in * )malloc ( sizeof ( struct sockaddr_in * ) );
  39. remote ->sin_family = AF_INET;
  40. tmpres = inet_pton (AF_INET, ip, ( void * ) ( & (remote ->sin_addr. s_addr ) ) );
  41. if ( tmpres < 0 )  
  42. {
  43. perror ( "Can't set remote->sin_addr.s_addr" );
  44. exit ( 1 );
  45. } else if (tmpres == 0 )
  46. {
  47. fprintf (stderr, "%s is not a valid IP address\n", ip );
  48. exit ( 1 );
  49. }
  50. remote ->sin_port = htons (PORT );
  51. if (connect (sock, ( struct sockaddr * )remote, sizeof ( struct sockaddr ) ) < 0 ) {
  52. perror ( "Could not connect" );
  53. exit ( 1 );
  54. }
  55. get = build_get_query (host, page );
  56. fprintf (stderr, "Query is:\n<<START>>\n%s<<END>>\n", get );
  57. //Send the query to the server
  58. int sent = 0;
  59. while (sent < strlen (get ) )
  60. {
  61. tmpres = send (sock, get +sent, strlen (get ) -sent, 0 );
  62. if (tmpres == -1 ) {
  63. perror ( "Can't send query" );
  64. exit ( 1 );
  65. }
  66. sent += tmpres;
  67. }
  68. //now it is time to receive the page
  69. memset (buf, 0, sizeof (buf ) );
  70. int htmlstart = 0;
  71. char * htmlcontent;
  72. while ( (tmpres = recv (sock, buf, BUFSIZ, 0 ) ) > 0 ) {
  73. if (htmlstart == 0 )
  74. {
  75. /* Under certain conditions this will not work.
  76.       * If the \r\n\r\n part is splitted into two messages
  77.       * it will fail to detect the beginning of HTML content
  78.       */
  79. htmlcontent = strstr (buf, "\r\n\r\n" );
  80. if (htmlcontent != NULL ) {
  81. htmlstart = 1;
  82. htmlcontent += 4;
  83. }
  84. } else {
  85. htmlcontent = buf;
  86. }
  87. if (htmlstart ) {
  88. fprintf (stdout, htmlcontent );
  89. }
  90. memset (buf, 0, tmpres );
  91. }
  92. if (tmpres < 0 )
  93. {
  94. perror ( "Error receiving data" );
  95. }
  96. free (get );
  97. free (remote );
  98. free (ip );
  99. close (sock );
  100. return 0;
  101. }
  102. void usage ( )
  103. {
  104. fprintf (stderr, "USAGE: htmlget host [page]\n\
  105. \thost: the website hostname. ex: coding.debuntu.org\n\
  106. \tpage: the page to retrieve. ex: index.html, default: /\n" );
  107. }
  108. int create_tcp_socket ( )
  109. {
  110. int sock;
  111. if ( (sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {
  112. perror ( "Can't create TCP socket" );
  113. exit ( 1 );
  114. }
  115. return sock;
  116. }
  117. char *get_ip ( char *host )
  118. {
  119. struct hostent *hent;
  120. int iplen = 15; //XXX.XXX.XXX.XXX
  121. char *ip = ( char * )malloc (iplen +1 );
  122. memset (ip, 0, iplen +1 );
  123. if ( (hent = gethostbyname (host ) ) == NULL )
  124. {
  125. herror ( "Can't get IP" );
  126. exit ( 1 );
  127. }
  128. if (inet_ntop (AF_INET, ( void * )hent ->h_addr_list [ 0 ], ip, iplen ) == NULL )
  129. {
  130. perror ( "Can't resolve host" );
  131. exit ( 1 );
  132. }
  133. return ip;
  134. }
  135. char *build_get_query ( char *host, char *page )
  136. {
  137. char *query;
  138. char *getpage = page;
  139. char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
  140. if (getpage [ 0 ] == '/' ) {
  141. getpage = getpage + 1;
  142. fprintf (stderr, "Removing leading \"/\", converting %s to %s\n", page, getpage );
  143. }
  144. // -5 is to consider the %s %s %s in tpl and the ending \0
  145. query = ( char * )malloc (strlen (host ) +strlen (getpage ) +strlen (USERAGENT ) +strlen (tpl ) -5 );
  146. sprintf (query, tpl, getpage, host, USERAGENT );
  147. return query;
  148. }

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

posted on 2012-07-17 09:11  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/07/17/2594618.html

C: Linux Socket Programming, TCP, a simple HTTP client - 安東尼隨手記 - Yahoo!奇摩部落格相关推荐

  1. 【Linux网络】Linux Socket编程 TCP协议

    话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. --有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程 ...

  2. linux socket通信tcp,基于TCP协议的socket通信

    一.服务器端 1.创建serverSocket,即服务器端的socket,绑定指定的端口,并侦听此端口 ServerSocket server = new ServerSocket(8888); 2. ...

  3. linux socket 多人聊天软件,Linux Socket编程---TCP实现多客户端的网络聊天室

    #include//打开文件 int fd; //客户端socket int client_sock; //文件路径 char path[100]; //段口号 char port[100]; //发 ...

  4. linux c socket programming

    http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http://www.slideshare.net/Arbow/asyn ...

  5. Linux Kernel TCP/IP Stack — Socket Layer — TCP/UDP Socket 网络编程

    目录 文章目录 目录 TCP/UDP Socket 逻辑架构 创建 Socket 绑定 Socket 请求建立 Socket 连接 监听 Socket 接受请求 关闭连接 数据的发送和接收 send ...

  6. linux下socket编程-TCP

    网络字节序 发送主机通常将发送缓冲区中的数据按内存地址从低到高的顺序发出,接收主机把从网络上接到的字节依次保存在接收缓冲区中,也是按内存地址从低到高的顺序保存,因此,网络数据流的地址应这样规定:先发出 ...

  7. 基于Socket的TCP协议Linux聊天系统(即时聊天、传送文件)

    基于Socket的TCP协议Linux聊天系统(即时聊天.传送文件)有源码!!!! 4.1 需求分析 4.1.1 系统目的 4.1.2 系统结构图 4.1.3 系统功能 4.1.4 系统数据流图 4. ...

  8. Linux下基于TCP的简易文件传输(socket编程)

    Linux下基于TCP的简易文件传输(socket编程) OSI和TCP/IP: 关于TCP/IP协议 关于TCP协议 TCP编程的一般步骤[^2] TCP文件传输实现 功能概述 服务器编程 客户端编 ...

  9. UDP socket programming in php

    2019独角兽企业重金招聘Python工程师标准>>> http://www.binarytides.com/udp-socket-programming-in-php/ UDP s ...

最新文章

  1. Struts2(1)简介
  2. 09、HTLM中直接写get请求和模板标签分片功能
  3. java 19 - 11 异常的注意事项
  4. JS向后台传递json数组对象
  5. [解读REST] 6.REST的应用经验以及教训
  6. inner join、 left join 、right join、full outer join之间的区别
  7. 局域网远程桌面无法连接到远程计算机,局域网无法远程连接桌面怎么解决
  8. 文献阅读(19)ISSCC 2020
  9. 想知道PDF转Word软件哪个好?向你推荐3个自用软件
  10. 移动端H5页面设计知识
  11. 没有进出口权,怎样接收外汇?
  12. 事件抽取(event extraction)
  13. TypeError: ufunc ‘isinf‘ not supported for the input types, and the inputs could not be safely...
  14. 文件下载(三):wireshark抓包文件下载整个过程
  15. python获取网卡名称_用Python获取计算机网卡信息
  16. redis指定数据库
  17. 【每日早报】2019/12/17
  18. CAN总线隔离器 插入式CAN总线隔离器
  19. 2023超简单解决“请在微信客户端打开链接”
  20. 软件测试精品在线开放课程申报书,精品在线开-放课程申报书.pdf

热门文章

  1. 使用canvas建立一个可以使用彩笔、橡皮檫的画板
  2. “巫妖王”袭来,工作游戏何去何从
  3. python二进制转为十进制-Python实现的十进制小数与二进制小数相互转换功能
  4. 杭州市安全技术防范行业协会第八届(换届)会员大会暨第八届第一次理事、监事会议顺利召开... 1
  5. linux分区,硬盘大于2T
  6. css网页屏幕自适应,css自适应宽度 如何让网页自适应所有屏幕宽度
  7. 两个List集合取交集、并集、差集
  8. 一文带你直观感受,BPM管理系统如何在低代码平台实现搭建
  9. Linux下开启SFTP服务
  10. 为什么感觉假期还没开始就快要结束了?