示例#1
0
void Client::own_thread()
{
    TcpSocket sock;
    sock.connect( m_host, m_port );

    unsigned int version = 1;
    unsigned int options = 0;

    sock.send( version );
    sock.send( options );
    // first send size of name.
    sock.sendString( m_name );

    unsigned int images = 0;
    unsigned int width = 0;
    unsigned int height = 0;
    unsigned int bpp = 0;
    try
    {
        sock.receive( images );
        sock.receive( width );
        sock.receive( height );
        sock.receive( bpp );
    } catch( std::string a_error )
    {
        std::cerr << a_error << std::endl;
        return ;
    }
    std::cout << "DEBUG: " << "images: " << images << " w: " << width << " h: " << height << " bpp: " << bpp << std::endl;

    std::unique_ptr< sepia::Writer > data = std::unique_ptr< sepia::Writer >( new sepia::Writer( m_name, images, width, height, bpp ) );


    int more = 0; // more data available if true.
    while( !m_terminate )
    {
        for( int i = 0; i < data->getGroupHeader()->count; i++ )
        {
            sock.receive( *data->getHeader( i ) );
            sock.receive( data->getAddress( i ), data->getSize( i ) );
        }
        data->update();
    }
}
int main()
{
    TcpServer server;

    if (server.listen(2000) != AbstractSocket::Done)
    {
        perror("Error :");
        return -1;
    }

    TcpSocket* s = server.accept();

    if (s == NULL)
    {
        cout << "Error: cannot accept conection" << endl;
        return -1;
    }

    cout << "Connected client" << endl;
    int i = 4;
    i++;
    cout << "Remote host : " << s->getRemotePort() << endl;
    cout << s->getLocalPort() << endl;
    cout << server.getRemoteAddress().toString() << endl;
    cout << s->getRemoteAddress().toString() << endl;
    s->sendInt32(10);
    sleep(2);
    s->sendString("hello");
    s->sendCharArray("world", 5);
    Frame f;
    f << "This is a frame " << 666 << '\n';

    if (s->sendFrame(f) != AbstractSocket::Done)
        perror("error on send frame");

    delete s;
    server.close();
    s->close();
    return 1;
}