windows 如何使用win32编写socket通讯程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/558028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write socket communication program using win32
提问by
New guy want to learn about socket programming in win32. I know bit of MFC but thats not good point to start as it just hides the internal details form programmer.
新人想学习win32下的socket编程。我知道一点 MFC 但这不是开始的好点,因为它只是隐藏了程序员的内部细节。
回答by Philibert Perusse
Use winsock functions (Winsock2.h).
使用 winsock 函数 (Winsock2.h)。
回答by Jonathan Adelson
Beej's guide is a pretty common starting point. It has been a few years since I started with this stuff, but iirc, the guide uses Berkeley sockets and points out the differences with winsock where appropriate.
Beej 的指南是一个非常常见的起点。我开始使用这些东西已经有几年了,但是 iirc,该指南使用了 Berkeley 套接字并在适当的地方指出了与 winsock 的差异。
回答by NTDLS
I wholeheartedly agree with ChrisW and comend you for passing on MFC.
我全心全意地同意 ChrisW 并赞扬你传递 MFC。
See the MSDN Winsock Referencefor lots of great WinSock information.
请参阅MSDN Winsock 参考以获取大量重要的 WinSock 信息。
Also, while it may be too large to learn off of, I have what I consider to be a very nice Socket Server Class- let me know if you find any of it useful.
此外,虽然它可能太大而无法学习,但我认为它是一个非常好的套接字服务器类- 如果您发现其中任何有用,请告诉我。
回答by cwap
A small hint would be (assuming you're using Visual Studio and the Winsocket librabry) that you need to link WS2_32.Lib manually. I find that this small step is something a lot of starters miss, and get some weird errors (weird in the eye of the beholder) because of it. http://social.microsoft.com/Forums/en-US/vcgeneral/thread/cfefa4a5-1f1a-4987-8bc7-f3197cb5826c
一个小提示是(假设您使用的是 Visual Studio 和 Winsocket 库)您需要手动链接 WS2_32.Lib。我发现这个小步骤是很多初学者错过的,因此会出现一些奇怪的错误(在旁观者眼中很奇怪)。 http://social.microsoft.com/Forums/en-US/vcgeneral/thread/cfefa4a5-1f1a-4987-8bc7-f3197cb5826c
Also; imho you shouldn't use MFC. There's plenty of great guides for winsocket programming on the google-net :)
还; 恕我直言,您不应该使用 MFC。google-net 上有很多关于 winsocket 编程的很棒的指南 :)
Best of luck
祝你好运
回答by jeffm
回答by jeffm
You are right that MFC will obscure some details, I have no idea why they decided to wrap it.
你说得对,MFC 会掩盖一些细节,我不知道他们为什么决定包装它。
There are a number of good books and tutorials on WinSock programming. Here is one;
有许多关于 WinSock 编程的好书和教程。这是一个;
回答by ChrisW
The answer is to use the win32 socket API.
答案是使用 win32 套接字 API。
There's information about this API in MSDN.
MSDN 中有关于此 API 的信息。
It (the so-called 'Winsock' API) is similar to (based on) the Unix/Posix API, so as well as MSDN a lot of the literature (books) about using sockets on Unix are relevent too.
它(所谓的“Winsock”API)类似于(基于)Unix/Posix API,因此与 MSDN 一样,很多关于在 Unix 上使用套接字的文献(书籍)也是相关的。
Googling will find you code samples, presumably.
谷歌搜索可能会找到您的代码示例。
You'll want to know a little about networks: what an "IP address" and a "port" are, for example.
您需要对网络有所了解:例如,“IP 地址”和“端口”是什么。
You'll want to choose a protocol (UDP or TCP).
您需要选择一种协议(UDP 或 TCP)。
One of the things that's different about Win32 is that you can use ReadFile and WriteFile when you have a connected socket, which means you can use the options for overlapped I/O that are associated with ReadFile and WriteFile.
Win32 的不同之处之一是,当您有连接的套接字时,您可以使用 ReadFile 和 WriteFile,这意味着您可以使用与 ReadFile 和 WriteFile 关联的重叠 I/O 选项。
回答by Sesh
Two options:
两种选择:
- write a file stream based code that talks to the serial port.
- use a C++ library for Serial Port communication (google for this)
- 编写一个基于文件流的代码,与串行端口对话。
- 使用 C++ 库进行串口通信(谷歌为此)
The first option means writing 'boiler plate' code so you would be better off using a 3rd party library for that.
第一个选项意味着编写“样板”代码,因此您最好为此使用 3rd 方库。
The most common 'starting trouble' with serial port programming is to establish a communication link with the device. You NEED to have the correct settings for the port name, baud rate, parity etc (usually given by the manufacturer of device).
串行端口编程最常见的“启动问题”是与设备建立通信链接。您需要正确设置端口名称、波特率、奇偶校验等(通常由设备制造商提供)。
Then there will also be a 'command set' for the device. For example if you send '?' to the device, it will return with device information like manufacturer name etc (the '?' command is an example, its not a standard - command set varies from device to device).
然后还有一个设备的“命令集”。例如,如果您发送“?” 到设备,它将返回设备信息,如制造商名称等('?' 命令是一个例子,它不是标准 - 命令集因设备而异)。
All serial communications work like this: - you write a command to the serial port - poll the read buffer for response from the device
所有串行通信的工作方式如下: - 向串行端口写入命令 - 轮询读取缓冲区以获取设备的响应
To my knowledge there are no built-in Serial port communication classes in C++ or MFC but there are a lot of 3rd party libs for this.
据我所知,C++ 或 MFC 中没有内置的串行端口通信类,但有很多 3rd 方库用于此。