Skip to content

Instantly share code, notes, and snippets.

@jkingry
Created June 25, 2010 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkingry/452893 to your computer and use it in GitHub Desktop.
Save jkingry/452893 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace RxDemoServer
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 4000);
listener.Start();
while (true)
{
var client = listener.AcceptTcpClient();
var stream = client.GetStream();
var message1Bytes = Encoding.UTF8.GetBytes("Hello World");
var size1Bytes = BitConverter.GetBytes(message1Bytes.Length);
Console.WriteLine("Sending message 1 length");
stream.Write(size1Bytes, 0, size1Bytes.Length);
Console.WriteLine("Sending message 1 bytes");
stream.Write(message1Bytes, 0, message1Bytes.Length);
var message2Bytes = Encoding.UTF8.GetBytes("Does this work?");
var size2Bytes = BitConverter.GetBytes(message2Bytes.Length);
byte[] combined = new byte[6];
Buffer.BlockCopy(size2Bytes, 2, combined, 0, 2);
Buffer.BlockCopy(message2Bytes, 0, combined, 2, 4);
Console.WriteLine("Sending 2 bytes of message 2 length");
stream.Write(size2Bytes, 0, 2);
Console.WriteLine("Sending 2 bytes of message 2 length + 4 bytes of message");
stream.Write(combined, 0, 6);
Console.WriteLine("Sending remaining messag 2 bytes");
stream.Write(message2Bytes, 4, message2Bytes.Length - 4);
Console.WriteLine("Closing socket");
stream.Close();
client.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment