www.pudn.com > mputil.rar > socketio.cpp


/* 
 * socketio.cpp 
 * 
 * Copyright (c) 2003 Machine Perception Laboratory 
 * University of California San Diego. 
 * Please read the disclaimer and notes about redistribution 
 * at the end of this file. 
 * 
 * Authors: Josh Susskind 
 */ 
 
#include  
#ifdef WIN32 
#include  
#endif 
#include "socketio.h"  
 
#include "../common.h" 
 
#ifdef WIN32 
#define VERIFY(x) if ( (x) == SOCKET_ERROR ) \ 
    {_isConnect = FALSE;} 
#define MSG_NOSIGNAL 0 
#else 
#define VERIFY(x) if ( (x) == -1) \ 
    {_isConnect = false;} 
#endif 
 
// ================================================================ 
 
 
	MPSocketIO::MPSocketIO() 
	{ 
		//hostName = new char[strlen(_hostName)+1]; 
		strcpy(m_hostName,"not specified"); 
		m_hostPort = 31000; 
		isConvert = false; 
		_isConnect = false; 
	} 
 
	// ================================================================ 
 
	MPSocketIO::MPSocketIO(char _hostName[],int _hostPort,int _isConvert) 
	{ 
		//hostName = new char[strlen(_hostName)+1]; 
		//m_sock = socket(AF_INET,SOCK_STREAM,0); 
		strcpy(m_hostName,_hostName); 
		m_hostPort = _hostPort; 
		isConvert = _isConvert; 
		_isConnect = FALSE; 
	} 
 
	// ================================================================ 
 
	MPSocketIO::~MPSocketIO() 
	{ 
		disconnect(); 
	} 
 
	// ================================================================ 
 
	BOOL MPSocketIO::isConnect() 
	{ 
		return _isConnect; 
	} 
 
	// ================================================================ 
 
	//connect to a server keeping because old code and I do not want to break past function calls 
	int MPSocketIO::connectServer(void) 
	{ 
		if((m_sock = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { _isConnect = FALSE; return -3; } 
		 
		m_addr.sin_addr.s_addr = inet_addr(m_hostName); 
		m_addr.sin_family = AF_INET; 
		m_addr.sin_port= htons(m_hostPort); 
		 
		if(::connect(m_sock,(struct sockaddr*)&(m_addr),sizeof(struct sockaddr)) < 0) { 
			closesocket(m_sock); 
			m_sock = -1; 
			_isConnect = FALSE; 
		} 
		else { 
			_isConnect = TRUE; 
		} 
		return _isConnect; 
	} 
 
	// ================================================================ 
 
	//connect to a server 
	int MPSocketIO::connect(void) 
	{ 
		if((m_sock = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { _isConnect = FALSE; return -3; } 
		 
		m_addr.sin_addr.s_addr = inet_addr(m_hostName); 
		m_addr.sin_family = AF_INET; 
		m_addr.sin_port= htons(m_hostPort); 
		 
		if(::connect(m_sock,(struct sockaddr*)&(m_addr),sizeof(struct sockaddr)) < 0) { 
			closesocket(m_sock); 
			m_sock = -1; 
			_isConnect = FALSE; 
		} 
		else { 
			_isConnect = TRUE; 
		} 
		return _isConnect; 
	} 
 
	// ================================================================ 
 
	//keep because this is old function call 
	void MPSocketIO::disconnectServer() 
	{ 
		if(isConnect()) closesocket(m_sock); 
		_isConnect = FALSE; 
		m_sock = -1; 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::disconnect() 
	{ 
		if(isConnect()) closesocket(m_sock); 
		_isConnect = FALSE; 
		m_sock = -1; 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::writeConvert(char byte[],int numByte) 
	{ 
		if(sizeof(byte) < (unsigned int)numByte) return; 
		for(int i=numByte-1;i>0;i--) 
		{ 
			VERIFY(::send(m_sock,&byte[i],1,MSG_NOSIGNAL)); 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::writeByte(const char &b) 
	{ 
		if(isConnect()) { 
			VERIFY(::send(m_sock,&b,1,MSG_NOSIGNAL)); 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const int &i) 
	{ 
		if(isConnect()) { 
			int_data.data = i; 
			if(isConvert) { 
				writeConvert(int_data.byte,4); 
			} else { 
				VERIFY(::send(m_sock,int_data.byte,4,MSG_NOSIGNAL)); 
			} 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const long &l) 
	{ 
		if(isConnect()) { 
			long_data.data = l; 
			if(isConvert) { 
				writeConvert(long_data.byte,8); 
			} else { 
				VERIFY(::send(m_sock,long_data.byte,8,MSG_NOSIGNAL)); 
			} 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const char &ch) 
	{ 
		if(isConnect()) { 
			VERIFY(::send(m_sock,&ch,1,MSG_NOSIGNAL)); 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const char *s) 
	{ 
		if(m_sock>0) { 
			VERIFY(::send(m_sock,s,strlen(s),MSG_NOSIGNAL)); 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const float &f) 
	{ 
		 
		if(isConnect()) { 
			float_data.data = f; 
			if(isConvert) { 
				writeConvert(float_data.byte,4); 
			} else { 
				VERIFY(::send(m_sock,float_data.byte,4,MSG_NOSIGNAL)); 
			} 
		} 
	} 
 
	// ================================================================ 
 
	void MPSocketIO::write(const double &d) 
	{ 
		if(isConnect()) { 
			double_data.data = d; 
			if(isConvert) { 
				writeConvert(double_data.byte,8); 
			} else { 
				VERIFY(::send(m_sock,double_data.byte,8,MSG_NOSIGNAL)); 
			} 
		} 
	} 
 
	// ================================================================ 
 
	char MPSocketIO::readByte() 
	{ 
		char ch; 
		readFull(&ch,1); 
		return ch; 
	} 
 
	// ================================================================ 
 
	int MPSocketIO::readInt() 
	{ 
		if(isConvert) { 
			readConvert(int_data.byte,4); 
		} else { 
			readFull(int_data.byte,4); 
		} 
		return int_data.data; 
	} 
 
	// ================================================================ 
 
	long MPSocketIO::readLong() 
	{ 
		if(isConvert) { 
			readConvert(long_data.byte,8); 
		} else { 
			readFull(long_data.byte,8); 
		} 
		return long_data.data; 
	} 
 
	// ================================================================ 
 
	float MPSocketIO::readFloat() 
	{ 
		if(isConvert) { 
			readConvert(float_data.byte,4); 
		} else { 
			readFull(float_data.byte,4); 
		} 
		return float_data.data; 
	} 
 
	// ================================================================ 
 
	double MPSocketIO::readDouble() 
	{ 
		if(isConvert) { 
			readConvert(double_data.byte,8); 
		} else { 
			readFull(double_data.byte,8); 
		} 
		return double_data.data; 
	} 
 
	// ================================================================ 
 
	int MPSocketIO::readConvert(char byte[],int numRead) 
	{ 
		int i , readbyte; 
		if(isConnect()) { 
			if(sizeof(byte) < (unsigned int)numRead) return -1; 
			for(i=numRead-1;i>=0;i--) 
			{ 
				readbyte = ::recv(m_sock,&byte[i],1,0); 
				if(readbyte < 0) return -1; 
			} 
			return numRead; 
		} 
		return -1; 
	} 
 
	// ================================================================ 
 
	int MPSocketIO::readFull(char byte[],int numRead) 
	{ 
		int readbyte; 
		if(_isConnect) { 
			memset(byte,0,numRead); 
			if(sizeof(byte) < (unsigned int)numRead) return -1; 
			readbyte = recv(m_sock,byte,numRead,0); 
			return readbyte; 
		} 
		return -1; 
	} 
 
	// ================================================================ 
 
	int MPSocketIO::readString(std::string &s) 
	{ 
		int readbyte; 
		const int MaxRecv = 512; 
		char buf[MaxRecv +1]; 
 
		s = ""; 
		memset(buf,0,MaxRecv+1); 
 
		if(_isConnect) { 
			readbyte = recv(m_sock,buf,MaxRecv,0); 
			if(readbyte < 0) return readbyte; 
			s = buf; 
			return readbyte; 
		} 
		return -1; 
	} 
 
	// ================================================================ 
 
	const MPSocketIO& MPSocketIO::operator >> (std::string& s) const 
	{ 
		int readbyte; 
		const int MaxRecv = 512; 
		char buf[MaxRecv +1]; 
 
		s = ""; 
		memset(buf,0,MaxRecv+1); 
 
		if(_isConnect) { 
			readbyte = recv(m_sock,buf,MaxRecv,0); 
			if(readbyte < 0) { return *this; } 
			s = buf; 
			 
		} 
		return *this; 
	} 
 
	// ================================================================ 
 
	const MPSocketIO& MPSocketIO::operator << (const std::string& s) const 
	{ 
		int readbyte; 
 
		if(_isConnect) { 
			readbyte = send(m_sock,s.c_str(),s.size(),MSG_NOSIGNAL); 
			if(readbyte < 0) { return *this; } 
			 
		} 
		return *this; 
	} 
 
	// ================================================================ 
 
	//accept incoming socket by a server 
	bool MPSocketIO::accept(MPSocketIO& new_socket) const{ 
		int addr_length = sizeof(m_addr); 
 
#ifdef WIN32 
		new_socket.m_sock= ::accept(m_sock, (sockaddr *) &m_addr, &addr_length); 
#else 
		new_socket.m_sock= ::accept(m_sock, (sockaddr *) &m_addr, ( socklen_t * )&addr_length); 
#endif 
		if (new_socket.m_sock <= 0) 
			return false; 
		else  
			return true; 
	} 
 
	// ================================================================ 
 
	//Bind a server to a port 
	int MPSocketIO::bind () { 
 
		if((m_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ) { _isConnect = FALSE; return -3; } 
		 
		m_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
		m_addr.sin_family = AF_INET; 
		m_addr.sin_port= htons(m_hostPort); 
		std::cout << "port number: " << ntohs(m_addr.sin_port) << std::endl; 
 
		int bind_rtn = ::bind(m_sock, (const struct sockaddr *)&m_addr, sizeof(m_addr)); 
		if (bind_rtn == -1) 
			return false; 
		else  
			return m_sock; 
	} 
 
	// ================================================================ 
 
	//listen to a server port 
	bool MPSocketIO::listen() const { 
 
		int listen_rtn = ::listen (m_sock, MAXCONNECTIONS); 
		if (listen_rtn == -1) 
			return false; 
		else 
			return true; 
	} 
 
 
 
// ================================================================ 
 
/* 
 *  
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 
 *  
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. 
 *  
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 *  
 */