最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#用websocket實現(xiàn)簡易聊天功能(客戶端)

 更新時間:2022年02月12日 19:45:18   作者:antRain  
這篇文章主要為大家詳細介紹了C#用websocket實現(xiàn)簡易聊天功能,客戶端方向,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C#用websocket實現(xiàn)簡易聊天功能的具體代碼,供大家參考,具體內(nèi)容如下

前言

使用C#語言進行開發(fā),基于.NET FrameWork4
功能包含群聊,和私聊
參考C# 利用websocket實現(xiàn)簡易聊天功能——服務端

界面

界面設計代碼

namespace chat_client
{
? ? partial class Form1
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 必需的設計器變量。
? ? ? ? /// </summary>
? ? ? ? private System.ComponentModel.IContainer components = null;

? ? ? ? /// <summary>
? ? ? ? /// 清理所有正在使用的資源。
? ? ? ? /// </summary>
? ? ? ? /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
? ? ? ? protected override void Dispose(bool disposing)
? ? ? ? {
? ? ? ? ? ? if (disposing && (components != null))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? components.Dispose();
? ? ? ? ? ? }
? ? ? ? ? ? base.Dispose(disposing);
? ? ? ? }

? ? ? ? #region Windows 窗體設計器生成的代碼

? ? ? ? /// <summary>
? ? ? ? /// 設計器支持所需的方法 - 不要修改
? ? ? ? /// 使用代碼編輯器修改此方法的內(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 = "連接服務器";
? ? ? ? ? ? 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)
? ? ? ? {
? ? ? ? ? ? // 客戶端連接服務器
? ? ? ? ? ? // 1 創(chuàng)建socket對象
? ? ? ? ? ? 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("連接服務器失敗");
? ? ? ? ? ? }
? ? ? ? ? ? //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;// 設置為后臺線程
? ? ? ? ? ? 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();//關閉連接
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (len <= 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //客戶端正常退出
? ? ? ? ? ? ? ? ? ? msg = String.Format("服務器{0}發(fā)送異常",
? ? ? ? ? ? ? ? ? ?proxSocket.RemoteEndPoint.ToString());
? ? ? ? ? ? ? ? ? ? stopConnect();//關閉連接
? ? ? ? ? ? ? ? ? ? return;//結(jié)束當前接受客戶端數(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)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //如果是跨線程訪問
? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //先判斷是否還有服務器處于連接狀態(tài)
? ? ? ? ? ? ? ? byte[] data = Encoding.Default.GetBytes(textBoxMsg.Text);
? ? ? ? ? ? ? ? clientSocket.Send(data, 0, data.Length, SocketFlags.None);
? ? ? ? ? ? ? ? textBoxMsg.Text = "";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("與服務器斷開連接");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

運行截圖

打開多個運行程序,在項目生成可執(zhí)行文件后,在項目文件目錄下,找到bin\Debug
可以點擊幾個可執(zhí)行程序

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#實現(xiàn)自動填充文字內(nèi)容到指定圖片

    C#實現(xiàn)自動填充文字內(nèi)容到指定圖片

    這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)自動填充文字內(nèi)容到指定圖片,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-04-04
  • C#實現(xiàn)簡單獲取及設置Session類

    C#實現(xiàn)簡單獲取及設置Session類

    這篇文章主要介紹了C#實現(xiàn)簡單獲取及設置Session類,涉及C#針對session的設置及獲取的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C# WebApi CORS跨域問題解決方案

    C# WebApi CORS跨域問題解決方案

    本篇文章主要介紹了C# WebApi CORS跨域問題解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • C#優(yōu)雅的實現(xiàn)INotifyPropertyChanged接口

    C#優(yōu)雅的實現(xiàn)INotifyPropertyChanged接口

    這篇文章介紹了C#實現(xiàn)INotifyPropertyChanged接口的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • 輕松學習C#的基礎入門

    輕松學習C#的基礎入門

    輕松學習C#的基礎入門,了解C#最基本的知識點,C#是一種簡潔的,類型安全的一種完全面向?qū)ο蟮拈_發(fā)語言,是Microsoft專門基于.NET Framework平臺開發(fā)的而量身定做的高級程序設計語言,需要的朋友可以參考下
    2015-11-11
  • c#基礎系列之值類型和引用類型的深入理解

    c#基礎系列之值類型和引用類型的深入理解

    這篇文章主要給大家介紹了關于c#基礎系列之值類型和引用類型的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法

    C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法

    這篇文章主要介紹了C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法,涉及C#類型轉(zhuǎn)換的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C#的File類實現(xiàn)文件操作實例詳解

    C#的File類實現(xiàn)文件操作實例詳解

    這篇文章主要介紹了C#的File類實現(xiàn)文件操作的方法,非常實用,需要的朋友可以參考下
    2014-07-07
  • C#值類型和引用類型的深入理解

    C#值類型和引用類型的深入理解

    本篇文章主要是對C#中值類型和引用類型進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • 淺解關于C#多線程的介紹

    淺解關于C#多線程的介紹

    本篇文章小編將為大家介紹,淺解關于C#的多線程,有需要的朋友可以參考一下
    2013-04-04

最新評論

盐池县| 大埔区| 黄山市| 邻水| 浪卡子县| 汤原县| 汤原县| 安新县| 孝义市| 东源县| 达拉特旗| 韶关市| 凤阳县| 剑川县| 开原市| 囊谦县| 九龙坡区| 万全县| 嘉义县| 天长市| 东至县| 华安县| 塔河县| 阜城县| 河西区| 清涧县| 灵山县| 湘乡市| 大连市| 定州市| 波密县| 吴川市| 深州市| 陆丰市| 怀化市| 开鲁县| 库车县| 安丘市| 桓仁| 峡江县| 涞源县|