InterProcessConnection, DatagramSocket help needed

Hello kind people,
I am trying to make 2 PCs to communicate via JUCE InterProcessConnection and DatagramSocket classes over the local network. Lets name the computers A and B.

I need to send data from A to B, so I set up a InterProcessConnectionServer on the B side and wait for a connection with beginWaitingForSocket on defined port and IP Address. On the A side I try to connect to that socket and it works. By that I have managed to send some messages from A to B. Now the thing is that I want to send my data as UDP packets,connectionless,from A to B but using the other port,not the same one as for interprocess. When I press the button on A side to send data, it first sends a message to B side to let it know to expect a data to come. On B side in messageReceived() it recognizes that message, binds its DatagramSocket to the correct port and calls a function that will wait for socket to be ready to read, read the data,process it and repeat it as long as the data is incoming. But the problem is that it constantly crashes somewhere in memory where I cannot reach by debugger. I’ll share some code below:

—Setting up the server on B side—

class Server :public InterprocessConnectionServer
{
 public:
    InterprocessConnection* createConnectionObject() override
    {
    	return process;
    }
    void addProcess(InterprocessConnection* p)
    {
    	process = p;
    }
private:
    InterprocessConnection* process;
};

Then, I create an instance of Server in my Component which is derived from InterProcessConnection. In its constructor I have:

server.addProcess(this);
server.beginWaitingForSocket(5001, "127.0.0.1");

Then,

void MainComponent::messageReceived(const MemoryBlock &message)
{
     String receivedMessage = message.toString();
     if (receivedMessage.contains("connectUDP"))
	{
		if(udpSocket.getBoundPort()!=5000)
			udpSocket.bindToPort(5000, "127.0.0.1");
		connected = true;
		receiveDataAndLog();
	}
}//<--------sometimes crashes here on debugging for writing access violation

Where,

void MainComponent::receiveDataAndLog()
{
	if (connected)
	{
		unsigned char* msg_data;

		int bytesReceived = 0;
		String ipAddress;
		int sendport;
		
		if (udpSocket.waitUntilReady(true, 1000))
		{
			bytesReceived = udpSocket.read(msg_data, packetByteLength, false, ipAddress, sendport);
			
			if (bytesReceived > 0)
			{
				//..do something
			}
			
		}
	}
}

On A side I use write function of its DatagramSocket and send it to the port and ip address of B side, data arrives, but then it crashes.

Is this a possible way to do something like this, did I set up the IPC server correctly? I have tried creating IPC with callbacksOnMessageThread set to both true and false but neither worked.
I have limited knowledge in this area, everything is still pretty new to me so any tips regarding socket communication is more than welcome. If you would like me to share more info I would gladly do it!
Thanks in advance,
Cila

Is it maybe something going wrong because I am testing this on the same computer, where for the receiving end I use an IP address 127.0.0.1?

127.0.0.1 is the loopback device, so you cannot be reached from anywhere other than the machine itself.
If you listen on 0.0.0.0 instead (which is a netmask matching any IPv4 address), you should be reachable from any machine in the subnet (or worldwide, if you have a public IP address).

Fun fact: google for “127.0.0.1 hack” :smiley:

When I put 2 different PCs on my LAN, A would have, for an example, 192.168.1.5, while B would be 192.168.1.2, and then I would let B listen on port 5001 on address 192.168.1.2 for IPC, and then bind DatagramSocket to port 5000 on the same address 192.168.1.2 and wait for data from 192.168.1.5? Something like that should work?

But while I am testing it,I am using one computer, and trying to see if it works,so basically I am sending from 127.0.0.1 to 127.0.0.1, can that cause some troubles?

Hahaha! I am still finding all those IP address things a bit abstract, but I guess it is fine, I have started getting into it 2 days ago :smiley:

Yes, on each interface you can bind a port for listening only once. No matter if you refer to the interface as your public IP address or as 0.0.0.0 (if you have more than one adapter connected, e.g. WIFI, it might silently use the other). But if your system is properly setup, it will avoid connecting twice into the same net or subnet.
So if your test program is already using that port, your other listen call will fail.

Wait, so I have to bind my DatagramSocket to a port each time I expect a data to come, or I can bind it only once on the start of transmission?

Unfortunately I am not very versed with DatagramSockets… but from reading the connection in DatagramSockets is only available during each packet, and free at any other time.

https://www.reddit.com/r/ProgrammerHumor/comments/14wv9p/i_was_gonna_tell_you_guys_a_joke_about_udp/

1 Like

Also this part in the B PCs is really annoying me, because everytime I debug, sender IP address is some random address,not the one that I am really sending from. I dont know where my packets are getting lost.
Ill try it when I get back home, on 2 PCs, maybe it will work

Thank you very much for your time and effort, I will try it again at home.:slight_smile:

If you want to get a working example of UDP datagram sockets look into the JUCE codes of NetworkServiceDiscovery struct and its Advertiser and AvailableServiceList classes.

Okay, so I have managed to fix my crashes. Apparently the logic of transmission was good, just that UDP packet receiving shall be done on separate Thread from IPC. Now it works fine and UDP packets are arriving on the destination. :slight_smile: