[OpendTect_Developers] Implementation of TCP/IP communication in OpendTect

Ranojay Sen ranojay.sen at dgbes.com
Mon Apr 4 09:14:07 CEST 2011


Dear All,

     As you all might know multi-machine processing of seismic attribute 
volumes is back in OpendTect 4.2, which was omitted in the previous 
version due to lack of support from Microsoft for executing remote 
commands in Windows Vista and Windows 7. We made a workaround to this 
problem by implementing socket communication classes for establishing 
connection between several computers connected in a network. All of this 
is done in the newly added Network module in OpendTect, which also 
contains some other network interface classes for HTTP, FTP along with 
TCP/IP. Our implementation is built around Qt's classes as usual, we 
made a wrapper around these classes and managed to trigger and receive 
call backs from them. In this discussion I will state the TCP/IP 
communication in brief, show you how to use the classes from Network 
module with example source codes.
     To start with TCP/IP, it has two parts, the server and the client. 
The server listens to a an IP address to a particular port. This scheme 
is similar to finding a room in a building. The IP address is the actual 
physical address of the computer in the network, and the port is the 
"room number" in the building where the application is listening to. The 
other part is the client, which connects to the server and then both the 
client and the server can exchange information via socket.
      For the source codes, I have attached two files with this post, 
which are very simple to understand. I explain them here too. One is the 
server and it listens to a port, the other is the client. The client 
connects to the server and sends values from 1 to 100 and the server 
relays them back to the client. these two programs can be on different 
machines in a network.

////////////////////////// TCP Server ///////////////////////
#include "callback.h"

#include "systeminfo.h"
#include "tcpserver.h"
#include <iostream>

#include <QCoreApplication>


class ODTCPServer : public CallBacker
{
public:
             ODTCPServer();
             ~ODTCPServer();

     void        listen( int );

protected:
     void        dataReceivedCB(CallBacker*);

     TcpServer&        server_;  // The TCP server opeject
};



ODTCPServer::ODTCPServer()
     : server_(*new TcpServer)
{
     server_.readyRead.notify( mCB(this,ODTCPServer,dataReceivedCB) ); 
  // Notify is something has arrived;
}


ODTCPServer::~ODTCPServer()
{
     delete &server_;
}


void ODTCPServer::listen( int port )
{
     server_.listen( System::localAddress(), port );  // listen the 
machine on the port;
}


void ODTCPServer::dataReceivedCB( CallBacker* cb ) // do if some thing 
has arrived.
{
     mCBCapsuleUnpack(int,id,cb); // the server can listen to multiple 
cliets, each assigned an id during its connection
     BufferString bs;
     server_.read( id, bs );
     std::cout << bs.buf() << std::endl;
     server_.write( id, bs );
}


int main( int argc, char** argv )
{
     QCoreApplication app( argc, argv );
     ODTCPServer server;
     server.listen( 5050 );  // listen to port 5020;
     app.exec();
}

/////////////////////////// TCP Client ////////////////////////

#include "callback.h"
#include "tcpsocket.h"

#include <iostream>

class ODTCPSocket : public CallBacker
{
public:
             ODTCPSocket();
             ~ODTCPSocket();

     void        connect(const char* host, int port);
     void        doWork();

protected:
     void        dataReceivedCB(CallBacker*);
     void        connectedCB(CallBacker*);

     TcpSocket&        socket_;  // the client socket.
};



ODTCPSocket::ODTCPSocket()
     : socket_(*new TcpSocket)
{
     socket_.connected.notify( mCB(this,ODTCPSocket,connectedCB) ); // 
If connected to the server;
     socket_.readyRead.notify( mCB(this,ODTCPSocket,dataReceivedCB) ); 
// Data arrived;
}


ODTCPSocket::~ODTCPSocket()
{
     socket_.readyRead.remove( mCB(this,ODTCPSocket,dataReceivedCB) );
     delete &socket_;
}


void ODTCPSocket::connect( const char* host, int port )
{
     socket_.connectToHost( host, port );
     socket_.waitForConnected( -1 ); // wait for the connection 
establishment.
}


void ODTCPSocket::connectedCB( CallBacker* )
{
     std::cout << "Connected ... " << std::endl;
}


void ODTCPSocket::doWork()
{
     for ( int idx=0; idx<100; idx++ )
     {
     BufferString data;
     data = idx;
     socket_.write( data );
     }
}


void ODTCPSocket::dataReceivedCB( CallBacker* cb )
{
     BufferString datain;
     socket_.read( datain );
     std::cout << datain << std::endl;
}


int main( int argc, char** argv )
{
     ODTCPSocket* socket = new  ODTCPSocket;
     socket->connect( "192.168.4.105", 5020 );  // machine IP and port.. 
5020 as the server is listenning to 5020
     socket->doWork();
}

///////////////////////////////end////////////////////////

As always, any questions are welcome.

Regards
Ranojay

-- 
Ranojay Sen
dGB Earth Sciences - India
304, Gateway Plaza
Hiranandani Gardens, Powai
Mumbai-400076
India
Phone: +91 22 25704984
Mobile: +91 9930834087

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: od_tcpclient.cc
URL: <http://lists.opendtect.org/pipermail/developers/attachments/20110404/8bf628a9/attachment.cc>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: od_tcpserver.cc
URL: <http://lists.opendtect.org/pipermail/developers/attachments/20110404/8bf628a9/attachment-0001.cc>


More information about the Developers mailing list