資源簡介
在原有從網上獲取的代碼的基礎上進行了修改,包括一些捕獲異常以及按鈕的應用,擴充了一個listbox確保服務端可以選擇和不同的客戶端進行通信。使用TCP協議,socket通信,可以作為學習使用。

代碼片段和文件信息
/*C#?winform實現一個服務端和多個客戶端進行通信
?*?作者:IT格子
?*?微博:IT格子
?*?QQ:320587491
?*?如有疑問可聯系我
?*/
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.Threading;
using?System.Net.Sockets;
using?System.Net;
namespace?chat2
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????????StartPosition?=?FormStartPosition.CenterScreen;
????????????//關閉對文本框的非法線程操作檢查
????????????TextBox.CheckForIllegalCrossThreadCalls?=?false;
????????}
????????Thread?threadclient?=?null;
????????Socket?socketclient?=?null;
????????List?mlist?=?new?List();
????????private?void?textBox1_TextChanged(object?sender?EventArgs?e)
????????{
????????}
????????private?void?textBox2_TextChanged(object?sender?EventArgs?e)
????????{
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????this.button1.Enabled?=?false;
????????????//定義一個套接字監聽
????????????socketclient?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????//獲取文本框中的IP地址
????????????IPAddress?address?=?IPAddress.Parse(textBox1.Text.Trim());
????????????//將獲取的IP地址和端口號綁定在網絡節點上
????????????IPEndPoint?point?=?new?IPEndPoint(address?int.Parse(textBox2.Text.Trim()));
????????????try
????????????{
????????????????//客戶端套接字連接到網絡節點上,用的是Connect
????????????????socketclient.Connect(point);
????????????}
????????????catch?(Exception)
????????????{
????????????????//MessageBox.
????????????????MessageBox.Show(“連接失敗\r\n“);
????????????????this.button1.Enabled?=?true;
????????????????return;
????????????}
????????????threadclient?=?new?Thread(recv);
????????????threadclient.IsBackground?=?true;
????????????threadclient.Start();
????????}
????????private?void?recv()//
????????{
????????????int?x?=?0;
????????????while?(true)//持續監聽服務端發來的消息
????????????{
????????????????try
????????????????{
????????????????????//定義一個1M的內存緩沖區,用于臨時性存儲接收到的消息
????????????????????byte[]?arrRecvmsg?=?new?byte[1024?*?1024];
????????????????????//將客戶端套接字接收到的數據存入內存緩沖區,并獲取長度
????????????????????int?length?=?socketclient.Receive(arrRecvmsg);
????????????????????//將套接字獲取到的字符數組轉換為人可以看懂的字符串
????????????????????string?strRevMsg?=?Encoding.UTF8.GetString(arrRecvmsg?0?length);
????????????????????if?(x?==?1)
????????????????????{
????????????????????????textBox3.AppendText(“服務器:“?+?GetCurrentTime()?+?“\r\n“?+?strRevMsg?+?“\r\n\n“);
????????????????????}
????????????????????else
????????????????????{
????????????????????????textBox3.AppendText(strRevMsg?+?“\r\n\n“);
????????????????????????x?=?1;
????????????????????}
????????????????}
????????????????catch?(Exception?ex)
?????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-11-22?14:21??客戶端\
?????目錄???????????0??2018-11-22?14:42??客戶端\chat2\
?????目錄???????????0??2018-11-22?14:42??客戶端\chat2\.vs\
?????目錄???????????0??2018-11-22?14:42??客戶端\chat2\.vs\chat2\
?????目錄???????????0??2018-11-22?14:42??客戶端\chat2\.vs\chat2\v14\
?????文件???????16896??2018-11-22?14:41??客戶端\chat2\.vs\chat2\v14\.suo
?????目錄???????????0??2018-11-22?14:21??客戶端\chat2\chat2\
?????文件?????????857??2018-11-22?14:21??客戶端\chat2\chat2.sln
?????文件???????16896??2018-11-22?14:47??客戶端\chat2\chat2.suo
?????目錄???????????0??2018-11-22?14:21??客戶端\chat2\chat2\bin\
?????目錄???????????0??2018-11-22?14:39??客戶端\chat2\chat2\bin\Debug\
?????文件???????12800??2018-11-22?14:43??客戶端\chat2\chat2\bin\Debug\chat2.exe
?????文件???????28160??2018-11-22?14:43??客戶端\chat2\chat2\bin\Debug\chat2.pdb
?????文件???????11608??2018-11-22?14:43??客戶端\chat2\chat2\bin\Debug\chat2.vshost.exe
?????文件?????????490??2018-04-12?07:35??客戶端\chat2\chat2\bin\Debug\chat2.vshost.exe.manifest
?????文件????????3562??2018-11-22?14:47??客戶端\chat2\chat2\chat2.csproj
?????文件????????5732??2018-11-22?14:47??客戶端\chat2\chat2\Form1.cs
?????文件????????8037??2018-11-22?14:43??客戶端\chat2\chat2\Form1.Designer.cs
?????文件????????6016??2018-11-22?14:43??客戶端\chat2\chat2\Form1.resx
?????目錄???????????0??2018-11-22?14:21??客戶端\chat2\chat2\obj\
?????目錄???????????0??2018-11-22?14:21??客戶端\chat2\chat2\obj\x86\
?????目錄???????????0??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\
?????文件?????????690??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.csproj.FileListAbsolute.txt
?????文件?????????975??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.csproj.GenerateResource.Cache
?????文件????????1757??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.csprojResolveAssemblyReference.cache
?????文件???????12800??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.exe
?????文件?????????180??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.Form1.resources
?????文件???????28160??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\chat2.pdb
?????文件?????????180??2018-11-22?14:39??客戶端\chat2\chat2\obj\x86\Debug\chat2.Properties.Resources.resources
?????文件????????1229??2018-11-22?14:42??客戶端\chat2\chat2\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6125??2018-11-22?14:43??客戶端\chat2\chat2\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
............此處省略42個文件信息
- 上一篇:附和水準平差計算
- 下一篇:如何用VS自帶報表制作打印功能
評論
共有 條評論