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

📄 osdef.h

📁 sun Linux 下的网络编程
💻 H
字号:
  /*******************************************************************
  *
  *    DESCRIPTION:	定义windows 和unix/linux(POSIX)系统下互斥锁、多线程和socket的差别
  *					允许此net4cpp库在windows和unix/linux下运行
  *
  *    AUTHOR:yyc---yycmail@263.sina.com
  *					http://yycnet.yeah.net
  *
  *    HISTORY:
  *
  *    DATE:2002-8-23
  *_________________________________________________________________________________________________
  *		unix/linux下编译此库说明 :
  *		Sun os下编译时要指定-D__STL_PTHREADS宏,连接时指定-lnsl -lsocket -lpthread 库
  *     linux下编译时可以不指定-D__STL_PTHREADS宏,连接时要指定-lnsl -lpthread 库
  **********************************************************************************************************
  *		windows,vc中编译说明
  *		1、此库用到了winsocket ,因此在用此库前要调用WSAStartup()初始化winSocket库函数
  *		2、结束时要调用WSAClearup() winSocket库资源
  *		3、连接时指定libcmtd.lib或libcmt.lib库
  *		4、This program requires the multithreaded library. For example,
  * 			compile with the following command line:
  *     	CL /MT /D "_X86_" BEGTHRD.C
  *
  * 		If you are using the Visual C++ development environment, select the 
  * 		Multi-Threaded runtime library in the compiler Project Settings 
  * 		dialog box.
  *		
  *		The Multithread Libraries Compile Option
  *		To build a multithread application that uses the C run-time libraries, 
  *		you must tell the compiler to use a special version of the libraries (LIBCMT.LIB).
  *		To select these libraries, first open the Project Settings dialog box (Build menu) 
  *		and click the C/C++ tab. Select Code Generation from the Category drop-down list box. 
  *		From the Use Run-Time Library drop-down box, select Multithreaded. Click OK to return to editing.
  *
  *		From the command line, the Multithread Library compiler option (/MT) 
  *		is the best way to build a multithread program with LIBCMT.LIB. 
  *		This option, which is automatically set when you specify a multithreaded application 
  *		when creating a new project, embeds the LIBCMT library name in the object file.

  *******************************************************************/

#ifndef __OSDEF_H__
#define __OSDEF_H__

    //#define __WINDOWS_SYS__
	#ifdef __WINDOWS_SYS__   //windows system

		#define FD_SETSIZE 65535
	
		#include <windows.h>
		//win32互斥锁
		#define pthread_mutex_t CRITICAL_SECTION 
		#define pthread_mutex_lock(m) EnterCriticalSection(m) 
		#define pthread_mutex_unlock(m) LeaveCriticalSection(m) 
		#define pthread_mutex_init(m,p) InitializeCriticalSection(m)
		#define pthread_mutex_destroy(m) DeleteCriticalSection(m)
		//创建win32线程
		extern "C"
			{
				//多线程
				#include <process.h>
			}
		#define pthread_t unsigned long  	
		#define pthread_M_CREATE(pReti,pThrid,pFunc,pArgs) \
				{ \
				*pThrid=_beginthread((void (*)(void *))pFunc,0, pArgs); \
				*pReti=0; \
				}
		#define pthread_M_EXIT(pRet) \
				{ \
				_endthread(); \
				}
		#define pthread_M_JOIN(pid,ptr) \
				{ \
				int res=WaitForSingleObject((HANDLE)pid, INFINITE); \
				}
		
		#define pthread_M_KILL(pid,signo) \
				{ \
				}
		#define pthread_M_DETACH(pid) \
				{ \
				}
					
		#ifdef __CSOCKET_H__
			//socket errno
			#define        ENOTSOCK        WSAEOPNOTSUPP
			#define        ECONNRESET      WSAECONNRESET
			#define        ENOTCONN        WSAENOTCONN
			#define        EBADF           WSAENOTSOCK
			#define        EPIPE            WSAESHUTDOWN
			#define		   MSG_NOSIGNAL    0  //windows下没有此定义
			#define        SOCK_M_GETERROR WSAGetLastError() //get wrong code
			//#define		   select(nfds,rfd,wfd,efd,t) 1
			#define socklen_t int
			#define vsnprintf _vsnprintf
			//-----------------
			#pragma comment(lib,"ws2_32")  //很重要,如果没有这一句则连接不能通过
										   //告诉程序搜索ws2_32.dll动态库,socket库
		#endif //?#ifdef __CSOCKET_H__
		   
	#else //UNIX or linux system 
		extern "C"
		{
		//多线程函数,互斥锁
		#include <pthread.h>
		}
		//多线程
		#define pthread_M_CREATE(pReti,pThrid,pFunc,pArgs) \
			{ \
			*pReti=pthread_create(pThrid, NULL, pFunc, pArgs); \
			}
		#define pthread_M_EXIT(pRet) \
			{ \
			pthread_exit(pRet); \
			}
		#define pthread_M_KILL(pid,signo) \
			{ \
			pthread_kill(pid,signo); \
			}
		#define pthread_M_JOIN(pid,ptr) \
			{ \
			pthread_join(pid,ptr); \
			}
		#define pthread_M_DETACH(pid) \
			{ \
			pthread_detach(pid); \
			}
		

		#ifdef __CSOCKET_H__
			extern "C"
			{
			//包含socket通讯库函数
			#include <unistd.h>  //::close(fd) --- close socket
			#include <sys/types.h>
			#include <sys/socket.h>
			#include <netinet/in.h>
			#include <arpa/inet.h>
			#include <netdb.h>
			}
			//Sun unix下没有定义此常量,linux下在/usr/include/bits/socket.h中定义有此常量
			//EPIPE  The local end has been shut down on a connection oriented socket.  In this case the  process  will  also
			//receive a SIGPIPE unless MSG_NOSIGNAL is set.
			//如不保护,将导致程序发生broken pipe错误
			#define MSG_NOSIGNAL 0x4000
			
			#define closesocket close
			
			#define SOCK_M_GETERROR errno //get wrong code
		#endif//?#ifdef __CSOCKET_H__
		
	#endif //?#ifdef __WINDOWS_SYS__  #else

#endif //?#ifndef __OSDEF_H__




⌨️ 快捷键说明

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