Server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;
using NetSockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Windows.Threading;
namespace InkServer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
NetObjectServer server = new NetObjectServer();
Dictionary<string, NetObjectClient> clients = new Dictionary<string, NetObjectClient>();
string strHost = "172.18.50.104";//104
//"172.18.200.118";
//string strHost = "172.18.200.118";
//"172.18.200.118";
int port = 11006;
int callbackPort = 11007;
public MainWindow()
{
InitializeComponent();
server.EchoMode = NetEchoMode.EchoAllExceptSender;
inkCanv.StrokeCollected+=new InkCanvasStrokeCollectedEventHandler(inkCanv_StrokeCollected);
this.inkCanv.DefaultDrawingAttributes.StylusTip = StylusTip.Ellipse;
this.inkCanv.DefaultDrawingAttributes.Width = 10;
this.inkCanv.DefaultDrawingAttributes.Height = 10;
this.inkCanv.EditingMode = InkCanvasEditingMode.Ink;
server.OnReceived += new NetClientReceivedEventHandler<NetObject>(server_OnReceived);
IPAddress ip = IPAddress.Parse(strHost);
//Start the server
server.Start(ip, port);
}
void client_OnReceived(object sender, NetReceivedEventArgs<NetObject> e)
{
//write("Client received" + e.Data.Name as string + e.Data.Object as string);
}
Thread th;
void server_OnReceived(object sender, NetClientReceivedEventArgs<NetObject> e)
{
//Get the Clients IP
if (e.Data.Object as string == "SendIP")
{
string ip = e.Data.Name;
if (!clients.Keys.Contains(ip))
{
NetObjectClient noc = new NetObjectClient();
if (!noc.IsConnected)
{
noc.TryConnect(ip, callbackPort);
}
clients.Add(ip, noc);
}
}
if (e.Data.ByteArray!=null)
OnInkStrokesUpdate(e.Data.ByteArray);
//th = new Thread(new ParameterizedThreadStart(OnInkStrokesUpdate));
//th.IsBackground = true;
//th.Start(e.Data.ByteArray);
}
private delegate void UpdateDelegate(byte[] ds);
//private delegate void UpdateDelegate(object ds);
public void OnInkStrokesUpdate(object bytesStroke)
{
byte[] byts = bytesStroke as byte[];
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
InkStrokesUpdate(byts);
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateDelegate(InkStrokesUpdate), byts);
}
}
}
public void OnInkStrokesUpdate(byte[] bytesStroke)
{
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
InkStrokesUpdate(bytesStroke);
SaveStrokes();
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateDelegate(InkStrokesUpdate), bytesStroke);
SaveStrokes();
}
}
}
private void InkStrokesUpdate(byte[] bytesStroke)
{
try
{
System.IO.MemoryStream memoryStream = new MemoryStream(bytesStroke);
this.inkCanv.Strokes = new StrokeCollection(memoryStream);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private delegate void SaveStrokesDelegate();
private void SaveStrokes()
{
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
SaveStrokesAndSend();
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new SaveStrokesDelegate(SaveStrokesAndSend));
}
}
try
{
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, Title);
}
}
private void SaveStrokesAndSend()
{
MemoryStream memoryStream = new MemoryStream();
this.inkCanv.Strokes.Save(memoryStream);
//Connect to the server
foreach (KeyValuePair<string, NetObjectClient> kv in clients)
{
if (kv.Value.IsConnected)
kv.Value.Send("", memoryStream.GetBuffer());
}
memoryStream.Flush();
}
private void inkCanv_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
SaveStrokes();
}
private void Window_Closed(object sender, EventArgs e)
{
foreach (KeyValuePair<string, NetObjectClient> client in clients)
{
client.Value.Disconnect();
}
server.Stop();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;
using NetSockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Windows.Threading;
namespace InkServer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
NetObjectServer server = new NetObjectServer();
Dictionary<string, NetObjectClient> clients = new Dictionary<string, NetObjectClient>();
string strHost = "172.18.50.104";//104
//"172.18.200.118";
//string strHost = "172.18.200.118";
//"172.18.200.118";
int port = 11006;
int callbackPort = 11007;
public MainWindow()
{
InitializeComponent();
server.EchoMode = NetEchoMode.EchoAllExceptSender;
inkCanv.StrokeCollected+=new InkCanvasStrokeCollectedEventHandler(inkCanv_StrokeCollected);
this.inkCanv.DefaultDrawingAttributes.StylusTip = StylusTip.Ellipse;
this.inkCanv.DefaultDrawingAttributes.Width = 10;
this.inkCanv.DefaultDrawingAttributes.Height = 10;
this.inkCanv.EditingMode = InkCanvasEditingMode.Ink;
server.OnReceived += new NetClientReceivedEventHandler<NetObject>(server_OnReceived);
IPAddress ip = IPAddress.Parse(strHost);
//Start the server
server.Start(ip, port);
}
void client_OnReceived(object sender, NetReceivedEventArgs<NetObject> e)
{
//write("Client received" + e.Data.Name as string + e.Data.Object as string);
}
Thread th;
void server_OnReceived(object sender, NetClientReceivedEventArgs<NetObject> e)
{
//Get the Clients IP
if (e.Data.Object as string == "SendIP")
{
string ip = e.Data.Name;
if (!clients.Keys.Contains(ip))
{
NetObjectClient noc = new NetObjectClient();
if (!noc.IsConnected)
{
noc.TryConnect(ip, callbackPort);
}
clients.Add(ip, noc);
}
}
if (e.Data.ByteArray!=null)
OnInkStrokesUpdate(e.Data.ByteArray);
//th = new Thread(new ParameterizedThreadStart(OnInkStrokesUpdate));
//th.IsBackground = true;
//th.Start(e.Data.ByteArray);
}
private delegate void UpdateDelegate(byte[] ds);
//private delegate void UpdateDelegate(object ds);
public void OnInkStrokesUpdate(object bytesStroke)
{
byte[] byts = bytesStroke as byte[];
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
InkStrokesUpdate(byts);
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateDelegate(InkStrokesUpdate), byts);
}
}
}
public void OnInkStrokesUpdate(byte[] bytesStroke)
{
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
InkStrokesUpdate(bytesStroke);
SaveStrokes();
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateDelegate(InkStrokesUpdate), bytesStroke);
SaveStrokes();
}
}
}
private void InkStrokesUpdate(byte[] bytesStroke)
{
try
{
System.IO.MemoryStream memoryStream = new MemoryStream(bytesStroke);
this.inkCanv.Strokes = new StrokeCollection(memoryStream);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private delegate void SaveStrokesDelegate();
private void SaveStrokes()
{
if (inkCanv != null)
{
// Checking if this thread has access to the object.
if (inkCanv.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
SaveStrokesAndSend();
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
inkCanv.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new SaveStrokesDelegate(SaveStrokesAndSend));
}
}
try
{
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, Title);
}
}
private void SaveStrokesAndSend()
{
MemoryStream memoryStream = new MemoryStream();
this.inkCanv.Strokes.Save(memoryStream);
//Connect to the server
foreach (KeyValuePair<string, NetObjectClient> kv in clients)
{
if (kv.Value.IsConnected)
kv.Value.Send("", memoryStream.GetBuffer());
}
memoryStream.Flush();
}
private void inkCanv_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
SaveStrokes();
}
private void Window_Closed(object sender, EventArgs e)
{
foreach (KeyValuePair<string, NetObjectClient> client in clients)
{
client.Value.Disconnect();
}
server.Stop();
}
}
}