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

📄 socket.cpp

📁 LINUX下发送邮件的库,测试很好用,有各种发送测试的例子
💻 CPP
字号:
/************************************************************************* *                                                                       * * This program is free software; you can redistribute it and/or modify  * * it under the terms of the GNU General Public License as published by  * * the Free Software Foundation; either version 2 of the License, or     * * (at your option) any later version.                                   * *                                                                       * *************************************************************************/#include "socket.h"/** * Constructor. * * @param host The host this socket is connected to. */Socket::Socket (const string &host) : Logger (){		this->host   = host;		this->sockfd = -1;}/** * Constructor. * * @param host The host this socket is connected to. * @param fd   The file descriptor this socket uses for IO. */Socket::Socket (const string &host, int fd) : Logger (){		this->host   = host;		this->sockfd = fd;}/** * Destructor. */Socket::~Socket (){	if (this->sockfd != -1)	{		close (this->sockfd);	}}/** * Connect this socket. */void Socket::connect () {}/** * Returns the name of the localhost or the * string "localhost" if something goes wrong. * * @returns The name of the localhost or localhost. */string Socket::localhost (){	char *buf     = 0;	int   buf_len = 0;	int myerror   = 0;	string host   = "localhost";	do 	{		if (buf) 		{			buf_len += buf_len;			buf = (char*)realloc (buf, buf_len);		} 		else 		{			buf_len = 128;        /* Initial guess */			buf = (char*)malloc (buf_len);		}		if (! buf)		{			return (host);		}	} while 	(	 	(		 	(myerror = gethostname (buf, buf_len)) == 0 && 			!memchr (buf, '\0', buf_len)		) || errno == ENAMETOOLONG	);	if (!myerror)	{		host = string (buf);	}	free (buf);	return (host);}/** * Reads one line from the socket. * * @returns One line read from the socket. * * @throws TransferException  *              On any uncommon event that occurs while receiving *              the data. */string Socket::readSocket (){	string data;	if (this->sockfd != -1)	{		bool EOL = false;		while (!EOL)		{			struct timeval tv;			tv.tv_sec = 30;			tv.tv_usec = 0;			fd_set rfds;			FD_ZERO (&rfds);			FD_SET (this->sockfd, &rfds);					int retval = select (this->sockfd+1, &rfds, NULL, NULL, &tv);			if (retval == -1)			{				throw (TransferException (strerror (errno), "Socket::readSocket()", 1));			}    			else if (retval == 0)			{				throw				( 					TransferException					(						LIBSMTP_I18N_1 ("Socket timeout."), 						"Socket::readSocket()", 						2					) 				);			}			else			{				char b;				int bytes = read (this->sockfd, &b, 1);				if (bytes == -1)				{					throw (TransferException (strerror (errno), "Socket::readSocket()", 3));				}    				else if (bytes == 0)				{					throw					( 						TransferException							(								LIBSMTP_I18N_1 ("Socket was closed."), 								"Socket::readSocket()", 								4							) 										);				}    				else if (b != '\n')				{					data += b;				}				else				{					EOL = true;				}			}		}	}	else	{    		throw (TransferException (LIBSMTP_I18N_1 ("Socket is closed."), "Socket::readSocket()", 5));	}	LOG_VERBOSE << this->host << ":\t" << data << endl;	return (data);}/** * Writes data to the socket. * * @param data The data string that should be written to  *             the socket. * * @throws TransferException  *             On any uncommon event that occurs while sending *             the data. */void Socket::writeSocket (const string &data){	if (this->sockfd != -1)	{		if (::write (this->sockfd, data.c_str (), data.length ()) == -1)		{			throw (TransferException (strerror (errno), "Socket::writeSocket()", 1));		}    	}	else	{    		throw (TransferException (LIBSMTP_I18N_1 ("Socket is closed."), "Socket::writeSocket()", 2));	}	LOG_VERBOSE << localhost () << ":\t" << data;}

⌨️ 快捷键说明

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