跨平台Socket封装——跨平台头文件

头文件platform.h

#ifndef __PLATFORM_H__
#define __PLATFORM_H__

//windows
#if defined(WIN32) || defined(WIN64)
#define WINDOWS_FAMILY
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#endif

//c/c++
#include <stddef.h>
#include <errno.h>
#include <iostream>
#include <string>

#ifdef WINDOWS_FAMILY  //Windows api
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#ifdef WINDOWS_FAMILY
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <string.h>
#include <typeinfo.h>
#include <tchar.h>
#endif

#else                  //Linux api
#include <sys/unistd.h>
#include <sys/socket.h>
#include <sys/unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#endif

typedef int                net_ioctl_request_t;

#ifdef WINDOWS_FAMILY
#define NET_EWOULDBLOCK     WSAEWOULDBLOCK
#define NET_ETIMEDOUT       WSAETIMEDOUT
#define NET_INFINITE        INFINITE
#define NET_SOCKET_ERROR    SOCKET_ERROR
#define NET_NO_ERROR        NO_ERROR
#define NET_INVALID_SOCKET  INVALID_SOCKET

typedef __int64            int64;
typedef unsigned __int64   uint64;
typedef SOCKET             net_socket_fd;

#else

#define NET_EWOULDBLOCK     EWOULDBLOCK
#define NET_ETIMEDOUT       EWOULDBLOCK
//#define NET_ETIMEDOUT       ETIMEDOUT
#define NET_INFINITE        0xffffffff
#define NET_SOCKET_ERROR    (-1)
#define NET_NO_ERROR        0
#define NET_INVALID_SOCKET  (-1)

#define SD_SEND     SHUT_RD
#define SD_RECEIVE  SHUT_WR
#define SD_BOTH     SHUT_RDWR

typedef int                net_socket_fd;
typedef int                BOOL;
typedef signed long long   int64;
typedef unsigned long long uint64;

#endif

#endif

Linux下设置接收超时,超时后,返回的是EWOULDBLOCK。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值