VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-dgram-createsocket-method/

⇱ Node.js dgram.createSocket() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js dgram.createSocket() Method

Last Updated : 6 Apr, 2023

The dgram.createSocket() method is an inbuilt application programming interface within dgram module which is used to create the dgram.socket object.

Syntax:

const dgram.createSocket(options[, callback])

Parameters: This method takes the following parameter:

  • options: It will use one of the following options- 
    • type: The family of sockets.
    • reuseAddr: Boolean value if the address has been reused.
    • ipv6Only: Setting ipv6Only to true will disable dual-stack support.
    • recvBufferSize: Sets the SO_RCVBUF socket value.
    • sendBufferSize: Sets the SO_SNDBUF socket value.
    • lookup: Custom lookup function.
  • callback: callback function for further operation.

Return Value: This method returns the object of dgram.socket.

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

Filename: index.js

Output:

12345
UDP String: Hello

Example 2: In this example, we will see the use of dgram.createSocket() Method

Filename: index.js

Output:

server listening 0.0.0.0:1234
1234567
server is created
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_dgram_createsocket_options_callback

Comment
Article Tags:

Explore