博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跨线程访问和服务器客户端访问互换(原创)
阅读量:4135 次
发布时间:2019-05-25

本文共 9264 字,大约阅读时间需要 30 分钟。

Server:

 

ExpandedBlockStart.gif
View Code
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();
        }
    }
}

 

 

Client:

ExpandedBlockStart.gif
View Code
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 InkClient
{
    
///
 
<summary>
    
///
 Interaction logic for MainWindow.xaml
    
///
 
</summary>
    
public 
partial 
class MainWindow : Window
    {
        NetObjectServer server = 
new NetObjectServer();
        NetObjectClient client = 
new NetObjectClient();
        
string strHost = 
"
172.18.50.104
";
        
//
132 大屏
        
//
104;
        
//
"172.18.200.118";
        
//
string strHost = "172.18.200.118";
//
"172.18.200.118";
        
int port = 
11006;
        
int callbackPort = 
11007;
        
string IPself
        {
            
get {
                IPHostEntry IPHost = Dns.Resolve(Dns.GetHostName());
                IPAddress[] address = IPHost.AddressList;
                
for (
int i = 
0; i < address.Count(); i++)
                {
                    
if (address[i].ToString().StartsWith(
"
172.18.50
"))
                    
return address[i].ToString();
                }
                
return 
"
127.0.0.1
";
            }
        }
        
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);
            client.OnReceived += 
new NetReceivedEventHandler<NetObject>(client_OnReceived);
            IPAddress ipSelf = IPAddress.Parse(IPself);
            
//
Start the server
            server.Start(ipSelf, callbackPort);
            
//
Connect to the server
            client.TryConnect(strHost, port);
            
//
send IP to server
            client.Send(IPself, 
"
SendIP
");
        }
        
void client_OnReceived(
object sender, NetReceivedEventArgs<NetObject> e)
        {
            
//
write("Client received" + e.Data.Name as string + e.Data.Object as string);
            
//
OnInkStrokesUpdate(e.Data.ByteArray);
        }
        Thread thOnReceived;
        
void server_OnReceived(
object sender, NetClientReceivedEventArgs<NetObject> e)
        {
            OnInkStrokesUpdate(e.Data.ByteArray);
            
//
thOnReceived = new Thread(new ParameterizedThreadStart(OnInkStrokesUpdate));
            
//
thOnReceived.IsBackground = true;
            
//
thOnReceived.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);
                }
                
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);
                }
            }
        }
        
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 
void SaveStrokes()
        {
            
try
            {
                MemoryStream memoryStream = 
new MemoryStream();
                
this.inkCanv.Strokes.Save(memoryStream);
                
if (client.IsConnected)
                    client.Send(
"", memoryStream.GetBuffer());
                memoryStream.Flush();
            }
            
catch (Exception exc)
            {
                MessageBox.Show(exc.Message, Title);
            }
        }
        Thread th;
        
private 
void inkCanv_StrokeCollected(
object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            SaveStrokes();
            
//
th = new Thread(new ThreadStart(SaveStrokes));
            
//
th.IsBackground = true;
            
//
th.Start();
        }
        
private 
void Window_Closed(
object sender, EventArgs e)
        {
            client.Disconnect();
            server.Stop();
        }
    }
}

 

 

转载地址:http://utpvi.baihongyu.com/

你可能感兴趣的文章
字节跳动安卓开发实习生面试分享
查看>>
好书分享之——《能力陷进》
查看>>
阅读笔记《c++ primer》
查看>>
阅读笔记《C++标准程序库》
查看>>
基于mirror driver的windows屏幕录像
查看>>
C语言8
查看>>
Qt实现简单延时
查看>>
qml有关矩形说明
查看>>
在qt中使用QSplitter设置初始比例setStretchFactor失效的解决方法
查看>>
repeater的使用
查看>>
qt msvc编译中文乱码解决
查看>>
qt中TextField输入框无法输入中文解决办法
查看>>
qt实现点击出现窗口,点击其他任何地方窗口消失
查看>>
QML DropArea拖拉文件事件
查看>>
CORBA links
查看>>
读后感:&gt;
查看>>
ideas about sharing software
查看>>
different aspects for software
查看>>
To do list
查看>>
Study of Source code
查看>>