Skip to content

Instantly share code, notes, and snippets.

@yallie
Created December 3, 2013 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yallie/7767323 to your computer and use it in GitHub Desktop.
Save yallie/7767323 to your computer and use it in GitHub Desktop.
Demonstrating the usage of streams in Zyan Communication Framework.
// Compile this code using:
//
// csc streams.cs /r:Zyan.Communication.dll
//
// First run — starts server.
// Second run — starts client.
using System;
using System.IO;
using System.Net.Sockets;
using Zyan.Communication;
using Zyan.Communication.Protocols.Tcp;
class Program
{
static void Main(string[] args)
{
try
{
RunServer();
}
catch (SocketException) // server already running
{
RunClient();
}
}
// shared code ---------------
interface IStreamService
{
Stream Download(string fileName);
}
// server code ---------------
class StreamService : IStreamService
{
public Stream Download(string fileName)
{
return File.OpenRead(fileName);
}
}
static void RunServer()
{
using (var zyanHost = new ZyanComponentHost("SampleStreamServer", 8194))
{
zyanHost.RegisterComponent<IStreamService, StreamService>();
Console.WriteLine("Server is ready. Press ENTER to stop.");
Console.WriteLine();
Console.WriteLine("Run the program again to download the file using streams.");
Console.ReadLine();
}
}
// client code ---------------
static void RunClient()
{
var serverUrl = "tcp://localhost:8194/SampleStreamServer";
Console.WriteLine("Connecting to {0}...", serverUrl);
Console.WriteLine();
using (var zyanConnection = new ZyanConnection(serverUrl))
{
var inputName = typeof(Program).Assembly.Location;
var outputName = inputName + "." + DateTime.Now.Ticks + ".dat";
Console.WriteLine("Downloading remote file: {0}", inputName);
var proxy = zyanConnection.CreateProxy<IStreamService>();
var input = proxy.Download(inputName);
var output = File.Create(outputName);
// copy data
input.CopyTo(output);
// close streams
input.Close();
output.Close();
Console.WriteLine("Download complete: {0}.", outputName);
Console.WriteLine("Press ENTER to quit.");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment