資源簡介
基于TCP實現的局域網通信,這個軟件是我給別人寫的一個小Demo,他們是和硬件通信,所以有些16進制轉換之類的!呵呵!
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Net.Sockets;
using?System.IO;
using?System.Net;
namespace?Client
{
????public?partial?class?FormClient?:?Form
????{
????????public?FormClient()
????????{
????????????InitializeComponent();
????????????this.hostName?=?this.textBoxIP.Text;
????????????this.port?=?int.Parse(this.textBoxPort.Text);
????????}
????????TcpClient?client;
????????string?hostName;
????????int?port;
????????private?void?buttonSend_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????using?(this.client?=?new?TcpClient(hostName?port))
????????????????{
????????????????????using?(NetworkStream?stream?=?client.GetStream())
????????????????????{
????????????????????????string[]?numbers?=?this.textBoxMessage.Text.Trim().Split(‘?‘);
????????????????????????List?buffer?=?new?List();
????????????????????????for?(int?i?=?0;?i?????????????????????????{
????????????????????????????byte?bit;
????????????????????????????if?(this.IsByte(numbers[i]?out?bit))
????????????????????????????{
????????????????????????????????buffer.Add(bit);
????????????????????????????}
????????????????????????}
????????????????????????stream.Write(buffer.ToArray()?0?buffer.Count);
????????????????????}
????????????????}
????????????}
????????????catch?(IOException)
????????????{
????????????????MessageBox.Show(“無法發送數據,遠程主機已經關閉!“);
????????????}
????????????catch?(SocketException)
????????????{
????????????????MessageBox.Show(“服務端未開啟!“);
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.Message);
????????????}
????????}
????????private?void?FormClient_FormClosing(object?sender?FormClosingEventArgs?e)
????????{
????????????if?(this.client?!=?null)
????????????{
????????????????this.client.Close();
????????????}
????????}
????????private?bool?IsByte(string?s?out?byte?bit)
????????{
????????????try
????????????{
????????????????bit?=?byte.Parse(s?System.Globalization.Numberstyles.AllowHexSpecifier);
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????bit?=?default(byte);
????????????????return?false;
????????????}
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????20480??2010-01-22?14:52??TCP_Demo\Application\Client.exe
?????文件??????22016??2010-01-22?14:52??TCP_Demo\Application\Client.pdb
?????文件???????5632??2005-12-08?14:51??TCP_Demo\Application\Client.vshost.exe
?????文件??????24576??2010-01-22?14:39??TCP_Demo\Application\Server.exe
?????文件??????26112??2010-01-22?14:39??TCP_Demo\Application\Server.pdb
?????文件???????5632??2005-12-08?14:51??TCP_Demo\Application\Server.vshost.exe
?????文件???????3286??2010-01-22?14:11??TCP_Demo\Client\Client.csproj
?????文件????????536??2010-01-22?14:11??TCP_Demo\Client\Client.csproj.user
?????文件???????2476??2010-01-22?14:52??TCP_Demo\Client\FormClient.cs
?????文件???????5698??2010-01-22?14:37??TCP_Demo\Client\FormClient.Designer.cs
?????文件???????5814??2010-01-22?14:37??TCP_Demo\Client\FormClient.resx
?????文件????????306??2010-01-22?14:52??TCP_Demo\Client\obj\Client.csproj.FileList.txt
?????文件????????905??2010-01-22?14:37??TCP_Demo\Client\obj\Debug\Client.csproj.GenerateResource.Cache
?????文件??????20480??2010-01-22?14:52??TCP_Demo\Client\obj\Debug\Client.exe
?????文件????????180??2010-01-22?14:37??TCP_Demo\Client\obj\Debug\Client.FormClient.resources
?????文件??????22016??2010-01-22?14:52??TCP_Demo\Client\obj\Debug\Client.pdb
?????文件????????180??2010-01-22?10:01??TCP_Demo\Client\obj\Debug\Client.Properties.Resources.resources
?????文件???????4608??2010-01-22?14:10??TCP_Demo\Client\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????470??2010-01-22?11:37??TCP_Demo\Client\Program.cs
?????文件???????1184??2010-01-22?09:06??TCP_Demo\Client\Properties\AssemblyInfo.cs
?????文件???????2834??2010-01-22?14:10??TCP_Demo\Client\Properties\Resources.Designer.cs
?????文件???????5612??2010-01-22?09:06??TCP_Demo\Client\Properties\Resources.resx
?????文件???????1102??2010-01-22?14:10??TCP_Demo\Client\Properties\Settings.Designer.cs
?????文件????????249??2010-01-22?09:06??TCP_Demo\Client\Properties\Settings.settings
?????文件???????5632??2005-12-08?14:51??TCP_Demo\Server\bin\Debug\Server.vshost.exe
?????文件???????3954??2010-01-22?14:39??TCP_Demo\Server\FormServer.cs
?????文件???????6563??2010-01-22?14:37??TCP_Demo\Server\FormServer.Designer.cs
?????文件???????5814??2010-01-22?14:37??TCP_Demo\Server\FormServer.resx
?????文件????????905??2010-01-22?14:37??TCP_Demo\Server\obj\Debug\Server.csproj.GenerateResource.Cache
?????文件??????24576??2010-01-22?14:39??TCP_Demo\Server\obj\Debug\Server.exe
............此處省略35個文件信息
- 上一篇:C# sql實現批量導入數據到數據庫
- 下一篇:C#左側導航菜單(動態生成)
評論
共有 條評論