資源簡介
這是一個用C#寫的Socket服務器和客戶端,當然Socket服務器和客戶端不是主要的(有興趣的朋友可以參考下),主要的內容是介紹如何通過這種Socket方式發送Object對象。
作者博客 http://luyugao.com

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Threading;
using?System.Net;
using?System.Net.Sockets;
using?System.IO;
using?System.Runtime.Serialization.Formatters.Binary;
using?User;
//object對象是通過user類派生的,這里引用的是User.dll?
//??作者博客:?http://www.luyugao.com
namespace?Client
{
????public?partial?class?Client?:?Form
????{
????????public?Client()
????????{
????????????InitializeComponent();
????????}
????????Socket?mySocket;
????????///?
????????///?接收信息緩沖區大小
????????///?
????????int?packetSize?=?1024?*?50;
????????private?void?bt_lj_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????if?(tb_ip.Text.Trim()?!=?““?&&?tb_port.Text.Trim()?!=?““)
????????????????{
????????????????????IPEndPoint?iep;
????????????????????try
????????????????????{
????????????????????????iep?=?new?IPEndPoint(
????????????????????????????IPAddress.Parse(tb_ip.Text)?Convert.ToInt32(tb_port.Text)
????????????????????????????);
????????????????????}
????????????????????catch?(Exception?ex)?{?MessageBox.Show(ex.Message);?return;?}
????????????????????mySocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????????mySocket.Connect(iep);
????????????????????Thread?t?=?new?Thread(resMsg);
????????????????????t.IsBackground?=?true;
????????????????????t.Start();
????????????????????bt_lj.Enabled?=?false;
????????????????????bt_sendobject.Enabled?=?bt_sendStr.Enabled?=?true;
????????????????}
????????????}
????????????catch?(Exception?ex)?{?MessageBox.Show(ex.Message);?}
????????}
????????//object對象是通過user類派生的,這里引用的是User.dll?
????????//??作者博客:?http://www.luyugao.com
????????///?
????????///?循環接收信息
????????///?
????????void?resMsg()
????????{
????????????while?(true)
????????????{
????????????????byte[]?by?=?new?byte[packetSize];
????????????????int?length?=?-1;
????????????????try
????????????????{
????????????????????length?=?mySocket.Receive(by);
????????????????}
????????????????catch?(SocketException)?{?break;?}
????????????????catch?(Exception)?{?break;?}
????????????????if?(length?2)//消息異常
????????????????????continue;
????????????????string?_cmd?=?getMsgHead(by);
????????????????switch?(_cmd)
????????????????{
????????????????????case?“01“://文本消息
????????????????????????string?Msg?=?Encoding.UTF8.GetString(by?2?length?-?2);
????????????????????????showMsg(Msg);
????????????????????????break;
????????????????????case“02“://對象消息
?????????????????????????showMsg(“收到對象、信息如下:“);
????????????????????????byte[]?by1=new?byte[length-2];
????????????????????????Buffer.BlockCopy(by?2?by1?0?by1.Length);
????????????????????????object?_obj?=?Deserialize(by1);
????????????????????????user?_u?=?_obj?as?user;
????????????????????????string?s?=?st
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????16384??2013-07-18?17:28??Sendob
?????文件??????34304??2013-07-18?17:28??Sendob
?????文件??????11608??2013-07-18?17:30??Sendob
?????文件???????4096??2013-07-18?17:00??Sendob
?????文件???????8252??2013-07-18?17:28??Sendob
?????文件???????3631??2013-07-18?17:01??Sendob
?????文件??????14119??2013-07-18?17:22??Sendob
?????文件???????5817??2013-07-18?17:22??Sendob
?????文件????????180??2013-07-18?17:22??Sendob
?????文件????????691??2013-07-18?17:30??Sendob
?????文件????????976??2013-07-18?17:22??Sendob
?????文件???????4257??2013-07-18?17:01??Sendob
?????文件??????16384??2013-07-18?17:28??Sendob
?????文件??????34304??2013-07-18?17:28??Sendob
?????文件????????180??2013-07-18?17:01??Sendob
?????文件???????5419??2013-07-18?17:29??Sendob
?????文件????????468??2013-07-18?16:06??Sendob
?????文件???????1344??2013-07-18?16:06??Sendob
?????文件???????2864??2013-07-18?16:06??Sendob
?????文件???????5612??2013-07-18?16:06??Sendob
?????文件???????1093??2013-07-18?16:06??Sendob
?????文件????????249??2013-07-18?16:06??Sendob
?????文件??????14848??2013-07-18?17:21??Sendob
?????文件??????34304??2013-07-18?17:21??Sendob
?????文件??????11608??2013-07-18?17:31??Sendob
?????文件???????4096??2013-07-18?17:00??Sendob
?????文件??????11718??2013-07-18?17:21??Sendob
?????文件???????5261??2013-07-18?17:18??Sendob
?????文件???????5817??2013-07-18?17:18??Sendob
?????文件????????789??2013-07-18?15:20??Sendob
............此處省略57個文件信息
- 上一篇:如鵬網2019 .NET高級開發就業班.rar
- 下一篇:云圖等高線圖繪制算法源碼
評論
共有 條評論