⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commhead.h

📁 在linux下基于UDP通讯的程序,包括客户端与服务端.
💻 H
字号:
#ifndef _COMMON_H_
#define _COMMON_H_
#define __DEBUG__
#include <memory.h>
#include <stdlib.h>
#include <time.h>
#include "stdio.h"
////////////////
#include <fcntl.h>
#ifdef _WIN32
#pragma comment(lib, "Ws2_32.lib")
#include <Winsock.h>
#include <io.h>
const char * const LogFile = "C://cucme.log";
#define CLOSE_FILE(fd) _close(fd)
#endif

#ifdef __LINUX__
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
const char* const LogFile = "/cucme.log";
#endif

#ifdef _WIN32
//head file
#include <Winsock.h>
#include <Winbase.h>
#pragma comment(lib, "Ws2_32.lib")

//关闭套结字
#define CLOSE(s) closesocket(s);
#define CLEAN_SCREEEN system("cls");

static void SLEEP(int t1, int t2)
{
	Sleep(t1*1000 + t2);
}

//types
typedef CRITICAL_SECTION  LockType;
typedef int socklen_t;
#endif

#ifdef __LINUX__
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#define CLEAN_SCREEEN system("clear");
#define CLOSE(s) close(s)
static void SLEEP(int t1, int t2)
{
	struct timeval tm;
	tm.tv_sec = t1;
	tm.tv_usec = t2;
	select(0, NULL, NULL, NULL, &tm);
}
typedef pthread_mutex_t LockType;
#endif

//---------------------------global functions-------------------------
//初始化锁变量
static int InitLock(LockType * lock)
{
#ifdef _WIN32
	InitializeCriticalSection(lock);
#else
	if(pthread_mutex_init(lock, NULL) != 0)
	{
		return -1;
	}
#endif
	return 1;
}

//加锁操作
static int LockOn(LockType* lock)
{
#ifdef _WIN32
	EnterCriticalSection(lock);
#endif
#ifdef __LINUX__
	if(pthread_mutex_lock(lock) != 0)
	{
		return -1;
	}
#endif
	return 1;
}
//解锁操作
static int LockOff(LockType* lock)
{
#ifdef _WIN32
    LeaveCriticalSection(lock);
#endif
#ifdef __LINUX__
	if(pthread_mutex_unlock(lock) != 0)
	{
		return -1;
	}
#endif
	return 1;
}
//初始化网络
static int  InitNetWork()
{
#ifdef _WIN32
	WSAData w;
    if(WSAStartup(0x0101, &w) != 0)
	{
		return -1;
    }
#endif
	srand((unsigned)time(NULL));
	return 1;
}
//关闭网络连接
static int TerminateNetWork()
{
#ifdef _WIN32
	if(WSACleanup())
	{
		return -1;
	}
#endif
	return 1;
}

static LockType LockPrint;
static bool Initted = false;
static void ThreadSafePrint(const char * szMsg)
{
	if(!Initted)
	{
		InitLock(&LockPrint);
		Initted = true;
	}
	LockOn(&LockPrint);
#ifdef __DEBUG__
	printf(szMsg);
#endif
	LockOff(&LockPrint);
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -