/*!

*/
MonitorServer::MonitorServer( QObject * parent,
                              const MainData & main_data,
                              const int port )

    : QObject( parent )
    , M_main_data( main_data )
    , M_port( static_cast< quint16 >( port ) )
    , M_socket( new QUdpSocket( this ) )

{
    //if ( ! M_socket->bind( QHostAddress::Any, M_port ) )
    if ( ! M_socket->bind( M_port ) )
    {
        std::cerr << "MonitorServer. failed to bind the socket."
                  << std::endl;
        M_socket->close();
        return;
    }

    if ( ! isConnected() )
    {
        std::cerr << "MonitorServer. failed to initialize the socket."
                  << std::endl;
        M_socket->close();
        return;
    }

    // setReadBufferSize() makes no effect for QUdpSocet...
    // M_socket->setReadBufferSize( 8192 * 256 );

    connect( M_socket, SIGNAL( readyRead() ),
             this, SLOT( handleReceive() ) );
}
Beispiel #2
0
void ClientSocket::process()
{
	if(m_socketState == InvalidState)
	{
		if(m_socket != InvalidSocket)
		{
			printf("socketState is invalid, but socket in not invalid, colse socket");
			closesocket(m_socket);
			m_socket = InvalidSocket;
		}
	}
	
	else if(m_socketState == ConnectionPending)
	{
		int optval = 0;
		socklen_t optlen = sizeof(optval);
		int ret = getsockopt(m_socket, SOL_SOCKET, SO_ERROR, (char*)&optval, &optlen);
		if(ret == -1)
		{
			printf("getsockopt() failed, state is ConnectionPending(%d)", WSAGetLastError());
			closesocket(m_socket);
			m_socket = InvalidSocket;
		}
		else
		{
			if(optval == WSAEINPROGRESS)
			{
				printf("still trying connect...");
			}
			else if(optval == 0)
			{
				printf("connected!");
				m_socketState = Connected;

				//连接上的操作

			}
			else
			{
				printf("optval failed, state is ConnectionPending(%d)", WSAGetLastError());
				closesocket(m_socket);
				m_socket = InvalidSocket;
			}
		}
	}

	else if(m_socketState == Connected)
	{
		handleReceive();
	}
}
void Client::doReceive()
{
	boost::asio::async_read(
		socket_,
		boost::asio::buffer(receive_.getData(), Packet::headerLength),
		[this](boost::system::error_code ec, std::size_t)
		{
			if ( ! ec)
				handleReceive();
			else
				socket_.close();
		}
	);
}
Beispiel #4
0
int Server::run () {
	// Waiting for data
	int epfd = epoll_create (1);
	struct epoll_event epollevent;
	epollevent.events = EPOLLIN;			// we want read on it
	epollevent.data.fd = sock; 			   	// user set (we use the file descriptor)
	CHECK (epoll_ctl (epfd, EPOLL_CTL_ADD, sock, &epollevent));


	struct epoll_event changed[1];
	int n;
	while ((n = epoll_wait (epfd, changed, 1, -1)) > 0){
		CHECK (handleReceive());
	}

	close (epollfd);
	close (sock);
	return 0;
}
/*!

*/
MonitorClient::MonitorClient( QObject * parent,
                              DispHolder & disp_holder,
                              const char * hostname,
                              const int port,
                              const int version )

    : QObject( parent )
    , M_disp_holder( disp_holder )
    , M_server_port( static_cast< quint16 >( port ) )
    , M_socket( new QUdpSocket( this ) )
    , M_timer( new QTimer( this ) )
    , M_version( version )
    , M_waited_msec( 0 )
{
    assert( parent );

    // check protocl versin range
    if ( version < 1 )
    {
        M_version = 1;
    }

    if ( 4 < version )
    {
        M_version = 4;
    }

    QHostInfo host = QHostInfo::fromName( QString::fromAscii( hostname ) );

    if ( host.error() != QHostInfo::NoError )
    {
        qDebug() << "Error " << host.errorString();
        return;
    }

    M_server_addr = host.addresses().front();

    // INADDR_ANY, bind random created port to local
    if ( ! M_socket->bind( 0 ) )
    {
        std::cerr << "MonitorClient. failed to bind the socket."
                  << std::endl;
        return;
    }

    if ( ! isConnected() )
    {
        std::cerr << "MonitorClient. failed to initialize the socket."
                  << std::endl;
        return;
    }

    // setReadBufferSize() makes no effect for QUdpSocet...
    // M_socket->setReadBufferSize( 8192 * 256 );

    connect( M_socket, SIGNAL( readyRead() ),
             this, SLOT( handleReceive() ) );

    connect( M_timer, SIGNAL( timeout() ),
             this, SLOT( handleTimer() ) );

}