使用Code::blocks在windows下写网络程序

使用Code::blockswindows下写网络程序


作者


He YiJun – storysnail<at>gmail.com


团队


ls


版权


转载请保留本声明!

本文档包含的原创代码根据General Public License,v3 发布
GPLv3 许可证的副本可以在这里获得:http://www.gnu.org/licenses/gpl.html

本文档根据GNU
Free Documentation License 1.3发布
GFDL1.3许可证的副本可以在这里获得:http://www.gnu.org/licenses/gfdl.html

文中所引用的软件版权详见各软件版权具体声明,文中所提及的所有商标均为各自商标所有人的财产。
作者在此向所有提供过帮助和支持的朋友表示感谢,此致!


更新


2014-11-10


修改版权


略...


...

前言:

这是一个用来读取指定网页内容的程序。当前还非常原始,但已经能完成基本的功能。未来我会在这个程序的基础上不断扩充,让这个程序变成一个可用的更新检测程序!

一:windows下用Code::blocks开发网络程序

1:
Code::blocks 中新建一个工程
2:
建完工程后点击Project菜单,选择Build
options...
3:
选择Linker
settings标签页,在Other
linker options:中添加:

-lwsock32

二 源代码

  1. /***********************************************************************
  2. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  3. Eabc-version-verfy.c
  4. Develop Team : ls
  5. Team Leader : He YiJun (storysnail<at>gmail.com)
  6. Main Programmer : He YiJun
  7. Programmer : Ling Ying
  8. Program comments : Ling Ying
  9. Dict Editor : Yang QiuXi
  10. Documents : Ling Ying、 Yang QiuXi
  11. Art Designer : He YiJun
  12. License : GPLv3
  13. Last Update : 2013-02-25
  14. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  15. *************************************************************************/
  16. #include <windows.h> // 新增 windows.h
  17. #include <winsock2.h>
  18. //#pragma comment(lib, "ws2_32.lib") // For VS
  19. #include <tchar.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <malloc.h>
  23. #include <io.h>
  24. #ifdef _MT
  25. #include <process.h>
  26. #endif
  27. /* DWORD_PTR (pointer precision unsigned integer) is used for integers
  28. * that are converted to handles or pointers
  29. * This eliminates Win64 warnings regarding conversion between
  30. * 32 and 64-bit data, as HANDLEs and pointers are 64 bits in
  31. * Win64 (See Chapter 16). This is enable only if _Wp64 is defined.
  32. */
  33. #if !defined(_Wp64)
  34. #define DWORD_PTR DWORD
  35. #define LONG_PTR LONG
  36. #define INT_PTR INT
  37. #endif
  38. #define MAX_RQRS_LEN 0x1000 //4096
  39. /* Required for sockets */
  40. #define SERVER_PORT 80
  41. typedef struct {
  42. LONG32 rsLen;
  43. BYTE record [MAX_RQRS_LEN];
  44. } RESPONSE;
  45. typedef struct {
  46. LONG32 rqLen;
  47. BYTE record [MAX_RQRS_LEN];
  48. } REQUEST;
  49. #define RQ_SIZE sizeof (REQUEST)
  50. #define RQ_HEADER_LEN RQ_SIZE-MAX_RQRS_LEN
  51. #define RS_SIZE sizeof (RESPONSE)
  52. #define RS_HEADER_LEN RS_SIZE-MAX_RQRS_LEN
  53. static BOOL SendRequest (REQUEST *, SOCKET);
  54. static BOOL ReceiveResponse (RESPONSE *, SOCKET);
  55. static VOID PrintError (LPCTSTR , DWORD , BOOL);
  56. struct sockaddr_in clientSAddr;
  57. int _tmain (int argc, LPSTR argv[])
  58. {
  59. SOCKET clientSock = INVALID_SOCKET;
  60. REQUEST request;
  61. RESPONSE response;
  62. WSADATA WSStartData; /* Socket library data structure */
  63. DWORD conVal;
  64. while (1) {
  65. _tprintf (_T("%s"), _T("\nEnter Command: "));
  66. _fgetts ((char *)request.record, MAX_RQRS_LEN-1, stdin);
  67. /* Get rid of the new line at the end */
  68. /* Messages use 8-bit characters */
  69. request.record[strlen((char *)request.record)-1] = ‘\0‘;
  70. if (strcmp ((char *)request.record, "$Quit") == 0)
  71. break;
  72. if (strncmp ((char *)request.record, "GET",3) == 0)
  73. request.record[strlen((char *)request.record)] = ‘\n‘;
  74. /* Initialize the WS library. Ver 2.2 */
  75. if (WSAStartup (MAKEWORD (2, 2), &WSStartData) != 0)
  76. PrintError (_T("Cannot support sockets"), 1, TRUE);
  77. /* Connect to the server */
  78. /* Follow the standard client socket/connect sequence */
  79. clientSock = socket(AF_INET, SOCK_STREAM, 0);
  80. if (clientSock == INVALID_SOCKET)
  81. PrintError (_T("Failed client socket() call"), 2, TRUE);
  82. memset (&clientSAddr, 0, sizeof(clientSAddr));
  83. clientSAddr.sin_family = AF_INET;
  84. //clientSAddr.sin_addr.s_addr = htonl(inet_addr ("121.127.248.96"));
  85. clientSAddr.sin_addr.s_addr = inet_addr ("121.127.248.96");
  86. clientSAddr.sin_port = htons(SERVER_PORT);
  87. conVal = connect (clientSock, (struct sockaddr *)&clientSAddr, sizeof(clientSAddr));
  88. if (conVal == SOCKET_ERROR) PrintError (_T("Failed client connect() call)"), 3, TRUE);
  89. SendRequest (&request, clientSock);
  90. ReceiveResponse (&response, clientSock);
  91. shutdown (clientSock, SD_BOTH); /* Disallow sends and receives */
  92. closesocket (clientSock);
  93. WSACleanup();
  94. close (clientSock);
  95. }
  96. _tprintf (_T("\n****Leaving client\n"));
  97. return 0;
  98. }
  99. // GET http://www.7fane.com/test.html
  100. BOOL SendRequest (REQUEST *pRequest, SOCKET sd)
  101. {
  102. /* Send the the request to the server on socket sd */
  103. BOOL disconnect = FALSE;
  104. LONG32 nRemainSend, nXfer;
  105. LPBYTE pBuffer;
  106. //char target[]="GET http://www.7fane.com/test.html\n";
  107. pRequest->rqLen = (DWORD)(strlen ((char *)pRequest->record) + 1);
  108. nRemainSend = pRequest->rqLen;
  109. pBuffer = (LPBYTE)pRequest->record;
  110. _tprintf (_T("%s%s"), _T("\nNow SendRequestMessage: "),pBuffer);
  111. while (nRemainSend > 0 && !disconnect) {
  112. nXfer = send (sd, (char *)pBuffer, nRemainSend, 0);
  113. //nXfer = send (sd, target, strlen(target), 0);
  114. if (nXfer == SOCKET_ERROR) PrintError (_T("client send() failed"), 5, TRUE);
  115. disconnect = (nXfer == 0);
  116. nRemainSend -=nXfer;
  117. pBuffer += nXfer;
  118. _tprintf (_T("%s%d"), _T("\nSend btyes: "),nXfer);
  119. //_tprintf (_T("%s%s"), _T("\nSend content: "),target);
  120. _tprintf (_T("%s%s"), _T("\nSend content: "),pRequest->record);
  121. }
  122. return disconnect;
  123. }
  124. BOOL ReceiveResponse (RESPONSE *pResponse, SOCKET sd)
  125. {
  126. BOOL disconnect = FALSE;
  127. LONG32 nRemainRecv, nXfer;
  128. LPBYTE pBuffer;
  129. _tprintf (_T("%s"), _T("\nNow ReceiveResponseMessage! "));
  130. while(!disconnect) {
  131. /* Read each response and send it to std out.*/
  132. memset (pResponse->record, 0, MAX_RQRS_LEN);
  133. nRemainRecv = MAX_RQRS_LEN;
  134. pBuffer = (LPBYTE)pResponse->record;
  135. while (nRemainRecv > 0 && !disconnect) {
  136. nXfer = recv (sd, (char *)pBuffer, nRemainRecv, 0);
  137. if (nXfer == SOCKET_ERROR) PrintError (_T("client response recv() failed"), 7, TRUE);
  138. disconnect = (nXfer == 0);
  139. nRemainRecv -=nXfer;
  140. pBuffer += nXfer;
  141. if(!disconnect) {
  142. _tprintf (_T("%s[%d]"), _T("\nReceive bytes: "),nXfer);
  143. _tprintf (_T("%s\n%s"), _T("\nReceive content: "),pResponse->record);
  144. }
  145. }
  146. }
  147. return disconnect;
  148. }
  149. VOID PrintError (LPCTSTR userMessage, DWORD exitCode, BOOL printErrorMessage)
  150. {
  151. DWORD eMsgLen, errNum = GetLastError ();
  152. LPTSTR lpvSysMsg;
  153. _ftprintf (stderr, _T("%s\n"), userMessage);
  154. if (printErrorMessage) {
  155. eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  156. NULL, errNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  157. (LPTSTR) &lpvSysMsg, 0, NULL);
  158. if (eMsgLen > 0)
  159. {
  160. _ftprintf (stderr, _T("%s\n"), lpvSysMsg);
  161. }
  162. else
  163. {
  164. _ftprintf (stderr, _T("Last Error Number; %d.\n"), (int)errNum);
  165. }
  166. if (lpvSysMsg != NULL) LocalFree (lpvSysMsg); /* Explained in Chapter 5. */
  167. }
  168. if (exitCode > 0)
  169. ExitProcess (exitCode);
  170. return;
  171. }

三 运行截图

程序开始运行

输入命令和网址

注意下面截图的网址是我和泠在很久以前建的网站地址,目前已经失效了,所以你应该用一个有效的网址替换!

程序得到的网页内容

退出程序

时间: 2024-08-09 12:45:02

使用Code::blocks在windows下写网络程序的相关文章

[转]CentOS虚拟机如何设置共享文件夹,并在Windows下映射网络驱动器?

CentOS虚拟机如何设置共享文件夹,并在Windows下映射网络驱动器? 转自这里 一.为什么要这么做? 最近在做Linux下的软件开发,但又想使用Windows下的编程工具“Source Insight”. 二.安装环境 本机系统:Windows 7 旗舰版 VMware:VMware Workstation 9.0 CentOS:CentOS 6.4 网络适配器设置为:NAT 转换 三.查看是否已经安装“samba” 命令:rpm -qa|grep samba 输出结果: samba-cl

CentOS虚拟机如何设置共享文件夹,并在Windows下映射网络驱动器

一.为什么要这么做? 最近在做Linux下的软件开发,但又想使用Windows下的编程工具“Source Insight”. 二.安装环境 本机系统:Windows 7 旗舰版 VMware:VMware Workstation 9.0 CentOS:CentOS 6.4 网络适配器设置为:NAT 转换 三.查看是否已经安装“samba” 命令:rpm -qa|grep samba 输出结果: samba-client-3.0.33-3.28-1.el5samba-common-3.0.33-3

在windows下编写R程序包

每个R的使用者总会遇到这样的问题,用R时间长了攒了几个自己写的函数需要经常调用,用source命令调用感觉不够方便,最好怎么办呢?编一个自己的包,只需安装一次就可多次调用,方便自己和他人使用.我把自己制作R包的步骤记了下来,主要参考了统计之都的一篇帖子(http://cos.name/cn/topic/15991)和<Creating R Packages: A Tutorial>中的部分内容,对一些过时的东西做了更正,以供大家参考吧. 在windows下编写R程序包

windows下python脚本程序的运行

c:\python33\python.exe c:\python33\trycoding.py windows下python脚本程序的运行,布布扣,bubuko.com

gcc和MinGW的异同(在cygwin/gcc做的东西可以无缝的用在linux下,没有任何问题,是在windows下开发linux程序的一个很好的选择)

cygwin/gcc和MinGW都是gcc在windows下的编译环境,但是它们有什么区别,在实际工作中如何选择这两种编译器. cygwin/gcc完全可以和在linux下的gcc化做等号,这个可以从boost库的划分中可以看出来端倪,cygwin下的gcc和linux下的gcc完全使用的是相同的Toolsets.所以完全可以和linux一起同步更新gcc版本,而不用担心问题,并且在cygwin/gcc做的东西(不用win32的)可以无缝的用在linux下,没有任何问题.是在windows下开发

Windows下,通过程序设置全屏抗锯齿(多重采样)的方法

这里说的全屏抗锯齿,不是基于着色器的FXAA之类的方式,而是兼容性更好的,基于固定管线的多重采样方式. 先来说一下开发环境,我用的是VC2013+GLEW1.11. 要通过程序设置多重采样,首先需要进入控制面板,确定抗锯齿的设置是“根据应用程序而定” ,当然,这个设置的具体名称,不同的显卡厂商有不同的叫法. 接下来,就是重点了. Windows下要通过程序设置多重采样,必须使用wglChoosePixelFormatARB这个函数.目前网上基本都是自己去获取这个函数的接口地址,其实根本不用这么麻

一个复杂的Windows下的socket程序

上节演示了 Linux 下的 socket 程序,这节来看一下 Windows 下的 socket 程序.异样,server.cpp 为效劳器端代码,client 为客户端代码.效劳器端代码 server.cpp: #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll int main(){ //初始化 DLL WSADATA ws

【Code::Blocks】windows 环境下编译 Code::Blocks(已修正)

Code::Blocks 在2012-11-25发布了最新的12.11版本,相比上一个版本(10.05),Code::Blocks 进行了许多改进和更新(Change log). 引用 Wikipedia:Code::Blocks: Code::Blocks是一个免费.开源.跨平台的IDE,使用C++开发,并且使用wxWidgets做为GUI函式库.Code::Blocks使用了插件架构,其功能可以使用插件自由地扩充.目前, Code::Blocks主要针对开发C/C++程式而设计. Code:

使用code::blocks编译windows的dll链接库

因为机子上没有安装Visual Studio,所以找到了一种通过code::blocks编译dll的方式,踩到的坑是code::blocks默认的compiler是32位的,这样编译出的dll也是32位的,编译64位的需要借助MinGW-w64的toolchain. 为code::blocks配置外部MinGW-w64编译器可以参考Compile 64-bit under windows with MinGW-w64. 使用code::blocks创建一个dll的工程,如下图: go和next到