├── res └── img │ ├── 1.png │ ├── 3.png │ └── 4.png ├── Tutorial ├── TCPIP网络编程基础教程(Chap1).pdf ├── TCPIP网络编程基础教程(Chap2).pdf └── TCPIP网络编程基础教程(Chap3).pdf ├── .gitignore ├── LICENSE ├── stdafx.h ├── Client └── time_Client.cpp ├── Server └── time_Server.cpp └── README.md /res/img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/res/img/1.png -------------------------------------------------------------------------------- /res/img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/res/img/3.png -------------------------------------------------------------------------------- /res/img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/res/img/4.png -------------------------------------------------------------------------------- /Tutorial/TCPIP网络编程基础教程(Chap1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/Tutorial/TCPIP网络编程基础教程(Chap1).pdf -------------------------------------------------------------------------------- /Tutorial/TCPIP网络编程基础教程(Chap2).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/Tutorial/TCPIP网络编程基础教程(Chap2).pdf -------------------------------------------------------------------------------- /Tutorial/TCPIP网络编程基础教程(Chap3).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhouie/study-Socket_Programming/HEAD/Tutorial/TCPIP网络编程基础教程(Chap3).pdf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 zhouie 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #if !defined(AFX_STDAFX_H__4607A810_33E2_483D_80D8_BE41F0D473D5__INCLUDED_) 7 | #define AFX_STDAFX_H__4607A810_33E2_483D_80D8_BE41F0D473D5__INCLUDED_ 8 | 9 | #if _MSC_VER > 1000 10 | #pragma once 11 | #endif // _MSC_VER > 1000 12 | 13 | 14 | // Insert your headers here 15 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 16 | #define DLLEXPORT __declspec(dllexport) 17 | #define DLLIMPORT __declspec(dllimport) 18 | 19 | #include "stdio.h" 20 | #include 21 | #include 22 | #include 23 | 24 | #pragma comment(lib,"ws2_32.lib") 25 | 26 | // TODO: reference additional headers your program requires here 27 | void Msg(char *szFormat, ...); 28 | void dbMsg(char *szFormat, ...); 29 | //{{AFX_INSERT_LOCATION}} 30 | // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 31 | 32 | #endif // !defined(AFX_STDAFX_H__4607A810_33E2_483D_80D8_BE41F0D473D5__INCLUDED_) -------------------------------------------------------------------------------- /Client/time_Client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #pragma comment(lib, "ws2_32.lib") 7 | 8 | #define MAXSIZE 255 9 | using namespace std; 10 | int main() 11 | { 12 | WORD sockVersion = MAKEWORD(2, 2); 13 | WSADATA wcdata; 14 | if(WSAStartup(sockVersion, &wcdata)!=0) 15 | { 16 | cout<<"Couldn't find a useable winsock.dll"<>data; 38 | const char * sendData; 39 | sendData = data.c_str(); //string -> const char* 40 | send(sclient, sendData, strlen(sendData), 0); 41 | 42 | char recData[MAXSIZE]; 43 | int ret = recv(sclient, recData, MAXSIZE, 0); 44 | if(ret<0) 45 | { 46 | cout<<"Recieve data failed!"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #pragma comment(lib,"ws2_32.lib") 9 | 10 | #define MAXSIZE 255 11 | 12 | using namespace std; 13 | int main(int argc, char* argv[]) 14 | { 15 | //初始化WSA 16 | WORD sockVersion = MAKEWORD(2,2); 17 | WSADATA wsaData; 18 | if(WSAStartup(sockVersion, &wsaData)!=0) 19 | { 20 | cout<<"Couldn't find a useable winsock.dll"< 1000 41 | #pragma once 42 | #endif // _MSC_VER > 1000 43 | 44 | // Insert your headers here 45 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 46 | #define DLLEXPORT __declspec(dllexport) 47 | #define DLLIMPORT __declspec(dllimport) 48 | 49 | #include "stdio.h" 50 | #include 51 | #include 52 | #include 53 | 54 | #pragma comment(lib,"ws2_32.lib") 55 | 56 | // TODO: reference additional headers your program requires here 57 | void Msg(char *szFormat, ...); 58 | void dbMsg(char *szFormat, ...); 59 | //{{AFX_INSERT_LOCATION}} 60 | // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 61 | 62 | #endif // !defined(AFX_STDAFX_H__4607A810_33E2_483D_80D8_BE41F0D473D5__INCLUDED_) 63 | ``` 64 | 65 | --- 66 | 67 | ### 报错 undefined reference to 'WSAStartup@8' 68 | 69 | Codeblocks MinGW 提供了一套简单方便的Windows下的基于GCC 程序开发环境。 70 | 71 | MinGW是完全免费的自由软件,收集了一系列免费的Windows 使用的头文件和库文件; 72 | 73 | 同时整合了GNU ( http://www.gnu.org/ )的工具集,特别是GNU 程序开发工具,如经典gcc, g++, make等。 74 | 75 | 它在Windows平台上模拟了Linux下GCC的开发环境,为C++的跨平台开发提供了良好基础支持,为了在Windows下工作的程序员熟悉Linux下的C++工程组织提供了条件。 76 | 77 | Windows 下C++ Socket编程,可能会调用`WSAStartup`函数报错:`undefined reference to 'WSAStartup@8'` 78 | 79 | 这是因为 **缺少socket的链接参数**,解决方案便是把 **-lwsock32** 加到链接器中,路径:`Setting -> Compiler setting -> Global compiler setting -> Linker setting -> Other linker options`。 80 | 81 | ![](http://pco46wcft.bkt.clouddn.com/zhouie/study-Socket_Programming/1.png) 82 | 83 | --- 84 | 85 | ### CodeBlocks中文乱码问题 86 | 87 | 在了解怎么解决之前,先要去搞清楚其中三个涉及到编码问题的地方 88 | 89 | **1、CodeBlocks编辑器保存源文件用的编码** 90 | 91 | 默认情况下,CodeBlocks是保存为windows本地编码格式的,也就是`WINDOWS-936(GBK)`编码。 92 | 93 | 但不幸的是,GCC编译器默认编译的时候是按照`UTF-8`解析的。你存成`GBK`,但是当成`UTF-8`解析,这怎么会编译通过呢,所以如果这两个地方编码不统一好,编译的时候就会报错:`error: converting to execution character set: Illegal byte sequence`! 94 | 95 | 其实要真正解决这个问题也很简单,编写CodeBlocks的人只需要在调用编译器之前检测一下源文件是什么编码,然后就自动让编译器用什么编码进行解析,问题就解决了。只是很可惜,他们还没有这么做,所以在易用性便不如微软了,免费和商业的东西还是有一定差距的。 96 | 97 | **2、GCC编译器编译的时候对输入的源文件解释用的编码** 98 | 99 | 这个在编译器中可以通过设置`-finput-charset=charset`来指定编译器用什么编码解释输入源文件。如果源文件的字符集是`GBk`,那么就必须指定`-finput-charset=GBK`,如果不指定,一律默认当做`UTF-8`处理。这个时候,除非你源文件真的是`UTF-8`,否则就会出现转换错误。 100 | 101 | **3、编译好的执行文件所用编码** 102 | 103 | 如果你 **1** 和 **2** 两个地方的编码都能统一,那么编译时也就不会报错了。But,这个时候运行一下看看,如果在控制台显示的依然是乱码,那么就是一步的问题的了。 104 | 105 | 那是因为啊,控制台显示的时候缺省的是使用系统默认的字符集,比如windows下用的是`GBk`,而在默认情况下,编译之后的执行文件时编译成`UTF-8`的,这又出现了不统一,乱码由此而生! 106 | 107 | 解决的方法也很简单,就是给编译器加上选项:`-fexec-charset=GBK`,和windows默认的统一就OK了。 108 | 109 | **综合以上**,搞懂了乱码产生的原因,那么也就不难得出结论,如何修改,你想修改成什么都OK,关键是要统一。 110 | 111 | --- 112 | 113 | #### 修改操作 114 | 115 | * 修改源文件保存编码格式 116 | - 路径:`settings->Editor->gernal settings->encoding setting` 117 | ![](http://pco46wcft.bkt.clouddn.com/zhouie/study-Socket_Programming/3.png) 118 | 119 | * 修改编译器对源文件解释编码格式和生成执行文件执行时候采用的编码格式 120 | - 路径:`settings->compiler and debugger settings->global compiler setting->Other compiler options` 121 | ``` 122 | -finput-charset=UTF-8 123 | -fexec-charset=GBK 124 | ``` 125 | ![](http://pco46wcft.bkt.clouddn.com/zhouie/study-Socket_Programming/4.png) 126 | 127 | --- 128 | 129 | ## Reference 130 | 131 | [TCP IP网络编程基础教程(Chap1-3)](https://github.com/zhouie/study-Socket_Programming/tree/master/Tutorial) 132 | 133 | [C++中Socket编程(入门)](https://www.cnblogs.com/kefeiGame/p/7246942.html) 134 | 135 | [Windows环境下用C++实现Socket编程](https://blog.csdn.net/xiaoquantouer/article/details/58001960) 136 | 137 | 138 | 139 | --------------------------------------------------------------------------------