求教跨平台的网络库开发的经验

BlackHamlet 2017-12-04 02:00:55
公司要做一个UDP的网络库,要windows/linux互跨的
网络接口的封装我做过一些,但是跨平台的还没有尝试过
不同的平台用的接口函数都不一样吧?
网络接口有C++的标准函数么?
我们不想维护两套源码,希望一套源码编译不同的平台文件
你们做过类似的么,有什么经验分享一下么
...全文
192 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-12-07
  • 打赏
  • 举报
回复
百度搜相关关键字。
BlackHamlet 2017-12-06
  • 打赏
  • 举报
回复
还有个很重要的问题,我已经没分了怎么办。。。。。 没法开新帖了
  • 打赏
  • 举报
回复
引用 7 楼 u011648839 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:]
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(2,2);
    if (0!=WSAStartup(wVersionRequested,&wsaData)) {
        printf("The winsock is not compatible!\n");
        return 2;
    }
#endif
//...
#ifdef WIN32
    WSACleanup();
#endif
可不可以解释一下SOCKET在两个平台上运行的区别,为什么winodws专门用一个WSAStartup来初始化,但是linux不用?[/quote] windows下的API都是自己搞的一套,叫windows socket,估计微软又基于自己的api再封装了一遍,实现了bsd socket 所以我认为常见的bsd socket接口函数windows下虽然也都有,但骨子里还是他自己那套基于事件和重叠IO的玩意
赵4老师 2017-12-06
  • 打赏
  • 举报
回复
引用 7 楼 u011648839 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:]
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(2,2);
    if (0!=WSAStartup(wVersionRequested,&wsaData)) {
        printf("The winsock is not compatible!\n");
        return 2;
    }
#endif
//...
#ifdef WIN32
    WSACleanup();
#endif
可不可以解释一下SOCKET在两个平台上运行的区别,为什么winodws专门用一个WSAStartup来初始化,但是linux不用?[/quote] WSAStartup The Windows Sockets WSAStartup function initiates use of WS2_32.DLL by a process. int WSAStartup ( WORD wVersionRequested, LPWSADATA lpWSAData ); Parameters wVersionRequested [in] The highest version of Windows Sockets support that the caller can use. The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number. lpWSAData [out] A pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation. Remarks The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and to retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after a successfully calling WSAStartup. In order to support future Windows Sockets implementations and applications that can have functionality differences from current version of Windows Sockets, a negotiation takes place in WSAStartup. The caller of WSAStartup and the WS2_32.DLL indicate to each other the highest version that they can support, and each confirms that the other's highest version is acceptable. Upon entry to WSAStartup, the WS2_32.DLL examines the version requested by the application. If this version is equal to or higher than the lowest version supported by the DLL, the call succeeds and the DLL returns in wHighVersion the highest version it supports and in wVersion the minimum of its high version and wVersionRequested. The WS2_32.DLL then assumes that the application will use wVersion. If the wVersion field of the WSADATA structure is unacceptable to the caller, it should call WSACleanup and either search for another WS2_32.DLL or fail to initialize. It is legal and possible for an application written to this version of the specification to successfully negotiate a higher version number than the version of this specification. In such a case, the application is only guaranteed access to higher-version functionality that fits within the syntax defined in this version, such as new Ioctl codes and new behavior of existing functions. New functions, for example, may be inaccessible. To be guaranteed full access to new syntax of a future version, the application must fully conform to that future version, such as compiling against a new header file, linking to a new library, or other special cases. This negotiation allows both a WS2_32.DLL and a Windows Sockets application to support a range of Windows Sockets versions. An application can use WS2_32.DLL if there is any overlap in the version ranges. The following chart gives examples of how WSAStartup works in conjunction with different application and WS2_32.DLL versions: App versions DLL Versions wVersion Requested wVersion wHigh Version End Result 1.1 1.1 1.1 1.1 1.1 use 1.1 1.0 1.1 1.0 1.1 1.0 1.0 use 1.0 1.0 1.0 1.1 1.0 1.0 1.1 use 1.0 1.1 1.0 1.1 1.1 1.1 1.1 use 1.1 1.1 1.0 1.1 1.0 1.0 Application fails 1.0 1.1 1.0 --- --- WSAVERNOT SUPPORTED 1.0 1.1 1.0 1.1 1.1 1.1 1.1 use 1.1 1.1 2.0 1.1 2.0 1.1 1.1 use 1.1 2.0 2.0 2.0 2.0 2.0 use 2.0 The following code fragment demonstrates how an application that supports only version 2.2 of Windows Sockets makes a WSAStartup call: WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } /* The WinSock DLL is acceptable. Proceed. */ Once an application or DLL has made a successful WSAStartup call, it can proceed to make other Windows Sockets calls as needed. When it has finished using the services of the WS2_32.DLL, the application or DLL must call WSACleanup in order to allow the WS2_32.DLL to free any resources for the application. Details of the actual Windows Sockets implementation are described in the WSADATA structure. An application or DLL can call WSAStartup more than once if it needs to obtain the WSAData structure information more than once. On each such call the application can specify any version number supported by the DLL. An application must call one WSACleanup call for every successful WSAStartup call to allow third-party DLLs to make use of a WS2_32.DLL on behalf of an application. This means, for example, that if an application calls WSAStartup three times, it must call WSACleanup three times. The first two calls to WSACleanup do nothing except decrement an internal counter; the final WSACleanup call for the task does all necessary resource deallocation for the task. Return Values The WSAStartup function returns zero if successful. Otherwise, it returns one of the error codes listed below. An application cannot call WSAGetLastError to determine the error code as is normally done in Windows Sockets is WSAStartup fails. The WS2_32.DLL will not have been loaded in the case of a failure so the client data area where the "last error" information is stored could not be established. Error Codes WSASYSNOTREADY Indicates that the underlying network subsystem is not ready for network communication. WSAVERNOTSUPPORTED The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation. WSAEINPROGRESS A blocking Windows Sockets 1.1 operation is in progress. WSAEPROCLIM Limit on the number of tasks supported by the Windows Sockets implementation has been reached. WSAEFAULT The lpWSAData is not a valid pointer. QuickInfo Windows NT: Yes Windows: Yes Windows CE: Use version 1.0 and later. Header: Declared in winsock2.h. Import Library: Link with ws2_32.lib. See Also send, sendto, WSACleanup
BlackHamlet 2017-12-05
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(2,2);
    if (0!=WSAStartup(wVersionRequested,&wsaData)) {
        printf("The winsock is not compatible!\n");
        return 2;
    }
#endif
//...
#ifdef WIN32
    WSACleanup();
#endif
可不可以解释一下SOCKET在两个平台上运行的区别,为什么winodws专门用一个WSAStartup来初始化,但是linux不用?
BlackHamlet 2017-12-05
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(2,2);
    if (0!=WSAStartup(wVersionRequested,&wsaData)) {
        printf("The winsock is not compatible!\n");
        return 2;
    }
#endif
//...
#ifdef WIN32
    WSACleanup();
#endif
666.我今天研究一天了,你这个算是我今天研究的一个很好的总结
ztenv 版主 2017-12-05
  • 打赏
  • 举报
回复
用现成的第三方库吧,不要从头开始,累
赵4老师 2017-12-05
  • 打赏
  • 举报
回复
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(2,2);
    if (0!=WSAStartup(wVersionRequested,&wsaData)) {
        printf("The winsock is not compatible!\n");
        return 2;
    }
#endif
//...
#ifdef WIN32
    WSACleanup();
#endif
BlackHamlet 2017-12-05
  • 打赏
  • 举报
回复
引用 2 楼 paschen 的回复:
http://blog.csdn.net/lianshaohua/article/details/73238403
这些是别人的库嘛,我们是想自己从0开发一个 那我问点具体的问题, 比如使用socket函数,受到平台的限制吗?
FoolCarpe 2017-12-04
  • 打赏
  • 举报
回复
试试这个
boost::asio

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧