A simple udp broadcast server and client implemented by Nodejs, nodejsudp
It is quite simple to send udp broadcasts to nodejs. First, we need to write a server to receive broadcast data. The Code is as follows:
Copy codeThe Code is as follows:
Var dgram = require ("dgram ");
Var server = dgram. createSocket ("udp4 ");
Server. on ("error", function (err ){
Console. log ("server error: \ n" + err. stack );
Server. close ();
});
Server. on ("message", function (msg, rinfo ){
Console. log ("server got:" + msg + "from" +
Rinfo. address + ":" + rinfo. port );
});
Server. on ("listening", function (){
Var address = server. address ();
Console. log ("server listening" +
Address. address + ":" + address. port );
});
Server. Binary (41234 );
Then write a client program to send a broadcast message. The Code is as follows:
Copy codeThe Code is as follows:
Var dgram = require ("dgram ");
Var socket = dgram. createSocket ("udp4 ");
Socket. bind (function (){
Socket. setBroadcast (true );
});
Var message = new Buffer ("Hi ");
Socket. send (message, 0, message. length, 41234, '2017. limit 255', function (err, bytes ){
Socket. close ();
});
Note that socket. setBroadcast (true); can be called only after the socket is successfully bound; otherwise, an Error: setBroadcast EBADF is reported.
It is quite simple for the client to send a broadcast. It is okay to set the data to be sent, the port, and other information.
Nodejs can be used to implement a simple interface server.
Var http = require ('http'); var mysql = require ('mysql'); var connection = mysql. createConnection ({host: 'localhost', user: 'me', password: 'secret',}); // start your mysql connection. connect (); var server = http. createServer (function (req, res) {// if you send a GET request to 127.0.0.1: 1337/test? Var url_info = require ('url') If a = 1 & B = 2 '). parse (req. url, true); // check whether requestif (url_info.pathname = '/test') for/test {// use url encode for query, in this way, you can use post to send var post_data = require ('querystring '). stringify (url_info.query); // optionvar post_options of post = {host: 'localhost', port: 1337, path: '/response_logic', method: 'post', headers: {'content-type': 'application/x-www-form-urlencoded ', 'content-length': post_data.length}; // issue the postvar request_made = http. request (post_options, function (response_received) {var buf_list = new Array (); response_received.on ('data', function (data) {buf_list.push (data );}); response_received.on ('end', function () {var response_body = Buffer. concat (buf_list); res. end (response_body); connection. query ('insert ......... ', function (err, rows, fields) {// process your result}) ;}); // send the post's datare ...... remaining full text>
A program for sending and receiving data through UDP broadcast? It is best to describe how to implement the principle in detail?
// Server
// Server. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <WinSock2.h>
# Include <stdio. h>
# Include <iostream>
Using namespace std;
# Pragma comment (lib, "ws2_32.lib ")
Const int MAX_BUF_LEN = 255;
Int _ tmain (int argc, _ TCHAR * argv [])
{
WORD wVersionRequested;
WSADATA wsaData;
Int err;
// Start the socket api
WVersionRequested = MAKEWORD (2, 2 );
Err = WSAStartup (wVersionRequested, & wsaData );
If (err! = 0)
{
Return-1;
}
If (LOBYTE (wsaData. wVersion )! = 2 |
HIBYTE (wsaData. wVersion )! = 2)
{
WSACleanup ();
Return-1;
}
// Create a socket
SOCKET connect_socket;
Connect_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP );
If (INVALID_SOCKET = connect_socket)
{
Err = WSAGetLastError ();
Printf ("/" socket/"error! Error code is % d/n ", err );
Return-1;
}
SOCKADDR_IN sin;
Sin. sin_family = AF_INET;
Sin. sin_port = htons (3779 );
Sin. sin_addr.s_addr = INADDR_BROADCAST;
Bool bOpt = true;
// Set this socket to broadcast type
Setsockopt (connect_socket, SOL_SOCKET, SO_BROADCAST, (char *) & bOpt, sizeof (bOpt ));
Int nAddrLen = sizeof (SOCKADDR );
Char buff [MAX_BUF_LEN] = "& q ...... remaining full text>