資源簡介
不錯的TCP協(xié)議開發(fā) 實現(xiàn)上位機網(wǎng)絡通信,很好用,直接運行可用

代碼片段和文件信息
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.IO;
using?System.Net;
using?System.Net.Sockets;
namespace?Host
{
????public?partial?class?Form1?:?Form
????{
????????TcpListener?myListener;
????????private?Thread?Communi?=?null;
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????String?Result1?=?““;
????????????String?str1;
????????????String?hostName1?=?Dns.GetHostName();
????????????IPAddress[]?myIP1?=?Dns.GetHostAddresses(hostName1);
????????????foreach?(IPAddress?address1?in?myIP1)
????????????{
????????????????str1?=?address1.ToString();
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????if?((str1[i]?>=?‘0‘)?&&?(str1[i]?<=?‘9‘?||?str1[i]?==?‘.‘))
????????????????????????Result1?=?str1;
????????????????}
????????????}
????????????IPAddress?ipa1?=?IPAddress.Parse(Result1);
??????????
???????????
????????????myListener?=?new?TcpListener(ipa1?8001);
????????????myListener.Start();
????????????Communi?=?new?Thread(new?ThreadStart(this.Communicate));
????????????Communi.IsBackground?=?true;
????????????Recive_TextBox.Text?=?string.Empty;
????????????Communi.Start();
?????????
????????}
????????public?void?Communicate()
????????{
????????????try
????????????{
????????????????while?(true)
????????????????{
????????????????????if?(myListener.Pending()?==?true)
????????????????????{
????????????????????????Socket?mySocket?=?myListener.AcceptSocket();
????????????????????//????(mySocket.RemoteEndPoint?as?IPEndPoint).IPAddress;
????????????????????????IPAddress?IP?=?(mySocket.RemoteEndPoint?as?IPEndPoint).Address;
????????????????????????textBox1.Text?=?IP.ToString();
????????????????????????byte[]?receive_databyte=new?byte[50];
????????????????????
????????????????????????mySocket.Receive(receive_databyte);
????????????????????????String?content?=?Encoding.Unicode.GetString(receive_databyte);
????????????????????????
????????????????????????Recive_TextBox.Text?+=?System.DateTime.Now.ToString()?+?“??“?+?IP.ToString()?+?“\n說:“?+?content;
????????????????????????
????????????????????????
????????????????????????byte[]?send_databyte=Encoding.Unicode.GetBytes(“我愛你“);
????????????????????????mySocket.Send(send_databyte);
?????????????????????//???mySocket.Send(Encoding.Unicode.GetBytes(send_message));
????????????????????????//?MessageBox.Show(“I?love?you“);
??????????????????????
????????????????????????
????????????????????}
????????????????????Thread.Sleep(10);
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.Message);
????????????}?
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????901??2011-04-08?14:39??TCP\tcp\Host.sln
????..A..H.?????17408??2011-04-13?17:34??TCP\tcp\Host.suo
?????文件??????20480??2011-04-08?17:10??TCP\tcp\tcp\bin\Debug\Host.exe
?????文件??????22016??2011-04-08?17:10??TCP\tcp\tcp\bin\Debug\Host.pdb
?????文件???????5632??2005-12-08?14:51??TCP\tcp\tcp\bin\Debug\Host.vshost.exe
?????文件???????2981??2011-04-11?17:33??TCP\tcp\tcp\Form1.cs
?????文件???????4248??2011-04-08?15:31??TCP\tcp\tcp\Form1.Designer.cs
?????文件???????5814??2011-04-08?15:31??TCP\tcp\tcp\Form1.resx
?????文件???????3215??2011-04-08?15:31??TCP\tcp\tcp\Host.csproj
?????文件????????842??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug\Host.csproj.GenerateResource.Cache
?????文件??????20480??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug\Host.exe
?????文件????????180??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug\Host.Form1.resources
?????文件??????22016??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug\Host.pdb
?????文件????????180??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug\Host.Properties.Resources.resources
?????文件????????687??2011-04-13?13:33??TCP\tcp\tcp\obj\Host.csproj.FileListAbsolute.txt
?????文件????????463??2011-04-08?14:39??TCP\tcp\tcp\Program.cs
?????文件???????1166??2011-04-08?14:39??TCP\tcp\tcp\Properties\AssemblyInfo.cs
?????文件???????2864??2011-04-08?14:39??TCP\tcp\tcp\Properties\Resources.Designer.cs
?????文件???????5612??2011-04-08?14:39??TCP\tcp\tcp\Properties\Resources.resx
?????文件???????1089??2011-04-08?14:39??TCP\tcp\tcp\Properties\Settings.Designer.cs
?????文件????????249??2011-04-08?14:39??TCP\tcp\tcp\Properties\Settings.settings
?????目錄??????????0??2011-04-08?14:43??TCP\tcp\tcp\obj\Debug\Refactor
?????目錄??????????0??2011-04-08?14:39??TCP\tcp\tcp\obj\Debug\TempPE
?????目錄??????????0??2011-04-08?17:10??TCP\tcp\tcp\bin\Debug
?????目錄??????????0??2011-04-08?17:10??TCP\tcp\tcp\obj\Debug
?????目錄??????????0??2011-04-08?14:39??TCP\tcp\tcp\bin
?????目錄??????????0??2011-04-08?17:10??TCP\tcp\tcp\obj
?????目錄??????????0??2011-04-08?14:39??TCP\tcp\tcp\Properties
?????目錄??????????0??2011-04-11?17:33??TCP\tcp\tcp
?????目錄??????????0??2018-09-07?11:13??TCP\tcp
............此處省略4個文件信息
評論
共有 條評論