首页 > Windows开发 > 详细

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

时间:2015-04-11 06:29:43      阅读:238      评论:0      收藏:0      [点我收藏+]

使用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. }


 

 

三 运行截图

程序开始运行

技术分享

 

输入命令和网址

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

技术分享

 

程序得到的网页内容

技术分享

 

退出程序

技术分享

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

原文:http://www.cnblogs.com/arely/p/4416115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!