-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPServer.js
31 lines (25 loc) · 1.11 KB
/
TCPServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var net = require( 'net' );
var TCPServer = net.createServer( function ( socket ) {
console.log( 'Someone Connects' );
TCPServer.maxConnections = 3;
TCPServer.getConnections( function( err, count ) {
console.log( 'The count of client is ' + count );
});
var address = 'RemoteIP : ' + socket.remoteAddress + ' RemoteFamily : ' + socket.remoteFamily;
address = address + ' RemotePort : ' + socket.remotePort + ' LocalAddress : ' + socket.localAddress;
address = address + ' LocalPort : ' + socket.localPort;
var message = 'Client, The TCP Server address is ' + JSON.stringify( address );
socket.write( message, function() {
var writeSize = socket.bytesWritten;
console.log( message + ' has send! ');
console.log( 'The size of message is ' + writeSize );
});
socket.on( 'data', function( data ) {
console.log( data.toString() );
var readSize = socket.bytesRead;
console.log( 'The size of data is ' + readSize );
});
});
TCPServer.listen( 18001, function() {
console.log( 'TCP Server is listening ... ' );
});