10 GP
C# using Socket Transfer Serialization Data
作者:貓貓風 ฅ●ω●ฅ│2019-09-25 13:03:46│巴幣:20│人氣:607
.
直接實作使用 Ethernet 直接將資料序列化後傳出
Server端收到資料後進行反序列化將資料進行還原
最後呈現在 GUI 上
Client
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using data_union;
- using System.Threading;
-
- namespace Socket_Serialize_transfer_Client
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- BinaryFormatter _bf = new BinaryFormatter();
- MemoryStream _stream;
- data_union.data_union _data = new data_union.data_union();
- Random _rnd = new Random();
- private Thread _th;
- IPEndPoint hostEP;
- Socket socket;
- bool _start_send = false;
- bool _first = true;
- bool _fail = false;
-
- private void Form1_Load(object sender, EventArgs e)
- {
- textBox3.Text = "Wait connect to Server";
-
- }
-
- public string CreateCheckCode()
- {
- char[] CharArray = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
- 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
- string sCode = "";
- Random random = new Random();
- for (int i = 0; i < 5; i++)
- {
- sCode += CharArray[random.Next(CharArray.Length)];
- }
- return sCode;
- }
-
- private void send_data_to_Server()
- {
- while (true)
- {
- if (_start_send == true)
- {
- try
- {
-
- byte[] bytesSend = new byte[1024];
- //序列化
- String rnd_str = CreateCheckCode();
- int ran_num = _rnd.Next(0, 1024);
- _data.data_identify = rnd_str;
- _data.data_value = ran_num;
- this.textBox1.Invoke((MethodInvoker)delegate
- {
- textBox1.Text = rnd_str;
- textBox2.Text = ran_num.ToString();
- });
- _stream = new MemoryStream();
- _bf.Serialize(_stream, _data);
-
- bytesSend = _stream.ToArray();
- socket.SendTo(bytesSend, 0, hostEP);
-
- }
- catch (Exception ex)
- {
- _fail = true;
- _first = true;
-
- if (_th != null)
- {
- this.textBox3.Invoke((MethodInvoker)delegate
- {
- textBox3.Text = "disconnect";
- });
- }
- //MessageBox.Show(ex.ToString());
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- _th.Abort();
- Application.Exit();
- }
- }
- Thread.Sleep(1000);
- }
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- if (_first)
- {
- _first = false;
- // 建立與Server端連線
- hostEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse("6300"));
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- socket.Connect(hostEP);
- //
- _th = new Thread(send_data_to_Server);
- _th.Start();
- }
- _start_send = true;
- textBox3.Text = "Connected to Server";
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- _start_send = false;
- }
-
- private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
- {
- int keychar = (int)e.KeyChar;
- int zero = (int)'0';
- int nine = (int)'9';
- int point = (int)'.';
-
- if ((keychar >= zero && keychar <= nine) || keychar == point || keychar == 8)
- {
- e.Handled = false;
- }
- else
- {
- e.Handled = true;
- }
- }
-
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!_fail && socket != null)
- {
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- _th.Abort();
- _th = null;
- }
- }
- }
- }
Server
執行結果
Server端每秒收到Client端傳來的資料
Server端收到後將資料反序列化後呈現在畫面上
原始資料和上一篇儲存的二進為檔原始內容相同
有些亂碼部分為編碼問題,目前沒特別做處理
相關的網路傳輸細節可以參考以下幾篇
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4540122
All rights reserved. 版權所有,保留一切權利
相關創作
同標籤作品搜尋:涼涼風|C#
留言共 4 篇留言
真島伯爵:
大大你好 最近剛開始學C# 只是網路教學的版本是好幾年前的 不知道大大有沒有教學的影片在網路上
09-25 13:10
真島伯爵:
想問大大 網路使用的版本是2015年的 我下載的是今年的 兩個版本有差別嗎? Visual Studio這個
09-25 15:00
珀伽索斯(Ama):
看起來好難的樣子,
如果以前C有學好應該會比較簡顛一點[e19]
09-27 06:28
我要留言提醒:您尚未登入,請先
登入再留言
10喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。
前一篇:C# 序列化 反序列化...
後一篇:貓咪大戰爭 絕 黑暗天堂...