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

时间:2021-12-28 18:22:24

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

 

 

作者

He YiJun – storysnail<at>gmail.com

团队

ls

版权

转载请保留本声明!

 

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

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

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

更新

2014-11-10 

修改版权,增加linux版示例程序

...

...

 

 

 

 

前言:

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

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

1:
Code::blocks 中新建一个工程
2:
建完工程后点击Project菜单,选择Buildoptions...
3:
选择Linkersettings标签页,在Otherlinker options:中添加:

-lwsock32

 

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

 

 

 

 

二 源代码

  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下写网络程序

 

输入命令和网址

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

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

 

程序得到的网页内容

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

 

退出程序

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

下面是该程序的linux版本,这段程序是《使用C4droidbotbrewandriod手机上编程 》这篇文章的两个示例程序之一,不过《使用C4droidbotbrewandriod手机上编程 》这篇文章现在已经放弃维护了!

 1 /********************************************************************************
2 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
3 get-www
4 main.c
5
6 Develop Team : ls
7 Main Programmer : He YiJun (storysnail<at>gmail.com)
8 License : GPLv3
9 Last Update : 2013-03-03
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
11 *********************************************************************************/
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19
20 static int gw_connect(char *domain,int port)
21 {
22 int sock_sd;
23 int i;
24 struct hostent *site_dns;
25 struct sockaddr_in s_addr;
26 site_dns = gethostbyname(domain);
27 if(site_dns == NULL) {
28 printf("gethostbyname error!\n");
29 return -2;
30 }
31 printf("default ip: %s\n",inet_ntoa(*((struct in_addr *)site_dns->h_addr)));
32 for(i=0; i< site_dns->h_length/sizeof(int); i++) {
33 printf("IP:%d:%s\n",i+1,inet_ntoa(*((struct in_addr *)site_dns->h_addr_list[i])));
34 }
35 sock_sd = socket(AF_INET,SOCK_STREAM,0);
36 if(sock_sd < 0) {
37 printf ("socket error!");
38 return -1;
39 }
40 memset(&s_addr,0,sizeof(struct sockaddr_in));
41 memcpy(&s_addr.sin_addr,site_dns ->h_addr_list[0],site_dns->h_length);
42 s_addr.sin_family = AF_INET;
43 s_addr.sin_port = htons(port);
44 printf("s_addr ip: %s",inet_ntoa(*((struct in_addr *)&s_addr.sin_addr)));
45 return (connect(sock_sd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr)) < 0 ? -1 : sock_sd);
46 }
47
48 static int gw_send(int sock_sd,char *fmt,...)
49 {
50 char buf [1024];
51 va_list argptr;
52 va_start(argptr,fmt);
53 vsprintf(buf,fmt,argptr);
54 va_end(argptr);
55 printf("Send:\n%s\n",buf);
56 return send(sock_sd,buf,strlen(buf),0);
57 }
58
59 void main(int argc,char **argv)
60 {
61 int sock_sd;
62 char rBuf[3];
63 sock_sd = gw_connect("www.7fane.com",80);
64 if(sock_sd < 0) {
65 printf("connect error!\n");
66 return;
67 }
68 //注意:该网站只用于个人测试,在2013年11月末到期,
69 //如果你在之后的日期使用,请使用其它网页地址
70 gw_send(sock_sd,"GET http://www.7fane.com/test.html\n");
71 gw_send(sock_sd,"%c",10);
72 while(read(sock_sd,rBuf,1) > 0)
73 printf("%c",rBuf[0]);
74 close(sock_sd);
75 return;
76 }