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

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

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

本文實(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)文章

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

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

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

    C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類

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

    C# WebApi CORS跨域問(wèn)題解決方案

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

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

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

    輕松學(xué)習(xí)C#的基礎(chǔ)入門

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

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

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

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

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

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

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

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

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

    淺解關(guān)于C#多線程的介紹

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

最新評(píng)論

澄迈县| 竹北市| 灵宝市| 偃师市| 惠东县| 京山县| 抚顺市| 大城县| 普兰县| 福建省| 固阳县| 措美县| 南丰县| 武清区| 鹤壁市| 乌兰县| 法库县| 平武县| 永胜县| 宜川县| 修武县| 宁远县| 湖南省| 宁河县| 互助| 泰宁县| 龙里县| 垣曲县| 阳谷县| 望城县| 西昌市| 菏泽市| 休宁县| 三穗县| 兴宁市| 竹山县| 新余市| 海门市| 波密县| 天台县| 阿克陶县|