Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing is sending data and the other thing is receiveing it. Since UDP protocol is bidirectional, then actually there is no strict difference between client and server. So your server and client code will be almost the same, the difference is that actually one will send packets and other will only respond. Also note that you have an infinite loop, because your probably using <code>.send</code> with <code>PORT</code> and <code>HOST</code> variables and you have to send to different host/port pair.</p> <p>Here's an example:</p> <p><strong>server</strong></p> <pre><code>var host = "127.0.0.1", port = 33333; var dgram = require( "dgram" ); var server = dgram.createSocket( "udp4" ); server.on( "message", function( msg, rinfo ) { console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg ); server.send( msg, 0, msg.length, rinfo.port, rinfo.address ); // added missing bracket }); server.bind( port, host ); </code></pre> <p><strong>client</strong></p> <pre><code>// NOTE: the port is different var host = "127.0.0.1", port = 33334; var dgram = require( "dgram" ); var client = dgram.createSocket( "udp4" ); client.on( "message", function( msg, rinfo ) { console.log( "The packet came back" ); }); // client listens on a port as well in order to receive ping client.bind( port, host ); // proper message sending // NOTE: the host/port pair points at server var message = new Buffer( "My KungFu is Good!" ); client.send(message, 0, message.length, 33333, "127.0.0.1" ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload