开发者

Socket Programming Under Visual Studio 2010, Non-Managed C++ [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Socket Programming for Windows C/C++

I am trying to understand what my options are for doing socket programming in C++ (non-managed) under Visu开发者_如何学编程al Studio 2010. Please note that one constraint I have is that I cannot use a third-party library. I must use only the capabilities provided directly by Visual Studio.

It seems there are two options:

  1. Use the Win32 (Winsock) API. This API is Unix-like.
  2. Use MFC.

Are these indeed the only two options? Or is there a newer, more "modern" way of doing socket programming?

Regards, Dave


The newer more "modern" way is to use a 3rd party library. Preferably a cross-platform library like boost::asio.

However, if you absolutely cannot use a 3rd party library, you best bet is to learn the Win32 socket API.

boost::asio on Windows provides its implementation in terms of the asynchronous Win32 socket API. Some functions to get you started, WSASend() and WSARecv().


The only way of doing it in Windows is using the Win32 Socket API (which is indeed POSIX compatible, for better or for worse). Both MFC and the .NET implementations are just wrappers around it.

If you can go higher level than just a socket though, say to the TCP or even HTTP (or FTP) levels, there are alternatives like the Internet class of Win32 functions.


is there a newer, more "modern" way of doing socket programming?

Yes, use Boost.Asio. To quote the documentation:

Most programs interact with the outside world in some way, whether it be via a file, a network, a serial cable, or the console. Sometimes, as is the case with networking, individual I/O operations can take a long time to complete. This poses particular challenges to application development.

Boost.Asio provides the tools to manage these long running operations, without requiring programs to use concurrency models based on threads and explicit locking.

The Boost.Asio library is intended for programmers using C++ for systems programming, where access to operating system functionality such as networking is often required


I'd stay away from MFC, personally. If you're constrained to develop your own code from the ground up, then you should use overlapped sockets with an IO completion port.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜