Problem with using DatagramSocket class

Hey Jules,
I am new to socket programming and trying to transfer data between two applications through UDP Socket.

I have two applications, say Application A and Application B.
Application A sends data in a specified protocol format through UDP at a particular Port ID and Application B receives the data from the same Port ID. I am using DatagramSocket class in both the applications.Everything works fine till it comes to reading the data packets from Application A, Application B is not able to bind with the port to which Application A is connected to.

Application A

unsigned char us8Data;
DatagramSocket*	_pDatagramSocket;
ApplicationA::ApplicationA()
{
	_pDatagramSocket     =  new DatagramSocket( const int localPortNumber );
	us8Data  = 1;
}
bool ApplicationA::statusOfConnectToPort()
{
	if ( _pDatagramSocket->connect( const String& remoteHostname,const int remotePortNumber ) == true )
	{
		startThread( 1 );
		return true;
	}
	else
	{
		return false;
	}
}
void ApplicationA::run( )
{
	while( ! threadShouldExit() )
	{
		_pDataGramWriteSocket->write( ( void* ) &us8Data  , 1 );	
		if( us8Data  < 255 )
		{
			us8Data  += us8Data  ;
		}
		else
		{
			us8Data  = 1;
		}			
	}
}

Application B

DatagramSocket*	_pDatagramSocket;
ApplicationB::ApplicationB()
{
	_pDatagramSocket     =  new DatagramSocket( const int localPortNumber );
}
bool ApplicationB::statusOfBindToPort()
{	
	if( _pDatagramSocket->bindToPort( const int localPortNumber ) == true )
	{			
		startThread( 1 );
		return true;	
	}
	else
	{
		return false;
	}	
}
void ApplicationB::run( )
{
	while( ! threadShouldExit() )
	{
		unsigned char us8DestBuffer;
		int iReadyForReading	 = _pDatagramSocket->waitUntilReady( false , 1000 );
		int iMaxBytesRead	 = _pDatagramSocket->read( ( unsigned char* ) &us8DestBuffer , 1 , true );		
	}
}

I wish if you could help me here.

Regards


must be

[code]int iReadyForReading    = _pDatagramSocket->waitUntilReady( true , 1000 );[/code]

must be

Thanks dev!! But the problem is not here…I am not able to bind to the port to which Application A is been connected to…[quote]bindToPort( const int portID ) always return false.In this case my thread never starts and ::run () code never get executed where I am trying to read datagrams [/quote]

I am not able to understand which part of my code is faulty.So, if any one could suggest me something it will be really helpful.

Thanks and Regards!!

Two applications on the same machine trying to bind to the same port? Well, that’s a bit silly first of all, like a hosepipe looped back and connected to the same spigot.

The OS is probably telling you to go away since it doesn’t make sense. Try each app on a separate computer. There is a socket option - something like SO_REUSE that tells the OS to allow multiple sockets to bind to the same port, but I still doubt it will work the way you want - all the data will probably show up in one of the sockets, and you’ll have to examine the packets and work out what sent them.

What are you actually trying to do? Each socket actually has more than one port - it has a send port, the port it’s bound to, and a possible receive port. In the case of UDP (Datagrams, as Juce terms it), you barely need to bind - you just send to a specific port number.

But anyway - if you’re making an app to run on different machines, it may well be working now!

Bruce