void TCPSocket::Open( Protocol protocol ) throw (TCPException) { if (IsOpened()) { throw TCPException("Socket::Open","重复打开"); } m_descriptor = socket(AF_INET,protocol,0); if (m_descriptor == SOCKET_ERROR) { throw TCPException("Socket::Open","创建失败",0); } }
TCPSocket::TCPSocket(addrinfo *addr) { if (addr->ai_socktype != SOCK_STREAM) { throw TCPException("Bad socket type"); } wasClosed = false; sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (sockfd == -1) { perror("socket"); throw TCPException("Failed to create socket"); } }
void TCPSocket::Bind( const sockaddr_in& addr ) { int error; if (!IsOpened()) { throw TCPException("Socket::bind","Socket没有打开"); } const sockaddr* socketAddress = reinterpret_cast<const sockaddr*>(&addr); error = ::bind(m_descriptor,socketAddress,sizeof(addr)); if(error == SOCKET_ERROR ) { throw TCPException("Socket::bind","绑定Socket失败",error); } }
void TCPSocket::Listen( int queuelen ) throw (TCPException) { int error; if(!IsOpened()) { throw TCPException("Socket::listen","Socket未打开"); } error = ::listen(m_descriptor, queuelen); if(error == SOCKET_ERROR) { throw TCPException("Socket::listen","listen函数有错误"); } }
void TCPSocket::bindSocket(addrinfo *addr) { int res = bind(sockfd, addr->ai_addr, addr->ai_addrlen); if (res == -1) { perror("bind"); throw TCPException("Failed to bind"); } }
void TCPSocketAddress::Lookup(const string & ip) throw (TCPException) { hostent * hostinfo = NULL; hostinfo = gethostbyname(ip.c_str()); if(hostinfo == NULL) { throw TCPException("Address::lookup","gethostbyname return NULL"); } m_strName = hostinfo->h_name; m_strAliases.clear(); // save all found aliases in list if(hostinfo->h_aliases[0] != NULL) { for(char ** alias = hostinfo->h_aliases; *alias != NULL; alias++) { m_strAliases.push_back(*alias); } } m_sreIpAddresses.clear(); // save all found IPs in list for(in_addr ** addr = reinterpret_cast<in_addr**>(hostinfo->h_addr_list); *addr != NULL; addr++) { m_sreIpAddresses.push_back(**addr); } }
void TCPSocket::connectToAddr(addrinfo *addr) { int res = connect(sockfd, addr->ai_addr, addr->ai_addrlen); if (res == -1) { perror("connect: "); throw TCPException("Failed to connect"); } }
TCPSocketAddress TCPSocket::GetSockName() throw (TCPException) { int error; if (!IsOpened()) { throw TCPException("Socket::getsockname()","Socket未打开"); } sockaddr_in address; int addrLen = sizeof(address); sockaddr* socketAddress = reinterpret_cast<sockaddr*>(&address); error = ::getsockname(m_descriptor,socketAddress,&addrLen); if (error == SOCKET_ERROR) { throw TCPException("Socket::getSockName","getsockname有异常"); } return TCPSocketAddress(address); }
int TCPSocket::Sendto( const sockaddr_in& addr, const char* data, size_t size, int flags /*= 0*/ ) { int bytes; if(!IsOpened()) { throw TCPException("Socket::sendto", "Socket没有打开", 0); } const sockaddr* socketAddress = reinterpret_cast<const sockaddr*>(&addr); bytes = ::sendto(m_descriptor, data, size, flags, socketAddress, sizeof(addr)); if(bytes == SOCKET_ERROR) { throw TCPException("Socket::sendto", "sendto出错"); } return bytes; }
TCPSocket::Descriptor TCPSocket::Accept( sockaddr_in& addr ) throw (TCPException) { Descriptor sfd; if(!IsOpened()) { throw TCPException("Socket::accept","Socket未打开"); } int addrLen = sizeof(addr); sockaddr* socketAddress = reinterpret_cast<sockaddr*>(&addr); sfd = ::accept(m_descriptor, socketAddress, &addrLen); if(sfd == SOCKET_ERROR) { int errorCode = WSAGetLastError(); throw TCPException("Socket::accept","::accept函数发生错误"); } return sfd; }
const string & TCPSocketAddress::GetAliasName(size_t index) const throw (TCPException) { if(index >= m_strAliases.size()) { throw TCPException("Address::getAliasName", "the index is too big"); } return m_strAliases[index]; }
void TCPSocket::reusePort() { // reusing the port int yes = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockport"); throw TCPException("Failed to set socket on port"); } }
int TCPSocket::Recvfrom( sockaddr_in& addr, char* data, size_t size, int flags /*= 0*/ ) throw (TCPException) { int bytes; if(!IsOpened()) { throw TCPException("Socket::recvfrom", "Socket没有打开", 0); } int SenderAddrSize = sizeof(addr); sockaddr* socketAddress = reinterpret_cast<sockaddr*>(&addr); bytes = ::recvfrom(m_descriptor, data, size, flags, socketAddress, &SenderAddrSize); if (bytes == SOCKET_ERROR) { throw TCPException("Socket::recvfrom","recvFrom函数失败"); } return bytes; }
string TCPSocketAddress::GetIPString(size_t index) const throw (TCPException) { if(index >= m_sreIpAddresses.size()) { throw TCPException("Address::getIPString", "the index is out of range", OFF_RANGE_CODE); } string ip = inet_ntoa(m_sreIpAddresses[index]); return ip; }
int TCPSocket::Send( const char* data, size_t size, int flags /*= 0 */ ) { //m_localmutex->Lock(); int bytes; if(!IsOpened()) { throw TCPException("Socket::send", "Socket没有开打", 0); } bytes = ::send(m_descriptor,data,size,flags); return bytes; }
int TCPSocket::Recv( char* data, size_t size, int flags /*= 0*/ ) { int bytes; if(!IsOpened()) { throw TCPException("Socket::recv", "Socket没有打开"); } bytes = ::recv(m_descriptor, data, size, flags); //_trace(L"TCPSocket::Recv \n"); return bytes; }
void TCPSocket::Connect( const sockaddr_in& addr ) throw(TCPException) { int error; if(!IsOpened()) { throw TCPException("Socket::connect", "Socket没有打开"); } const sockaddr* socketAddress = reinterpret_cast<const sockaddr*>(&addr); error = ::connect(m_descriptor, socketAddress, sizeof(addr)); if (error == SOCKET_ERROR) { //throw TCPException("Socket::connect","Socket连接出错",error); } }
TCPSocketAddress::operator sockaddr_in() const throw (TCPException) { if(m_sreIpAddresses.empty()) { throw TCPException("Address::operator()", " no ip address"); } sockaddr_in address; address.sin_family = AF_INET; address.sin_port = m_usPort; address.sin_addr = m_sreIpAddresses.front(); return address; }
void TCPSocketAddress::Lookup() throw (TCPException) { hostent * hostinfo = NULL; if(m_sreIpAddresses.empty()) { throw TCPException("Address::lookup", "ip address is null"); } // just using first IP in list hostinfo = gethostbyaddr(reinterpret_cast<char*>(&m_sreIpAddresses.front()), sizeof(in_addr), AF_INET); if(hostinfo == NULL) { throw TCPException("Address::lookup","gethostbyaddr return NULL"); } m_strName = hostinfo->h_name; m_strAliases.clear(); if(hostinfo->h_aliases[0] != NULL) { for(char ** alias = hostinfo->h_aliases; *alias != NULL; alias++) { m_strAliases.push_back(*alias); } } m_sreIpAddresses.clear(); for(in_addr ** addr = reinterpret_cast<in_addr**>(hostinfo->h_addr_list); *addr != NULL; addr++) { m_sreIpAddresses.push_back(**addr); } }
int TCPSocket::Wait( bool& read, bool& write, bool& exception, int seconds /*= 0*/, int useconds /*= 0*/ ) throw (TCPException) { if(!IsOpened()) { throw TCPException("Socket::wait","Socket未打开",NOT_OPENED_CODE); } struct timeval time; time.tv_sec = seconds; time.tv_usec = useconds; fd_set* readfds = NULL; fd_set* writefds = NULL; fd_set* exceptfds = NULL; if (read) { readfds = new fd_set; FD_ZERO(readfds); FD_SET(m_descriptor,readfds); } if (write) { writefds = new fd_set; FD_ZERO(writefds); FD_SET(m_descriptor,writefds); } if (exceptfds) { exceptfds = new fd_set; FD_ZERO(exceptfds); FD_SET(m_descriptor,exceptfds); } int ret = select(m_descriptor+1,readfds,writefds,exceptfds,&time); if (read) { //检查m_descriptor在readfds集合中的状态是否发生变化,返回true,表示有变化 read = (bool)(FD_ISSET(m_descriptor,readfds)); } if(write) { write = (bool)(FD_ISSET(m_descriptor,writefds)); } if (exception) { exception = (bool)(FD_ISSET(m_descriptor,exception)); } delete readfds; delete writefds; delete exceptfds; if (ret < 0) { throw TCPException("Socket::wait","select 发生错误"); } return (ret!=0); }