inetsocket.cpp

来自「LINUX下发送邮件的库,测试很好用,有各种发送测试的例子」· C++ 代码 · 共 102 行

CPP
102
字号
/************************************************************************* *                                                                       * * 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 "inetsocket.h"/** * Constructor. * * @param host The host this socket should connect. * @param port The port this socket should connect. */InetSocket::InetSocket		( 			const string &host, 			int port		) : SSLSocket (host){		this->port = port;}/** * Constructor. * * @param host The host this socket should connect. * @param port The port this socket should connect. * @param fd   The file descriptor this socket uses for IO. */InetSocket::InetSocket		( 		 	int fd,			const string &host,			int port		) : SSLSocket (host,fd){		this->port = port;}/** * Destructor. */InetSocket::~InetSocket () {}/** * Open the socket. * * @throws TransferException *                    If the connection to the host cannot be *                    established. */void InetSocket::connect (){	struct hostent     *servp;	struct sockaddr_in  addr;	servp = gethostbyname (this->host.c_str ());	if (servp == NULL)	{         		string msg;		switch (h_errno)		{			case HOST_NOT_FOUND:				msg = LIBSMTP_I18N_1 ("The specified host is unknown.");				throw (TransferException (msg, "InetSocket::connect()", 1));				break;			case NO_ADDRESS:				msg  = LIBSMTP_I18N_1 ("The requested name is valid but does not have an IP address.");				throw (TransferException (msg, "InetSocket::connect()", 2));				break;			case NO_RECOVERY:				msg = LIBSMTP_I18N_1 ("A non-recoverable name server error occurred.");				throw (TransferException (msg, "InetSocket::connect()", 3));				break;			case TRY_AGAIN:				msg  =  LIBSMTP_I18N_1 ("A temporary error occurred on an authoritative name server.");				msg  += " " + LIBSMTP_I18N_1 ("Try again later.");				throw (TransferException (msg, "InetSocket::connect()", 4));				break;		}	}	if ((this->sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1)	{		throw (TransferException (strerror (errno), "InetSocket::connect()", 5));	}	addr.sin_family = AF_INET;	addr.sin_port   = htons (this->port);	addr.sin_addr   = *((struct in_addr *)servp->h_addr);	bzero (&(addr.sin_zero), 8); 	if (::connect (this->sockfd, (struct sockaddr *)&addr, sizeof (struct sockaddr)) == -1)	{		throw (TransferException (strerror (errno), "InetSocket::connect()", 6));	}}

⌨️ 快捷键说明

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