Esempio n. 1
0
void InetWvIn :: listen( int port, unsigned int nChannels,
                         Stk::StkFormat format, Socket::ProtocolType protocol )
{
    mutex_.lock();

    if ( connected_ ) delete soket_;

    if ( nChannels < 1 ) {
        oStream_ << "InetWvIn()::listen(): the channel argument must be greater than zero.";
        handleError( StkError::FUNCTION_ARGUMENT );
    }

    if ( format == STK_SINT16 ) dataBytes_ = 2;
    else if ( format == STK_SINT32 || format == STK_FLOAT32 ) dataBytes_ = 4;
    else if ( format == STK_FLOAT64 ) dataBytes_ = 8;
    else if ( format == STK_SINT8 ) dataBytes_ = 1;
    else {
        oStream_ << "InetWvIn(): unknown data type specified!";
        handleError( StkError::FUNCTION_ARGUMENT );
    }
    dataType_ = format;

    unsigned long bufferBytes = bufferFrames_ * nBuffers_ * nChannels * dataBytes_;
    if ( bufferBytes > bufferBytes_ ) {
        if ( buffer_) delete [] buffer_;
        buffer_ = (char *) new char[ bufferBytes ];
        bufferBytes_ = bufferBytes;
    }

    data_.resize( bufferFrames_, nChannels );
    lastFrame_.resize( 1, nChannels, 0.0 );

    bufferCounter_ = 0;
    writePoint_ = 0;
    readPoint_ = 0;
    bytesFilled_ = 0;

    if ( protocol == Socket::PROTO_TCP ) {
        TcpServer *socket = new TcpServer( port );
        oStream_ << "InetWvIn:listen(): waiting for TCP connection on port " << socket->port() << " ... ";
        handleError( StkError::STATUS );
        fd_ = socket->accept();
        if ( fd_ < 0) {
            oStream_ << "InetWvIn::listen(): Error accepting TCP connection request!";
            handleError( StkError::PROCESS_SOCKET );
        }
        oStream_ << "InetWvIn::listen(): TCP socket connection made!";
        handleError( StkError::STATUS );
        soket_ = (Socket *) socket;
    }
    else {
        soket_ = new UdpSocket( port );
        fd_ = soket_->id();
    }

    connected_ = true;

    mutex_.unlock();
}
Esempio n. 2
0
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;
}