www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sending UDP packets via the internet and between processes?

reply Dustin Keeting <dksdsfd gmail.com> writes:
I'm setting up two applications, a client and a server, that will 
exchange some data back and forth. The server may be on the same 
machine as the client, or it may not be, or there might be 
multiple clients which may or may not be on the same machine. 
Both applications will be running other code and I'd like them to 
be able to access a list of received packets rather than waiting 
for them to come in.

What do I need to do to set this up? This is as far as I've got:

         ushort port = 3000;

	auto sender = new UdpSocket();
	sender.blocking = false;
	auto address = new InternetAddress(port);
	sender.bind(address);

	ubyte[Packet.sizeof] bytes;
	Packet* packet = cast(Packet*)bytes.ptr;
         //not sure how to convert a struct to big endian easily?

	sender.send(bytes);

I'm not really sure how to receive sent bytes or if I need to put 
in ip addresses to make it work online? I've looked at a couple 
of examples and I've gotten some html to work by accessing a 
localhost port, but I've always struggled with networking, so if 
someone can help explain how I'm meant to connect sockets that 
may be online or on the same machine... that would be a big help!
May 12 2018
parent EHuckert <edgar.huckert huckert.com> writes:
On Saturday, 12 May 2018 at 09:29:43 UTC, Dustin Keeting wrote:
 I'm setting up two applications, a client and a server, that 
 will exchange some data back and forth. The server may be on 
 the same machine as the client, or it may not be, or there 
 might be multiple clients which may or may not be on the same 
 machine. Both applications will be running other code and I'd 
 like them to be able to access a list of received packets 
 rather than waiting for them to come in.

 What do I need to do to set this up? This is as far as I've got:

         ushort port = 3000;

 	auto sender = new UdpSocket();
 	sender.blocking = false;
 	auto address = new InternetAddress(port);
 	sender.bind(address);

 	ubyte[Packet.sizeof] bytes;
 	Packet* packet = cast(Packet*)bytes.ptr;
         //not sure how to convert a struct to big endian easily?

 	sender.send(bytes);

 I'm not really sure how to receive sent bytes or if I need to 
 put in ip addresses to make it work online? I've looked at a 
 couple of examples and I've gotten some html to work by 
 accessing a localhost port, but I've always struggled with 
 networking, so if someone can help explain how I'm meant to 
 connect sockets that may be online or on the same machine... 
 that would be a big help!
You may look at my Web page http://www.huckert.com/ehuckert/huckertp112.html under the heading "Sockets". You will find there a working UDP sender (a client) and a working receiver (a server). Both programs are written in D. I use UDP heavily for logging. For the communication between two processes on the same machine I would rather recommend TCP instead of UDP. To connect sockets on the same machine it is normally sufficient to use "localhost" as the hostname or "127.0.0.1". From time to time I encounter a small problem: the firewalls on the machines may block the communication on the ports. For tests I prefer switching off the firewalls. E.Huckert
May 12 2018