本文章用以记录自己在windows到linux移植过程中遇到的各种问题和各种坑。
linux头文件,作用有待继续学习= =
#ifndef ZRLINUX_H
#define ZRLINUX_H
// linux头文件
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <semaphore.h>
#include <stdarg.h>
#include <ifaddrs.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/timeb.h>
#include <sys/ioctl.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <arpa/inet.h>
// 自定义
#define EvoUDPOperatorLibAPI //extern "C"
// windows变量
#define PVOID void*
#define BOOL int
#define FALSE 0
#define TRUE 1
#define Sleep(time) usleep(time * 1000);
// socket差异
#define FAR
#define SOCKET int
#define socklen_t int
#define ssize_t int
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define closesocket close
#define ioctlsocket fcntl
// linux临界区
#define CRITICAL_SECTION pthread_mutex_t
#define InitializeCriticalSection(mutex) pthread_mutex_init(mutex,0)
#define DeleteCriticalSection(mutex) pthread_mutex_destroy(mutex)
#define EnterCriticalSection(mutex) pthread_mutex_lock(mutex)
#define LeaveCriticalSection(mutex) pthread_mutex_unlock(mutex)
// linux信号量
// linux线程
// linux错误号
extern int errno;
#endif
系统调用差异
socket网络差异
select函数
linux下的select函数需要设置第一位参数为set的大小;
ioctlsocket和fcntl
windows下:
u_long mode = 0;
ioctlsocket(s, FIONBIO, &mode);
控制为阻塞方式
u_long mode = 1;
ioctlsocket(s, FIONBIO, &mode);
控制为非阻塞方式
linux下:
int flags = fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, flags | O_NONBLOCK);
将非阻塞的设置回阻塞可以用
int flags = fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, flags & ~O_NONBLOCK);