使用Code::blocks在windows下写网络程序
作者 |
He YiJun – storysnail<at>hotmail.com |
||||
版权 |
转载请保留本声明! 本文档包含的原创代码根据General Public License,v3 发布 本文档根据GNU
Free Documentation License 1.3发布 文中所引用的软件版权详见各软件版权具体声明,文中所提及的所有商标均为各自商标所有人的财产。 |
||||
更新 |
|
前言:
这是一个用来读取指定网页内容的程序。当前还非常原始,但已经能完成基本的功能。未来我会在这个程序的基础上不断扩充,让这个程序变成一个可用的更新检测程序!
一:windows下用Code::blocks开发网络程序
1:
Code::blocks 中新建一个工程
2:
建完工程后点击Project菜单,选择Build
options...
3:
选择Linker
settings标签页,在Other
linker options:中添加:
-lwsock32
二 源代码
- /***********************************************************************
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
- Eabc-version-verfy.c
- Develop Team : ls
- Team Leader : He YiJun (storysnail<at>gmail.com)
- Main Programmer : He YiJun
- Programmer : Ling Ying
- Program comments : Ling Ying
- Dict Editor : Yang QiuXi
- Documents : Ling Ying、 Yang QiuXi
- Art Designer : He YiJun
- License : GPLv3
- Last Update : 2013-02-25
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
- *************************************************************************/
- #include <windows.h> // 新增 windows.h
- #include <winsock2.h>
- //#pragma comment(lib, "ws2_32.lib") // For VS
- #include <tchar.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <io.h>
- #ifdef _MT
- #include <process.h>
- #endif
- /* DWORD_PTR (pointer precision unsigned integer) is used for integers
- * that are converted to handles or pointers
- * This eliminates Win64 warnings regarding conversion between
- * 32 and 64-bit data, as HANDLEs and pointers are 64 bits in
- * Win64 (See Chapter 16). This is enable only if _Wp64 is defined.
- */
- #if !defined(_Wp64)
- #define DWORD_PTR DWORD
- #define LONG_PTR LONG
- #define INT_PTR INT
- #endif
- #define MAX_RQRS_LEN 0x1000 //4096
- /* Required for sockets */
- #define SERVER_PORT 80
- typedef struct {
- LONG32 rsLen;
- BYTE record [MAX_RQRS_LEN];
- } RESPONSE;
- typedef struct {
- LONG32 rqLen;
- BYTE record [MAX_RQRS_LEN];
- } REQUEST;
- #define RQ_SIZE sizeof (REQUEST)
- #define RQ_HEADER_LEN RQ_SIZE-MAX_RQRS_LEN
- #define RS_SIZE sizeof (RESPONSE)
- #define RS_HEADER_LEN RS_SIZE-MAX_RQRS_LEN
- static BOOL SendRequest (REQUEST *, SOCKET);
- static BOOL ReceiveResponse (RESPONSE *, SOCKET);
- static VOID PrintError (LPCTSTR , DWORD , BOOL);
- struct sockaddr_in clientSAddr;
- int _tmain (int argc, LPSTR argv[])
- {
- SOCKET clientSock = INVALID_SOCKET;
- REQUEST request;
- RESPONSE response;
- WSADATA WSStartData; /* Socket library data structure */
- DWORD conVal;
- while (1) {
- _tprintf (_T("%s"), _T("
Enter Command: "));
- _fgetts ((char *)request.record, MAX_RQRS_LEN-1, stdin);
- /* Get rid of the new line at the end */
- /* Messages use 8-bit characters */
- request.record[strlen((char *)request.record)-1] = '