当前位置: 首页>>代码示例>>C++>>正文


C++ SocketError函数代码示例

本文整理汇总了C++中SocketError函数的典型用法代码示例。如果您正苦于以下问题:C++ SocketError函数的具体用法?C++ SocketError怎么用?C++ SocketError使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SocketError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: socket

void UDPSocket::open(unsigned short localPort)
{
	// create
	mSocketFD = socket(AF_INET,SOCK_DGRAM,0);
	if (mSocketFD<0) {
		perror("socket() failed");
		throw SocketError();
	}
	// Set "close on exec" flag to avoid open sockets inheritance by
	// child processes, like 'transceiver'.
	int flags = fcntl(mSocketFD, F_GETFD);
	if (flags >= 0) fcntl(mSocketFD, F_SETFD, flags | FD_CLOEXEC);


	// bind
	struct sockaddr_in address;
	size_t length = sizeof(address);
	bzero(&address,length);
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = INADDR_ANY;
	address.sin_port = htons(localPort);
	if (bind(mSocketFD,(struct sockaddr*)&address,length)<0) {
		perror("bind() failed");
		throw SocketError();
	}
}
开发者ID:0x7678,项目名称:openbts-uhd,代码行数:26,代码来源:Sockets.cpp

示例2: socket

void UDDSocket::open(const char* localPath)
{
	// create
	mSocketFD = socket(AF_UNIX,SOCK_DGRAM,0);
	if (mSocketFD<0) {
		perror("socket() failed");
		devassert(0);
		throw SocketError();
	}

	// bind
	struct sockaddr_un address;
	size_t length = sizeof(address);
	bzero(&address,length);
	address.sun_family = AF_UNIX;
	strcpy(address.sun_path,localPath);
	unlink(localPath);
	if (bind(mSocketFD,(struct sockaddr*)&address,length)<0) {
		char buf[1100];
		sprintf(buf,"bind(path %s) failed",localPath);
		perror(buf);
		devassert(0);
		throw SocketError();
	}
}
开发者ID:5728136cs,项目名称:CommonLibs,代码行数:25,代码来源:Sockets.cpp

示例3: connect

Future<Nothing> connect(const Socket& socket, const Address& to)
{
  // Now check that a successful connection was made.
  int opt;
  socklen_t optlen = sizeof(opt);
  int s = socket.get();

  // NOTE: We cast to `char*` here because the function prototypes on Windows
  // use `char*` instead of `void*`.
  if (::getsockopt(
          s,
          SOL_SOCKET,
          SO_ERROR,
          reinterpret_cast<char*>(&opt),
          &optlen) < 0) {
    return Failure(
        SocketError("Failed to get status of connection to " + stringify(to)));
  }

  if (opt != 0) {
    // Make the error visible to the `SocketError` constructor by pushing
    // it into the global per-thread error. MESOS-6520 which should
    // allow us to pass the error code directly into `SocketError`.

#ifdef __WINDOWS__
    ::WSASetLastError(opt);
#else
    errno = opt;
#endif

    return Failure(SocketError("Failed to connect to " + stringify(to)));
  }

  return Nothing();
}
开发者ID:liyubobj,项目名称:mesos,代码行数:35,代码来源:poll_socket.cpp

示例4: assert

ClientSocket::ClientSocket(const std::string & hostNameOrIpAddress, const HostShort & portNumber)
{
    // Should have been initialized by the superclass.
    assert(socketId != NET_INVALID_SOCKET_ID);

    // Must have an IP address or URL.
    assert(!hostNameOrIpAddress.empty());

    //
    // Note: `gethostbyname()` is deprecated in WinSock2 in favor
    // of `getaddrinfo()`. So this is a possible future improvement.
    //
    hostent * hostEntity = gethostbyname(hostNameOrIpAddress.c_str());
    if (hostEntity == nullptr)
    {
        Close();
        throw SocketError("Failed to get hostent for " + hostNameOrIpAddress);
    }

    sockaddr_in addr;
    std::memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port   = portNumber.ToNetShort().GetRawValue();
    addr.sin_addr   = *reinterpret_cast<in_addr *>(hostEntity->h_addr);

    if (connect(socketId, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) != 0)
    {
        Close();
        throw SocketError("Failed to connect with host " + hostNameOrIpAddress +
                " at port #" + std::to_string(portNumber.GetRawValue()));
    }
}
开发者ID:walker-zheng,项目名称:code,代码行数:33,代码来源:sockets.cpp

示例5: DB

    void Socket::setBlockingMode(bool isBlocking) {
#ifndef _WIN32
        DB(PrintStatus(fd));
        if (!_myIsConnected) {
            AC_WARNING << "Socket::setBlockingMode setting blocking mode of a not connected socket, the mode will be lost after you connect";
        }
        int status = fcntl(fd, F_GETFL, 0);
        if (!isBlocking) {
            status |= O_NONBLOCK;
        } else {
            status &= ~O_NONBLOCK;
        }
        if (fcntl(fd, F_SETFL, status)== -1) {
            throw SocketError(getLastSocketError(), "Socket::setBlockingMode failed");
        }
#else
        u_long theFlag = 1;
        if (isBlocking) {
            theFlag = 0;
        }
        int theRC = ioctlsocket(fd, FIONBIO, &theFlag);
        if (theRC == SOCKET_ERROR) {
            int err = getLastSocketError();
            throw SocketError(err, "Socket::setBlockingMode failed");
        }
#endif
    }
开发者ID:artcom,项目名称:asl,代码行数:27,代码来源:Socket.cpp

示例6: kevent

std::vector<SocketStatus> Kqueue::wait(const SocketTable &table, int ms)
{
	std::vector<SocketStatus> sockets;
	timespec ts = { 0, 0 };
	timespec *pts = (ms <= 0) ? nullptr : &ts;

	ts.tv_sec = ms / 1000;
	ts.tv_nsec = (ms % 1000) * 1000000;

	int nevents = kevent(m_handle, nullptr, 0, &m_result[0], m_result.capacity(), pts);

	if (nevents == 0) {
		throw SocketError(SocketError::Timeout, "kevent");
	}
	if (nevents < 0) {
		throw SocketError(SocketError::System, "kevent");
	}

	for (int i = 0; i < nevents; ++i) {
		SocketAbstract *sc = table.at(m_result[i].ident).first;
		int flags = m_result[i].filter == EVFILT_READ ? SocketListener::Read : SocketListener::Write;

		sockets.push_back(SocketStatus{*sc, flags});
	}

	return sockets;
}
开发者ID:bdrewery,项目名称:irccd,代码行数:27,代码来源:SocketListener.cpp

示例7: ac_close

result_t Socket::create(int32_t family, int32_t type)
{
    if (m_sock != INVALID_SOCKET)
    {
        if (exlib::Service::hasService())
            ac_close();
        else
        {
            asyncEvent ac;
            close(&ac);
        }
    }

    m_family = family;
    m_type = type;

    if (family == net_base::_AF_INET)
        family = AF_INET;
    else if (family == net_base::_AF_INET6)
        family = AF_INET6;
    else
        return CHECK_ERROR(CALL_E_INVALIDARG);

    if (type == net_base::_SOCK_STREAM)
        type = SOCK_STREAM;
    else if (type == net_base::_SOCK_DGRAM)
        type = SOCK_DGRAM;
    else
        return CHECK_ERROR(CALL_E_INVALIDARG);

#ifdef _WIN32

    m_sock = WSASocket(family, type, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
    if (m_sock == INVALID_SOCKET)
        return CHECK_ERROR(SocketError());

    CreateIoCompletionPort((HANDLE) m_sock, s_hIocp, 0, 0);

#else

    m_sock = socket(family, type, 0);
    if (m_sock == INVALID_SOCKET)
        return CHECK_ERROR(SocketError());

    fcntl(m_sock, F_SETFL, fcntl(m_sock, F_GETFL, 0) | O_NONBLOCK);
    fcntl(m_sock, F_SETFD, FD_CLOEXEC);

#endif

#ifdef MacOS

    int set_option = 1;
    setsockopt(m_sock, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
               sizeof(set_option));

#endif

    return 0;
}
开发者ID:CYCOK,项目名称:fibjs,代码行数:59,代码来源:Socket.cpp

示例8: throw

int SocketClient::recv(char *buf, int len) throw (SocketError) {
    int ret = ::recv(cfd, buf, len, 0);
    if (ret <= 0) {
        throw SocketError(cfd, errno, strerror(errno));
    }
    else if (ret == 0) {
        throw SocketError(cfd, EREMOTEIO, "remote client closed");
    }
    return ret;
}
开发者ID:ClickSmile,项目名称:HTTPSrv,代码行数:10,代码来源:socket.cpp

示例9: FD_ZERO

std::vector<SocketStatus> Select::wait(const SocketTable &table, int ms)
{
	timeval maxwait, *towait;
	fd_set readset;
	fd_set writeset;

	FD_ZERO(&readset);
	FD_ZERO(&writeset);

	SocketAbstract::Handle max = 0;

	for (const auto &s : table) {
		if (s.second.second & SocketListener::Read) {
			FD_SET(s.first, &readset);
		}
		if (s.second.second & SocketListener::Write) {
			FD_SET(s.first, &writeset);
		}

		if (s.first > max) {
			max = s.first;
		}
	}

	maxwait.tv_sec = 0;
	maxwait.tv_usec = ms * 1000;

	// Set to nullptr for infinite timeout.
	towait = (ms < 0) ? nullptr : &maxwait;

	auto error = ::select(max + 1, &readset, &writeset, nullptr, towait);
	if (error == SocketAbstract::Error) {
		throw SocketError(SocketError::System, "select");
	}
	if (error == 0) {
		throw SocketError(SocketError::Timeout, "select", "Timeout while listening");
	}

	std::vector<SocketStatus> sockets;

	for (auto &c : table) {
		if (FD_ISSET(c.first, &readset)) {
			sockets.push_back(SocketStatus{*c.second.first, SocketListener::Read});
		}
		if (FD_ISSET(c.first, &writeset)) {
			sockets.push_back(SocketStatus{*c.second.first, SocketListener::Write});
		}
	}

	return sockets;
}
开发者ID:bdrewery,项目名称:irccd,代码行数:51,代码来源:SocketListener.cpp

示例10: SocketPeek

uint32 SocketPeek(TSocket *s, char *buffer, uint32 len)
{
	int32 r=recv(*s,buffer,len,MSG_PEEK);

	if (r==(-1))
#ifdef _WIN32
		if (SocketError()==WSAEMSGSIZE)
#else
		if (SocketError()==EMSGSIZE)
#endif
			return len;

	return r;
}
开发者ID:MessagePlus,项目名称:httpServer,代码行数:14,代码来源:socket.c

示例11: main

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        throw SocketError();
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        throw SocketError();
    while(1) {
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
			throw SocketError();
            bzero(buffer,256);
            n = read(sockfd,buffer,255);
        if (n < 0)
			throw SocketError();
	    if( strcmp(buffer, "quit") == 0 || strcmp(buffer, "QUIT") == 0 ) {
			printf("QUIT command received. Bye!\n");
			break;
		}
        printf("%s\n",buffer);
}
    close(sockfd);
    return 0;
}
开发者ID:yoryos,项目名称:Bloomberg-CMS,代码行数:48,代码来源:client.cpp

示例12: process

        virtual result_t process()
        {
            static LPFN_CONNECTEX ConnectEx;
            int nError;

            if (!ConnectEx)
            {
                GUID guidConnectEx = WSAID_CONNECTEX;
                DWORD dwBytes;

                if (SOCKET_ERROR
                        == WSAIoctl(m_s, SIO_GET_EXTENSION_FUNCTION_POINTER,
                                    &guidConnectEx, sizeof(guidConnectEx),
                                    &ConnectEx, sizeof(ConnectEx), &dwBytes, NULL,
                                    NULL))
                    return CHECK_ERROR(SocketError());
            }

            if (ConnectEx(m_s, (sockaddr *) &m_ai, (int) m_ai.size(), NULL, 0,
                          NULL, this))
                return CHECK_ERROR(CALL_E_PENDDING);

            nError = WSAGetLastError();
            return CHECK_ERROR((nError == WSA_IO_PENDING) ? CALL_E_PENDDING : -nError);
        }
开发者ID:Andrew-Zhang,项目名称:fibjs,代码行数:25,代码来源:Socket_iocp.cpp

示例13: SocketConnReset

/** \brief read the given number of bytes 
 *
 * Read size bytes into the buffer. Wait until size Bytes are available
 * data maight be lossed, if size is smaller then the incomming package.
 * This situation will not be treated as an error.
 *
 * \param buf   Pointer to the data buffer.
 * \param size  Maximum count of bytes to read.
 * \param from  Will be overwritten with the address of the sender
 *
 * \see Socket::recvAvailable Socket::recv
 */
int DgramSocket::recvFrom(void *buf,int size,Address &from)
{
    int len;
    SocketLenT addrLen=from.getSockAddrSize();

    len=recvfrom(_sd,
                 (char*)buf,
                 size,
                 0,
                 from.getSockAddr(),
                 &addrLen);
    if(len<0)
    {
#if defined WIN32
        if(getError()==WSAECONNRESET)
        {
            throw SocketConnReset("recvfrom");
        }
        if(getError()==WSAEMSGSIZE)
        {
            len=size;
        }
        else
#endif
        throw SocketError("recvfrom()");
    }
    return len;
}
开发者ID:BackupTheBerlios,项目名称:opensgplus,代码行数:40,代码来源:OSGDgramSocket.cpp

示例14: CmdDlgProc

//////////////////////////////////////////////////////////////////////////
//远程CMD窗口过程
//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK CmdDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_COMMAND:
		{
			switch(LOWORD(wParam))
			{
			case IDC_BUTTON1:
				{
					//发送远程CMD命令要求
					CmdRequest(sockfd,hDlg);
				}
				break;
			}
		}
		break;
	case WM_SOCKET:
		{
			switch(LOWORD(lParam))
			{
			case FD_WRITE:
				{
					//继续发送
					SendCmd(sockfd,NULL,NULL);
				}
				break;
			case FD_READ:
				{
					RecvCmd(sockfd);
				}
				break;
			case FD_CLOSE:
				{
					//SOCKET出错处理,MainFunc.H里 清理缓冲区
					SocketError(sockfd);
					EndDialog(hDlg,NULL);
				}
				break;
			}
		}
		break;
	case WM_INITDIALOG:
		{
			sockfd = (SOCKET)lParam;
			//非拥塞模式
			WSAAsyncSelect(sockfd,hDlg,WM_SOCKET,FD_WRITE|FD_READ|FD_CLOSE);
		}
		break;
	case WM_CLOSE:
		{
			EndDialog(hDlg,NULL);
		}
		break;
	default:
		break;
	}

	return FALSE;
}
开发者ID:drupalhunter,项目名称:HackCollect,代码行数:63,代码来源:NsDoor.cpp

示例15: m_handle

Kqueue::Kqueue()
	: m_handle(kqueue())
{
	if (m_handle < 0) {
		throw SocketError(SocketError::System, "kqueue");
	}
}
开发者ID:bdrewery,项目名称:irccd,代码行数:7,代码来源:SocketListener.cpp


注:本文中的SocketError函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。