C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能(客戶端)
本文實(shí)例為大家分享了C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能的具體代碼,供大家參考,具體內(nèi)容如下
前言
使用C#語(yǔ)言進(jìn)行開(kāi)發(fā),基于.NET FrameWork4
功能包含群聊,和私聊
參考C# 利用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能——服務(wù)端
界面
界面設(shè)計(jì)代碼
namespace chat_client { ? ? partial class Form1 ? ? { ? ? ? ? /// <summary> ? ? ? ? /// 必需的設(shè)計(jì)器變量。 ? ? ? ? /// </summary> ? ? ? ? private System.ComponentModel.IContainer components = null; ? ? ? ? /// <summary> ? ? ? ? /// 清理所有正在使用的資源。 ? ? ? ? /// </summary> ? ? ? ? /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> ? ? ? ? protected override void Dispose(bool disposing) ? ? ? ? { ? ? ? ? ? ? if (disposing && (components != null)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? components.Dispose(); ? ? ? ? ? ? } ? ? ? ? ? ? base.Dispose(disposing); ? ? ? ? } ? ? ? ? #region Windows 窗體設(shè)計(jì)器生成的代碼 ? ? ? ? /// <summary> ? ? ? ? /// 設(shè)計(jì)器支持所需的方法 - 不要修改 ? ? ? ? /// 使用代碼編輯器修改此方法的內(nèi)容。 ? ? ? ? /// </summary> ? ? ? ? private void InitializeComponent() ? ? ? ? { ? ? ? ? ? ? this.buttonSend = new System.Windows.Forms.Button(); ? ? ? ? ? ? this.textBoxMsg = new System.Windows.Forms.TextBox(); ? ? ? ? ? ? this.textBoxLog = new System.Windows.Forms.TextBox(); ? ? ? ? ? ? this.buttonStart = new System.Windows.Forms.Button(); ? ? ? ? ? ? this.labelPort = new System.Windows.Forms.Label(); ? ? ? ? ? ? this.textBoxPort = new System.Windows.Forms.TextBox(); ? ? ? ? ? ? this.labelIP = new System.Windows.Forms.Label(); ? ? ? ? ? ? this.textBoxIP = new System.Windows.Forms.TextBox(); ? ? ? ? ? ? this.labelName = new System.Windows.Forms.Label(); ? ? ? ? ? ? this.textBoxName = new System.Windows.Forms.TextBox(); ? ? ? ? ? ? this.SuspendLayout(); ? ? ? ? ? ? //? ? ? ? ? ? ? // buttonSend ? ? ? ? ? ? //? ? ? ? ? ? ? this.buttonSend.Location = new System.Drawing.Point(769, 504); ? ? ? ? ? ? this.buttonSend.Name = "buttonSend"; ? ? ? ? ? ? this.buttonSend.Size = new System.Drawing.Size(99, 43); ? ? ? ? ? ? this.buttonSend.TabIndex = 15; ? ? ? ? ? ? this.buttonSend.Text = "發(fā)送"; ? ? ? ? ? ? this.buttonSend.UseVisualStyleBackColor = true; ? ? ? ? ? ? this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click); ? ? ? ? ? ? //? ? ? ? ? ? ? // textBoxMsg ? ? ? ? ? ? //? ? ? ? ? ? ? this.textBoxMsg.Location = new System.Drawing.Point(36, 504); ? ? ? ? ? ? this.textBoxMsg.Name = "textBoxMsg"; ? ? ? ? ? ? this.textBoxMsg.Size = new System.Drawing.Size(653, 25); ? ? ? ? ? ? this.textBoxMsg.TabIndex = 14; ? ? ? ? ? ? //? ? ? ? ? ? ? // textBoxLog ? ? ? ? ? ? //? ? ? ? ? ? ? this.textBoxLog.Location = new System.Drawing.Point(36, 78); ? ? ? ? ? ? this.textBoxLog.Multiline = true; ? ? ? ? ? ? this.textBoxLog.Name = "textBoxLog"; ? ? ? ? ? ? this.textBoxLog.Size = new System.Drawing.Size(832, 406); ? ? ? ? ? ? this.textBoxLog.TabIndex = 13; ? ? ? ? ? ? //? ? ? ? ? ? ? // buttonStart ? ? ? ? ? ? //? ? ? ? ? ? ? this.buttonStart.Location = new System.Drawing.Point(726, 18); ? ? ? ? ? ? this.buttonStart.Name = "buttonStart"; ? ? ? ? ? ? this.buttonStart.Size = new System.Drawing.Size(142, 45); ? ? ? ? ? ? this.buttonStart.TabIndex = 12; ? ? ? ? ? ? this.buttonStart.Text = "連接服務(wù)器"; ? ? ? ? ? ? this.buttonStart.UseVisualStyleBackColor = true; ? ? ? ? ? ? this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click); ? ? ? ? ? ? //? ? ? ? ? ? ? // labelPort ? ? ? ? ? ? //? ? ? ? ? ? ? this.labelPort.AutoSize = true; ? ? ? ? ? ? this.labelPort.Location = new System.Drawing.Point(267, 33); ? ? ? ? ? ? this.labelPort.Name = "labelPort"; ? ? ? ? ? ? this.labelPort.Size = new System.Drawing.Size(54, 15); ? ? ? ? ? ? this.labelPort.TabIndex = 11; ? ? ? ? ? ? this.labelPort.Text = "port:"; ? ? ? ? ? ? //? ? ? ? ? ? ? // textBoxPort ? ? ? ? ? ? //? ? ? ? ? ? ? this.textBoxPort.Location = new System.Drawing.Point(327, 33); ? ? ? ? ? ? this.textBoxPort.Name = "textBoxPort"; ? ? ? ? ? ? this.textBoxPort.Size = new System.Drawing.Size(100, 25); ? ? ? ? ? ? this.textBoxPort.TabIndex = 10; ? ? ? ? ? ? this.textBoxPort.Text = "6666"; ? ? ? ? ? ? //? ? ? ? ? ? ? // labelIP ? ? ? ? ? ? //? ? ? ? ? ? ? this.labelIP.AutoSize = true; ? ? ? ? ? ? this.labelIP.Location = new System.Drawing.Point(74, 33); ? ? ? ? ? ? this.labelIP.Name = "labelIP"; ? ? ? ? ? ? this.labelIP.Size = new System.Drawing.Size(31, 15); ? ? ? ? ? ? this.labelIP.TabIndex = 9; ? ? ? ? ? ? this.labelIP.Text = "IP:"; ? ? ? ? ? ? //? ? ? ? ? ? ? // textBoxIP ? ? ? ? ? ? //? ? ? ? ? ? ? this.textBoxIP.Location = new System.Drawing.Point(125, 30); ? ? ? ? ? ? this.textBoxIP.Name = "textBoxIP"; ? ? ? ? ? ? this.textBoxIP.Size = new System.Drawing.Size(100, 25); ? ? ? ? ? ? this.textBoxIP.TabIndex = 8; ? ? ? ? ? ? this.textBoxIP.Text = "127.0.0.1"; ? ? ? ? ? ? //? ? ? ? ? ? ? // labelName ? ? ? ? ? ? //? ? ? ? ? ? ? this.labelName.AutoSize = true; ? ? ? ? ? ? this.labelName.Location = new System.Drawing.Point(473, 33); ? ? ? ? ? ? this.labelName.Name = "labelName"; ? ? ? ? ? ? this.labelName.Size = new System.Drawing.Size(67, 15); ? ? ? ? ? ? this.labelName.TabIndex = 17; ? ? ? ? ? ? this.labelName.Text = "用戶名:"; ? ? ? ? ? ? //? ? ? ? ? ? ? // textBoxName ? ? ? ? ? ? //? ? ? ? ? ? ? this.textBoxName.Location = new System.Drawing.Point(546, 30); ? ? ? ? ? ? this.textBoxName.Name = "textBoxName"; ? ? ? ? ? ? this.textBoxName.Size = new System.Drawing.Size(100, 25); ? ? ? ? ? ? this.textBoxName.TabIndex = 16; ? ? ? ? ? ? this.textBoxName.Text = "ant1"; ? ? ? ? ? ? //? ? ? ? ? ? ? // Form1 ? ? ? ? ? ? //? ? ? ? ? ? ? this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); ? ? ? ? ? ? this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ? ? ? ? ? ? this.ClientSize = new System.Drawing.Size(904, 564); ? ? ? ? ? ? this.Controls.Add(this.labelName); ? ? ? ? ? ? this.Controls.Add(this.textBoxName); ? ? ? ? ? ? this.Controls.Add(this.buttonSend); ? ? ? ? ? ? this.Controls.Add(this.textBoxMsg); ? ? ? ? ? ? this.Controls.Add(this.textBoxLog); ? ? ? ? ? ? this.Controls.Add(this.buttonStart); ? ? ? ? ? ? this.Controls.Add(this.labelPort); ? ? ? ? ? ? this.Controls.Add(this.textBoxPort); ? ? ? ? ? ? this.Controls.Add(this.labelIP); ? ? ? ? ? ? this.Controls.Add(this.textBoxIP); ? ? ? ? ? ? this.Name = "Form1"; ? ? ? ? ? ? this.Text = "客戶端"; ? ? ? ? ? ? this.ResumeLayout(false); ? ? ? ? ? ? this.PerformLayout(); ? ? ? ? } ? ? ? ? #endregion ? ? ? ? private System.Windows.Forms.Button buttonSend; ? ? ? ? private System.Windows.Forms.TextBox textBoxMsg; ? ? ? ? private System.Windows.Forms.TextBox textBoxLog; ? ? ? ? private System.Windows.Forms.Button buttonStart; ? ? ? ? private System.Windows.Forms.Label labelPort; ? ? ? ? private System.Windows.Forms.TextBox textBoxPort; ? ? ? ? private System.Windows.Forms.Label labelIP; ? ? ? ? private System.Windows.Forms.TextBox textBoxIP; ? ? ? ? private System.Windows.Forms.Label labelName; ? ? ? ? private System.Windows.Forms.TextBox textBoxName; ? ? } }
源代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; namespace chat_client { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? public Socket clientSocket; ? ? ? ? ? ? ? ? private void buttonStart_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? // 客戶端連接服務(wù)器 ? ? ? ? ? ? // 1 創(chuàng)建socket對(duì)象 ? ? ? ? ? ? clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ? ? ? ? ? ? //2、綁定ip和端口 ? ? ? ? ? ? String ip = textBoxIP.Text; ? ? ? ? ? ? int port = Convert.ToInt32(textBoxPort.Text); ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("連接服務(wù)器失敗"); ? ? ? ? ? ? } ? ? ? ? ? ? //4、發(fā)送name ? ? ? ? ? ? byte[] data = Encoding.Default.GetBytes(textBoxName.Text); ? ? ? ? ? ? clientSocket.Send(data, 0, data.Length, SocketFlags.None); ? ? ? ? ? ? //3、接受消息 ? ? ? ? ? ? Thread thread = new Thread(new ParameterizedThreadStart(recevie)); ? ? ? ? ? ? thread.IsBackground = true;// 設(shè)置為后臺(tái)線程 ? ? ? ? ? ? thread.Start(clientSocket); ? ? ? ? ? ? buttonStart.Enabled = false; ? ? ? ? } ? ? ? ? private void recevie(object socket) ? ? ? ? { ? ? ? ? ? ? var proxSocket = socket as Socket; ? ? ? ? ? ? byte[] data = new byte[1024 * 1024]; ? ? ? ? ? ? String msg; ? ? ? ? ? ? int len = 0; ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? len = proxSocket.Receive(data, 0, data.Length, SocketFlags.None); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? catch ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? stopConnect();//關(guān)閉連接 ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (len <= 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //客戶端正常退出 ? ? ? ? ? ? ? ? ? ? msg = String.Format("服務(wù)器{0}發(fā)送異常", ? ? ? ? ? ? ? ? ? ?proxSocket.RemoteEndPoint.ToString()); ? ? ? ? ? ? ? ? ? ? stopConnect();//關(guān)閉連接 ? ? ? ? ? ? ? ? ? ? return;//結(jié)束當(dāng)前接受客戶端數(shù)據(jù)的異步線程 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //顯示消息 ? ? ? ? ? ? ? ? msg = Encoding.Default.GetString(data, 0, len); ? ? ? ? ? ? ? ? showLog(msg); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void stopConnect() ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (clientSocket.Connected) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? clientSocket.Shutdown(SocketShutdown.Both); ? ? ? ? ? ? ? ? ? ? clientSocket.Close(100); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? private void showLog(String msg) ? ? ? ? { ? ? ? ? ? ? if (textBoxLog.InvokeRequired) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //如果是跨線程訪問(wèn) ? ? ? ? ? ? ? ? textBoxLog.Invoke(new Action<String>( ? ? ? ? ? ? ? ? ? ?s => { ? ? ? ? ? ? ? ? ? ? ? ?this.textBoxLog.Text += msg + "\r\n"; ? ? ? ? ? ? ? ? ? ?}), msg); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? this.textBoxLog.Text += msg; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void buttonSend_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (clientSocket.Connected) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //先判斷是否還有服務(wù)器處于連接狀態(tài) ? ? ? ? ? ? ? ? byte[] data = Encoding.Default.GetBytes(textBoxMsg.Text); ? ? ? ? ? ? ? ? clientSocket.Send(data, 0, data.Length, SocketFlags.None); ? ? ? ? ? ? ? ? textBoxMsg.Text = ""; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("與服務(wù)器斷開(kāi)連接"); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
運(yùn)行截圖
打開(kāi)多個(gè)運(yùn)行程序,在項(xiàng)目生成可執(zhí)行文件后,在項(xiàng)目文件目錄下,找到bin\Debug
可以點(diǎn)擊幾個(gè)可執(zhí)行程序
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity3D利用DoTween實(shí)現(xiàn)卡牌翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Unity3D利用DoTween實(shí)現(xiàn)卡牌翻轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Unity UGUI的ToggleGroup選項(xiàng)組件介紹使用
這篇文章主要為大家介紹了Unity UGUI的ToggleGroup選項(xiàng)組件介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07關(guān)于C# TabPage如何隱藏的問(wèn)題
TabPage沒(méi)有Visible屬性,所以只能通過(guò)設(shè)置將其與父控件(tabcontrol)的關(guān)聯(lián)性去除就好了,如下面代碼:2013-04-04C#調(diào)用C動(dòng)態(tài)鏈接庫(kù)的實(shí)現(xiàn)
動(dòng)態(tài)鏈接庫(kù)是不能直接執(zhí)行的,也不能接收消息,它只是一個(gè)獨(dú)立的文件,本文主要介紹了C#調(diào)用C動(dòng)態(tài)鏈接庫(kù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C#數(shù)據(jù)類(lèi)型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型)
本文詳細(xì)講解了C#數(shù)據(jù)類(lèi)型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01c# RSA非對(duì)稱(chēng)加解密及XML&PEM格式互換方案
這篇文章主要介紹了c# RSA非對(duì)稱(chēng)加解密及XML&PEM格式互換方案,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12