Created
October 4, 2019 16:40
-
-
Save Gumball12/5994b350fc02f6b1548e5dfc7dc71c58 to your computer and use it in GitHub Desktop.
tcp/udp socket program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* tcp socket programming (client-side) | |
* @author shj | |
*/ | |
// import module | |
const net = require('net'); | |
// create TCP socket | |
const client = new net.Socket(); | |
// try make a connection to server (target: 127.0.0.1, port: 8888) | |
client.connect({ port: 8888, host: '127.0.0.1' }, () => { // when connected | |
// print message | |
console.log('connected to server'); | |
// send message to server | |
client.write('hello server'); | |
// when server sends a message | |
client.on('data', (chunk) => { | |
// print message | |
console.log('client got a message >> ', chunk.toString()); | |
}); | |
// when connection is over | |
client.on('end', () => { | |
// print message | |
console.log('connection is over'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* tcp socket programming (server-side) | |
* @author shj | |
*/ | |
// import module | |
const net = require('net'); // for tcp socket programming | |
// create TCP server | |
const server = new net.Server(); | |
// listening for client connection (port: 8888) | |
server.listen(8888, () => { | |
console.log('server listening...'); | |
}); | |
// when connected | |
server.on('connection', (sock) => { // get client socket | |
// send message | |
sock.write('connected with server'); | |
// when client sends a message | |
sock.on('data', (chunk) => { // chunk: message(Buffer type) | |
// print message | |
console.log('server got a message >> ', chunk.toString()); | |
}); | |
// when connection is over | |
sock.on('end', () => { | |
// print message | |
console.log('connection is over'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* udp socket programming (client-side) | |
* @author shj | |
*/ | |
// import module | |
const dgram = require('dgram'); | |
// create client socket | |
const client = dgram.createSocket('udp4'); | |
// create a message | |
const message = Buffer.from('message from client!'); | |
// init target | |
const target = '54.146.216.8'; // AWS EC2 instance | |
// send message (msg, offset, length, port, address, callback) | |
client.send(message, 0, message.length, 33333, target, (err, bytes) => { | |
// print message | |
console.log(`send message: ${message}`); | |
// close client socket | |
client.close(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* udp socket programming (server-side) | |
* @author shj | |
*/ | |
// import module | |
const dgram = require('dgram'); // for transport datagram packet | |
// create server socket | |
const server = dgram.createSocket('udp4'); | |
// error handling | |
server.on('error', (err) => { | |
// print error | |
console.log(`server error with >>\n${err.stack}`); | |
server.close(); // close server socket | |
}); | |
// message handling | |
server.on('message', (msg, rinfo) => { | |
// print message | |
console.log(`server got a message >>\n${msg} from ${rinfo.address}:${rinfo.port}`); | |
}); | |
// when init handling | |
server.on('listening', () => { | |
// print message | |
console.log('server listening...'); | |
}); | |
// start listening | |
server.bind(33333); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment