经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ 网络编程 单纯http客户端,服务器端
来源:cnblogs  作者:小石王  时间:2018/10/12 9:45:32  对本文有异议

网络编程 单纯http客户端,服务器端

1,http客户端

2,http服务器端

http客户端:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <errno.h>
  7. #include <netdb.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. int main(int argc, char* argv[]){
  11. int err;
  12. int sock;
  13. char buf[32];
  14. char* deststr;
  15. addrinfo hints, *res0, *res;
  16. if(argc != 2){return 1;}
  17. deststr = argv[1];
  18. memset(&hints, 0, sizeof(hints));
  19. hints.ai_socktype = SOCK_STREAM;
  20. hints.ai_family = PF_UNSPEC;
  21. if((err = getaddrinfo(deststr, "http", &hints, &res0)) != 0){
  22. printf("error %d:%s\n", err, gai_strerror(err));
  23. return 1;
  24. }
  25. for(res = res0; res != NULL; res = res->ai_next){
  26. printf("%d\n", res->ai_family);
  27. sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  28. if(sock < 0){continue;}
  29. if(connect(sock, res->ai_addr, res->ai_addrlen) != 0){
  30. close(sock);
  31. continue;
  32. }
  33. break;
  34. }
  35. freeaddrinfo(res0);
  36. if(res == NULL){
  37. printf("failed\n");
  38. return 1;
  39. }
  40. snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
  41. int n = write(sock, buf, (int)strlen(buf));
  42. while(n > 0){
  43. n = read(sock, buf, sizeof(buf));
  44. write(fileno(stdout), buf, n);
  45. }
  46. close(sock);
  47. return 0;
  48. }

github源代码

发送端的执行方式:

  1. ./a.out www.baidu.com

http服务器端

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <errno.h>
  7. #include <netdb.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. int main(){
  11. int sock0;
  12. sockaddr_in client;
  13. socklen_t len;
  14. int sock;
  15. int yes = 1;
  16. addrinfo *res, hints;
  17. int err;
  18. char buf[2048];
  19. int n;
  20. char inbuf[2048];
  21. hints.ai_family = AF_INET;
  22. hints.ai_flags = AI_PASSIVE;
  23. hints.ai_socktype = SOCK_STREAM;
  24. err = getaddrinfo(NULL, "12345", &hints, &res);
  25. if(err != 0){
  26. printf("getaddrinfo %s\n", gai_strerror(err));
  27. return 1;
  28. }
  29. sock0 = socket(res->ai_family, res->ai_socktype, 0);
  30. setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(yes));
  31. bind(sock0, res->ai_addr, res->ai_addrlen);
  32. listen(sock0, 5);
  33. snprintf(buf, sizeof(buf),
  34. "HTTP/1.0 200 OK\r\n"
  35. "Content-Length: 20\r\n"
  36. "Content-Type:text/html\r\n"
  37. "\r\n"
  38. "HELLO\r\n");
  39. while(1){
  40. len = sizeof(client);
  41. sock = accept(sock0, (sockaddr*)&client, &len);
  42. n = read(sock, inbuf, sizeof(inbuf));
  43. write(fileno(stdout), inbuf, n);
  44. write(sock, buf, (int)strlen(buf));
  45. close(sock);
  46. }
  47. close(sock0);
  48. return 0;
  49. }

github源代码

测试方式:

  1. 在浏览器里输入:http://127.0.0.1:12345
  2. 或者输入:http://localhost:12345

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号