Node.JS TCP Server:
var net = require('net');
var fs = require('fs');
var HOST = '127.0.0.1';
var PORT = 3000;
var timeout = 420000; // msec
var lg = function(message) {
console.log(message);
};
var server = net.createServer();
server.on('listening', function() {
lg('Server listening on ' + HOST +':'+ PORT);
});
server.on('connection', function(sock) {
sock.setTimeout(timeout, function() {
try {
sock.end();
}
catch(x) {
lg('on end' + x);
}
});
sock.setNoDelay(true);
sock.setEncoding('ascii');
sock.on('data', function(data) {
try {
sock.write(data);
}
catch(x) {
lg(x);
}
});
sock.on('end', function(data) {
try {
sock.end();
}
catch(x) {
lg('on end' + x);
}
});
sock.on('error', function(err) {
lg(err);
});
sock.on('close', function(data) {
try {
sock.end();
}
catch(x) {
lg(x);
}
try {
sock.destroy();
}
catch(x) {
lg('on close' + x);
}
});
sock.on('timeout', function() {
});
});
server.on('error', function(err) {
});
server.on('close', function() {
});
server.listen(PORT, HOST);
Go TCP Server:
package main
import (
"log"
"net"
"os"
"runtime"
)
func main() {
ln, err := net.Listen("tcp", ":3000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go handleConn(conn)
}
}
func handleConn(c net.Conn) {
buf := make([]byte, 65536)
for {
n, err := c.Read(buf)
if err != nil || n == 0 {
c.Close()
break
}
n, err = c.Write(buf[0:n])
if err != nil {
c.Close()
break
}
}
log.Printf("conn %v closed", c.RemoteAddr())
}
func init() {
runtime.GOMAXPROCS(1110)
flags := log.Flags()
log.SetFlags(flags | log.Lmicroseconds | log.Llongfile | log.Lshortfile)
fn := "log.log"
f, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModeAppend)
if err != nil {
log.Println(err)
}
log.SetOutput(f)
}
And the client is in C#:
//...
namespace TestClient
{
static partial class Program
{
static void Main(string[] args)
{
ThreadPool.SetMaxThreads(10000, 5000);
ThreadPool.SetMinThreads(2000, 2500);
var taskCount = 10000;
var time = new Stopwatch();
time.Start();
for (int i = 0; i < taskCount; i++)
{
SampleSend();
}
var lastLogAt = DateTime.Now.AddMinutes(-1);
while (TaskCount < taskCount)
{
Thread.Sleep(10);
if ((DateTime.Now - lastLogAt).TotalSeconds > 0.5)
{
Console.WriteLine(Interlocked.Read(ref TaskCount));
lastLogAt = DateTime.Now;
}
}
time.Stop();
var l = string.Format("sent: {0}% rcvd: {1}%, fail: {2}%,
count: {3}", 100d * SentCount / taskCount, 100d * RcvdCount / taskCount,
100d * FailCount / taskCount, taskCount);
var l2 = string.Format("{0} op/sec", taskCount /
time.Elapsed.TotalSeconds);
Log(l);
Log(l2);
Console.WriteLine("press any key to exit");
Console.ReadKey();
}
public static long SentCount = 0;
public static long RcvdCount = 0;
public static long FailCount = 0;
public static long TaskCount = 0;
static async void SampleSend()
{
await Task.Delay(0);
Interlocked.Increment(ref TaskCount);
TcpClient client = null;
var enc = new ASCIIEncoding();
try
{
client = new TcpClient();
client.SendTimeout = TimeSpan.FromSeconds(0.5).Milliseconds;
client.ReceiveTimeout =
TimeSpan.FromSeconds(0.5).Milliseconds;
client.Connect(IPAddress.Parse("127.0.0.1"), 3000);
var strm = client.GetStream();
try
{
var message = "ping";
var bytes = enc.GetBytes(message);
strm.Write(bytes, 0, bytes.Length);
strm.Flush();
Interlocked.Increment(ref SentCount);
int b = 0;
var rcvdBytes = new List<byte>();
while ((b = strm.ReadByte()) > -1)
{
rcvdBytes.Add((byte)(b & 0xFF));
if (Encoding.ASCII.GetString(rcvdBytes.ToArray())
== message) break;
}
Interlocked.Increment(ref RcvdCount);
}
finally
{
strm.Close();
}
}
catch (Exception x)
{
LogError(x);
Interlocked.Increment(ref FailCount);
}
finally { try { client.Close(); } catch { } }
}
//...
}
}
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.