简体   繁体   English

Visual C,WinSock HTTP Req和非Windows

[英]Visual C, WinSock HTTP Req, and Non-Windows

I just have a simple question. 我有一个简单的问题。 I am a student and I am learning. 我是学生,正在学习。 I am not very proficient at C++, but I finally set up a working http reqest app in c++ using winsock and I just wanted to know that after compiling will the http request still be sent from other OSes that don't have WinSock? 我不是很精通C ++,但是我最终使用winsock在c ++中建立了一个可运行的http reqest应用程序,我只是想知道在编译之后,http请求是否仍会从其他没有WinSock的操作系统发送?

WinSock, or at least the parts you probably will have been using for introductory networking stuff, is based off of, and largely compatible with, the BSD socket API, which is available on all current operating systems. WinSock或至少您可能会用于入门网络知识的部分基于BSD套接字API,并且在很大程度上与BSD套接字API兼容,该API在所有当前操作系统上均可用。 If you don't have experience with cross-platform development it's unlikely that your code would compile the first time through on a Linux system, but the underlying techniques would be the same. 如果您没有跨平台开发的经验,那么您的代码不太可能在Linux系统上首次编译,但是底层技术是相同的。

For cross-platform networking, you might want to consider something like Qt, which provides an API which will work mostly the same on all OSes without much per-platform stuff. 对于跨平台网络,您可能需要考虑Qt之类的东西,它提供了一个API,该API在所有OS上几乎都可以使用,而没有太多基于平台的内容。 Its networking API is also based off Berkeley sockets. 它的网络API也基于Berkeley套接字。

In general, though, there's just no way of checking that your code works cross-platform without testing it cross-platform. 但是,总的来说,如果不跨平台测试代码,就无法检查代码是否跨平台工作。 Grab a Linux distribution and try it out; 获取Linux发行版并试用; note that some are specifically intended to work from your Windows disk without the need to repartition. 请注意,某些磁盘专门用于在Windows磁盘上工作而无需重新分区。

If you use standardized BSD-based socket functions, then your code will typically work on multiple platforms socket() , bind() , connect() , send() , recv() , etc. Caveats include: 如果您使用标准化的基于BSD的套接字函数,则您的代码通常可以在多个平台上工作,例如socket()bind()connect()send()recv()等。注意事项包括:

  1. error codes. 错误代码。 When a WinSock/BSD function fails on Windows, the error code is retreived using WSAGetLastError() , whereas other platforms use errno instead. 当Windows上的WinSock / BSD函数失败时,使用WSAGetLastError()检索错误代码,而其他平台则使用errno So you will have to wrap that portion if you are trying to write cross-platform code. 因此,如果您要编写跨平台代码,则必须包装该部分。

  2. get/setsockopt() may implement different options on different platforms, not all options are standardized across all platforms. get/setsockopt()可能在不同平台上实现不同的选项,但并非所有选项都在所有平台上标准化。

If you use WinSock-specific functions, then your code will only work on Windows. 如果您使用特定于WinSock的功能,则您的代码将仅在Windows上有效。 WSASocket() , WSAConnect/Ex() , WSASend/Ex() , WSARecv/Ex() , etc. WSASocket()WSAConnect/Ex()WSASend/Ex()WSARecv/Ex()等。

All versions of Windows have WinSock. Windows的所有版本都有WinSock。 Most platforms, including Windows, support a BSD-based socket API. 包括Windows在内的大多数平台都支持基于BSD的套接字API。

After you compile your code, it will only run on the specific platform it was compiled for. 编译代码后,它将仅在为其编译的特定平台上运行。 You would have to compile the code separately for each platform you want to support. 您将必须针对要支持的每个平台分别编译代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM