VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-socket-send-method/

⇱ Node.js socket.send() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js socket.send() Method

Last Updated : 11 Apr, 2023

The socket.send() method is an inbuilt application programming interface of class Socket within dgram module which is used to send the message from one socket to another.

Syntax:

socket.send(msg[, offset, length][, port][, address][, callback])

Parameters: This method takes the following parameter:

  • msg: Message to be sent.
  • offset: Offset in the buffer where the message starts.
  • length: Number of bytes in the message.
  • port: Destination port.
  • address: Destination hostname or IP address.
  • callback: Called when the message has been sent.

Return Value: This method does not return any value.

Example 1: In this example, we will see the use of socket.send() Method

Filename: index.js

Output:

[ 'message' ]
UDP String: Hello

Example 2: In this example, we will see the use of a socket.send() Method

Filename: index.js

Output:

server listening 0.0.0.0:1234
[ 'message', 'listening' ]
message sent
UDP String: Hello

Run the index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/dgram.html#dgram_socket_send_msg_offset_length_port_address_callback

Comment
Article Tags:

Explore