Skip to content

Commit

Permalink
adding udp support
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Mar 19, 2015
1 parent 0e8e9e8 commit c358092
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
37 changes: 28 additions & 9 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var cluster = require('cluster');
var net = require('net');
var dgram = require('dgram');
var numCPUs = require('os').cpus().length;

function new_connection(socket) {
console.log('Connection accepted for ' + socket.remoteAddress + ":" + socket.remotePort);
}
var HOST = "127.0.0.1"

if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
Expand All @@ -15,18 +14,16 @@ if (cluster.isMaster) {
});
} else {

// Echo
// TCP Echo
net.createServer(function(socket) {
new_connection(socket);
socket.on('data', function(data) {
socket.write(data);
});
socket.on('error', function() {});
}).listen(4444);
}).listen(30000);

// Info
// TCP Info
net.createServer(function(socket) {
new_connection(socket);
socket.on('data', function(data) {
var response = {
"client-ip": socket.remoteAddress,
Expand All @@ -37,5 +34,27 @@ if (cluster.isMaster) {
socket.write(JSON.stringify(response));
});
socket.on('error', function() {});
}).listen(5555);
}).listen(30001);

// UDP Echo
var udp_echo_server = dgram.createSocket("udp4");
udp_echo_server.on("message", function(msg, rinfo) {
udp_echo_server.send(msg, 0, msg.length, rinfo.port, rinfo.address);
});
udp_echo_server.bind(40000, HOST);

// UDP Info
var udp_info_server = dgram.createSocket("udp4");
udp_info_server.on("message", function(msg, rinfo) {
var response = JSON.stringify({
"client-ip": rinfo.address,
"data": msg,
"text-data": msg.toString(),
"size": msg.length
});

var buffer = new Buffer(response);
udp_info_server.send(buffer, 0, buffer.length, rinfo.port, rinfo.address);
});
udp_info_server.bind(40001, HOST);
}
34 changes: 27 additions & 7 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html>
<head>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<title>tcpbin(1): TCP Client Testing Service</title>
<title>tcpbin(1): TCP/UDP Client Testing Service</title>
<style type='text/css' media='all'>
/* style: man */
body#manpage {margin:0}
Expand Down Expand Up @@ -61,28 +61,48 @@


<div class='mp'>
<h1>tcpbin(1): TCP Request &amp; Response Service</h1>
<h1>tcpbin(1): TCP/UDP Request &amp; Response Service</h1>
<p>Created by <a href="https://www.mashape.com/">Mashape</a></p>

<h2 id="USAGE">FEATURES</h2>

<ul>
<li><b>echo:</b> Echoes any request sent to <code>54.175.103.105:4444</code>
<li><b>info:</b> Get request info at <code>54.175.103.105:5555</code>
<li><b>TCP echo:</b> Echoes any request sent to <code>54.175.103.105:30000</code>
<li><b>TCP info:</b> Get request info at <code>54.175.103.105:30001</code>
</ul>
<ul>
<li><b>UDP echo:</b> Echoes any request sent to <code>54.175.103.105:40000</code>
<li><b>UDP info:</b> Get request info at <code>54.175.103.105:40001</code>
</ul>

<h2 id="DESCRIPTION">DESCRIPTION</h2>

<p>This project has been started to help testing TCP requests in a very easy way. It is very useful for seeing what your clients are sending to TCP servers and debug problems. It can also be used for mock integration tests.</p>
<p>This project has been started to help testing TCP and UDP requests in a very easy way. It is very useful for seeing what your clients are sending to TCP servers and debug problems. It can also be used for mock integration tests.</p>

<h2 id="EXAMPLES">EXAMPLES</h2>

<h3 id="-echo-sample">$ echo "Text to send" | ncat 54.175.103.105 4444</h3>
<h3 id="-echo-sample">$ echo "Text to send" | ncat 54.175.103.105 30000</h3>

<pre><code>Text to send
</code></pre>

<h3 id="-info-sample">$ echo "Get some info" | ncat 54.175.103.105 30001</h3>

<pre><code>
{
"client-ip": "::ffff:41.130.36.121",
"data": [71,101,116,32,115,111,109,101,32,105,110,102,111,10],
"text-data": "Get some info\n",
"size": 14
}
</code></pre>

<h3 id="-echo-sample">$ echo -n "Text to send" | nc -4u -w1 54.175.103.105 40000</h3>

<pre><code>Text to send
</code></pre>

<h3 id="-info-sample">$ echo "Get some info" | ncat 54.175.103.105 5555</h3>
<h3 id="-info-sample">$ echo -n "Get some info" | nc -4u -w1 54.175.103.105 40001</h3>

<pre><code>
{
Expand Down

0 comments on commit c358092

Please sign in to comment.