Socket programming err

I used some mature codes of socket in C++ to construct a socket communication.
Here is part of my codes:
[Socket.h] part:
[color=olive]class clsSocket {
public:
clsSocket();
virtual ~clsSocket();

// Server initialization
bool create();
bool bind( const int port );
bool listen() const;
bool accept( clsSocket& ) const;

// Client initialization
bool connect( const std::string host, const int port );

// Data Transmission
bool send( const std::string ) const;
int recv( std::string& ) const;

void set_non_blocking( const bool );

bool is_valid() const { return m_sock != -1; }

private:
int m_sock;
sockaddr_in m_addr;
};

class clsClientSocket : private clsSocket
{
public:
clsClientSocket( std::string host, int port );
virtual ~clsClientSocket() {};

const clsClientSocket& operator << ( const std::string& ) const;
const clsClientSocket& operator >> ( std::string& ) const;

};

[Socket.cpp] part:
[color=olive]clsSocket::clsSocket() : m_sock( -1 )
{
printf( “Initial m_sock is %d.\n”, m_sock );
memset( &m_addr, 0, sizeof(m_addr) );
}

clsSocket::~clsSocket()
{
if ( is_valid() )
::close( m_sock );
}

bool clsSocket::create()
{
m_sock = socket( AF_INET, SOCK_STREAM, 0 );

if ( !is_valid() )
	return false;

if ( !is_valid() )
	return false;

// TIME_WAIT - argh
int on = 1;
if ( setsockopt( m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on) ) == -1 )
return false;
else
return true;
}

bool clsSocket::bind( const int port )
{
if ( !is_valid() )
{
return false;
}

m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons( port );

// int bind_return = ::bind( m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr) );
int bind_return = ::bind( m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr) );
if ( bind_return == -1 )
{
return false;
}
return true;
}

bool clsSocket::listen() const
{
if ( !is_valid() )
{
return false;
}

int listen_return = ::listen( m_sock, MAXCONNECTIONS );
if ( listen_return == -1 )
{
	return false;
}
return true;

}

bool clsSocket::accept( clsSocket& new_socket ) const
{
int addr_length = sizeof( m_addr );
new_socket.m_sock = ::accept( m_sock, (sockaddr *)&m_addr, (socklen_t *)&addr_length );

if ( new_socket.m_sock == -1 )
	return false;
else
	return true;

}

bool clsSocket::send( const std::string s ) const
{
int status = ::send( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
if ( status == -1 )
return false;
else
return true;
}

/*

  • Implementation of clsClientSocket
    */
    clsClientSocket::clsClientSocket( std::string host, int port )
    {
    if ( ! clsSocket::create() )
    {
    // throw SocketException ( “Could not create client socket.” );
    printf( “Could not create client socket.\n” );
    }

    if ( ! clsSocket::connect(host, port) )
    {
    // throw SocketException ( “Could not bind to port.” );
    printf( “Could not bind to port.\n” );
    }

}

// TIME_WAIT - argh
int on = 1;
if ( setsockopt( m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on) ) == -1 )
return false;
else
return true;
}

bool clsSocket::bind( const int port )
{
if ( !is_valid() )
{
return false;
}

m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons( port );

// int bind_return = ::bind( m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr) );
int bind_return = ::bind( m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr) );
if ( bind_return == -1 )
{
return false;
}
return true;
}

bool clsSocket::listen() const
{
if ( !is_valid() )
{
return false;
}

int listen_return = ::listen( m_sock, MAXCONNECTIONS );
if ( listen_return == -1 )
{
	return false;
}
return true;

}

bool clsSocket::accept( clsSocket& new_socket ) const
{
int addr_length = sizeof( m_addr );
new_socket.m_sock = ::accept( m_sock, (sockaddr *)&m_addr, (socklen_t *)&addr_length );

if ( new_socket.m_sock == -1 )
	return false;
else
	return true;

}

bool clsSocket::send( const std::string s ) const
{
int status = ::send( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
if ( status == -1 )
return false;
else
return true;
}

/*

  • Implementation of clsClientSocket
    */
    clsClientSocket::clsClientSocket( std::string host, int port )
    {
    if ( ! clsSocket::create() )
    {
    // throw SocketException ( “Could not create client socket.” );
    printf( “Could not create client socket.\n” );
    }

    if ( ! clsSocket::connect(host, port) )
    {
    // throw SocketException ( “Could not bind to port.” );
    printf( “Could not bind to port.\n” );
    }

}

[main.cpp] part:
[color=olive]clsClientSocket client_socket ( “localhost”, 30000 );

errors:
[color=red]undefined reference to “bind”;
undefined reference to “connect”;
undefined reference to “listen”;
undefined reference to “recv”;
undefined reference to “send”;
undefined reference to “socket”;

in linker options set “-lsocket”

Thx, get thru. Could u plz give me a further explanation on this? Thank u.

IDE->project properties->tab “Linker”->field “Other options”->type -lsocket

The Socket library is not included by default in the search for modules by the linker. The historical reason for this, unlike say Unix, is probably that the preferred way to communicate between QNX processes is with message passing.

thx, i made it and successfully built the project. And could u explain the mechanism of the lsocket to me? Thank u.

lsocket refers to the library file /usr/libsocket.a or /usr/libsocket.so. The former is a library with the socket routines, and the latter a library of stubs that connect to a dll. By including -lsocket, you tell the linker to search these library files when linking your program. Since your program has call to the Socket interface, without including this file, the linker can’t resolve the names and you get errors. I don’t think there is room here to explain all details of compiling, linking, and loading a module, but I’m sure you find out all you need by going to Wikopedia, or even just googling the three relevant words.

Thank u, maschoen.
Currently i am constructing an adhoc among several QNX machines, probably with other Linux machines later. So what kind of mechanism i should use to realize wireless communication? Message passing or socket? Thank u.

It not depened of mechanism that you want to use, it depend from net driver, that will tranfer your messages(transfer may be on TCPIP or MAC addresing) …
cuter say if you have good driver for wireless connection – you in game :slight_smile:

Thx. But i’m afraid i’m not following u. If i have good wireless net driver support, then how about the two methods?

You may use 2 methods together! Net packets same in all methods – it’s ETHERNET PACKS!

Between 2 QNX machines, generally message passing is more efficient and easier. Between QNX and Linux or Linux and Linux you will need to use TCP/IP. Depending on your application it might be less complex to just use TCP/IP.

The wireless issue is a completely separate issue. There doesn’t seem to be a lot of wireless support, although there’s been some talk lately in these forums.

Now here’s a wrinkle you might be interested in. There’s an open source product called “Simpl”. It creates a QNX like message passing layer on top of TCP/IP so that Linux and Windows and QNX and any other machine that has a port can communicate using message passing.

Maschoen, as far as I know Simpl only works on Linux and doesn’t support networking.

If by networking, you mean transparent resource access, you are correct. If you mean that two Linux machines with Simpl running cannot communicate, then you are incorrect. Cross OS communication is one of Simpl’s highlights.

QNX 4 had a special Simpl version, since it already has message passing. I ported the Linux version to QNX 6.3 with minimal trouble. It does not use native message passing however.

I’ve heard about a Windows port too, but haven’t had any direct experience. Anything close to Posix with TCP/IP should be an easy port.

What if the case that with many QNX machines in a wireless network working at the same time, while some routing protocols are needed? How does the message passing deal with the stuff in network layer?
Considering the expandability and robustness of the overall multi-agent system, then i should adopt the msg passing method (with all QNX machines in the network)?

What if the case that with many QNX machines in a wireless network >working at the same time, while some routing protocols are needed?
How does the message passing deal with the stuff in network layer?

QNET usually is the Network layer. It is not a routing protocol, so nodes must all be in the same physical network. It can be run on top of TCP/IP in which IP provides the routing.

Considering the expandability and robustness of the overall multi-agent >system, then i should adopt the msg passing method (with all QNX >machines in the network)?

If your nodes are QNX nodes and all are on the same physical network, then there are a number of advantages to QNET. The overhead is lower, and it provides transparent access to resources such as files and devices.

By the way for great C++ Classes for handling TCP:IP check the poco library (google it up )

No routing needed. It is one shared media.

It doesn’t even know there is a network layer. QNet extends the kernels name resolution mechanism, so that opening a prefix is identical for both the networked and local cases.

Certainly. If you have the option to make all nodes QNX, then it will be the easiest development environment (since you won’t need to write a single line of code to handle the network case - the O/S will handle it all for you). Even if you need to have a few non-QNX nodes, it would most likely still be a win to handle the non-QNX nodes with a gateway that registers a global name in the QNX name space (see: gns in the docs).

Thx for your explicit explanation, rqallen. May consider this case: node A has 3 neighbors B,C, D. And more than one hop away from A is E, F. Then if A wants to talk to E, then it has to relay its msg to B or C or D with a predefined cost function, then say, B may relay the data to E (E is within one hop away from B). So routing should be considered now. Am i right? Thank u.

A wireless hop is NOT a routing thing. It’s taken care of by the wireless protocol is is totaly transpartent to any high level protocol.