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

📄 socket.h

📁 自动调整大小的进程池类的实现。 一个运用进程池的server类。 一个socket类。
💻 H
字号:
/**
 * Copyright 2007,神州数码(中国)有限公司
 * All rights reserved.
 * 
 * 文件名称:socket
 * 摘    要:socket
 * 
 * 当前版本:
 * 作    者:
 * 完成日期:
 *
 * 取代版本:1.1 
 * 原 作 者:gaozh
 * 完成日期:2007年
 *
 * 
 */
#ifndef __SOCKET_H_
#define __SOCKET_H_ 

#include <netinet/in.h>
#include <dirent.h>
#include <string.h>
#include <memory.h>
#include <utmp.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <net/if.h>           	/*if.h must put on the frontier of if_arp.h*/
#include <net/if_arp.h>
#include <netdb.h>            	/*gethostbyname()*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
//#include <firetalk.h>
#include <string>
#include <iostream>

using namespace std;

class ServerSocket;						//server socket类
class ClientSocket;						//client socket类
class Socket;							//socket通信类


void ignore_pipe();

/**
 * 服务器端socket类。给定一个监听端口,在该端口上建立server socket,
 * 等待client连接。连接之后返回一个socket类,通过该类可以发送接受数据。
 */
class ServerSocket
{
public:
    ServerSocket( const char* ps_port );		//监听端口为char *类型的server socket构造函数
    ServerSocket( const int& port );			//监听端口为int类型的server socket构造函数
    ServerSocket( const string& port );
    string client_ip( ){ return _ipaddr; }
    Socket* accept_connect( );					//accept client 连接

private:
    int     _listening_socket;					//listening socket号
    int     _port;								//监听端口
    string _ipaddr;
    bool set_port( const char* ps_port );		//设置监听端口
    bool set_port( const int& port );			//设置监听端口
    bool init_socket( const int& socket_type );	//初始化socket
};

/**
 * 数据接受与发送类,由ServerSocket类的accept_connect( )方法返回。
 * 其中有socket io句柄,接受、发送函数。
 */
class Socket
{
public:
    Socket( int &socket ) : _s( socket ){ };	//复制io句柄的构造函数
    Socket( const int &socket ) : _s( socket ) { };
    ~Socket( ){ close2( ); };					//析构函数,析构时,一定要关闭io句柄

    int receive( void* in, size_t count,int timeout=-1 );		//接受函数
    int Send( void* out, size_t count );		//发送函数
    int exactrecv( void* in, size_t count,int timeout=-1 );//精确接收
    bool LimitTimeOut(int time);							//接收超时函数
    void close2( );								//关闭socket io句柄

private:
    int _s;										//socket io句柄
};

class ClientSocket
{
public:
	ClientSocket( const char* ipaddr, const char* port, const char* proto );

	ClientSocket( char* ipaddr, char* port, char* proto );
	ClientSocket( string& ipaddr, string& port, string& proto );

	ClientSocket( char* ipaddr, int& port, char* proto);
	ClientSocket( string& ipaddr, int& port, string& proto );

	Socket* make_connect( );

private:
	char _proto[4];
	char _port[6];
	char _ipaddr[40];

	struct in_addr* atoaddr( );
	int atoport( );
};

class semaphore
{
	private:
		int _sid;//信号量的值
		int _semnum;//信号量的数量
		key_t _keyval;//键值
	public:
		semaphore( key_t& keyval,int& numsems );
		semaphore( );
		virtual ~semaphore( ){ remove( ); };
		bool lock( int seq=0 );
		bool unlock( int seq=0 );
		void remove( );
		bool create();
		bool init(int seq ,int value);
};
#endif

⌨️ 快捷键说明

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