C#實(shí)現(xiàn)簡易多人聊天室
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡易多人聊天室的具體代碼,供大家參考,具體內(nèi)容如下
只有一個(gè)群聊的功能
服務(wù)端

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.Threading.Tasks;
using System.Windows.Forms;
namespace FinalChatRoomClient
{
? ? public partial class Client : Form
? ? {
? ? ? ? //客戶端負(fù)責(zé)接收服務(wù)端發(fā)來的數(shù)據(jù)消息的線程
? ? ? ? Thread threadClient = null;
? ? ? ? //創(chuàng)建客戶端套接字,負(fù)責(zé)連接服務(wù)器
? ? ? ? Socket socketClient = null;
? ? ? ? public Client()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? //關(guān)閉對文本框跨線程操作的檢查
? ? ? ? ? ? TextBox.CheckForIllegalCrossThreadCalls = false;
? ? ? ? }
? ? ? ? private void start_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //獲得文本框中的IP地址對象
? ? ? ? ? ? IPAddress address = IPAddress.Parse(txtIp.Text.Trim());
? ? ? ? ? ? //創(chuàng)建包含IP和端口的網(wǎng)絡(luò)節(jié)點(diǎn)對象
? ? ? ? ? ? IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim()));
? ? ? ? ? ? //創(chuàng)建客戶端套接字,負(fù)責(zé)連接服務(wù)器
? ? ? ? ? ? socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //客戶端連接到服務(wù)器
? ? ? ? ? ? ? ? socketClient.Connect(endPoint);
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器成功");
? ? ? ? ? ? }
? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? threadClient = new Thread(ReceiveMsg);
? ? ? ? ? ? threadClient.IsBackground = true;
? ? ? ? ? ? threadClient.Start();
? ? ? ? }
? ? ? ? private void btnSend_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? string strMsg = txtMsg.Text.Trim();
? ? ? ? ? ? //將字符串轉(zhuǎn)成方便網(wǎng)絡(luò)傳送的二進(jìn)制數(shù)組
? ? ? ? ? ? byte[] arrMsg = Encoding.UTF8.GetBytes(strMsg);
? ? ? ? ? ? byte[] arrMsgSend = new byte[arrMsg.Length + 1];
? ? ? ? ? ? arrMsgSend[0] = 0;//設(shè)置標(biāo)識(shí)位,0代表發(fā)送的是文字
? ? ? ? ? ? Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? socketClient.Send(arrMsgSend);
? ? ? ? ? ? ? ? //清空發(fā)送消息文本框中的消息
? ? ? ? ? ? ? ? this.txtMsg.Text = "";
? ? ? ? ? ? }
? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端發(fā)送消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端發(fā)送消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void ShowMsg(string msg)
? ? ? ? {
? ? ? ? ? ? txtRecord.AppendText(msg + "\r\n");
? ? ? ? }
? ? ? ? private void ReceiveMsg()
? ? ? ? {
? ? ? ? ? ? while (true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個(gè)接收消息用的字節(jié)數(shù)組緩沖區(qū)(2M大?。?
? ? ? ? ? ? ? ? byte[] arrMsgRev = new byte[1024 * 1024 * 2];
? ? ? ? ? ? ? ? //將接收到的數(shù)據(jù)存入arrMsgRev,并返回真正接收到數(shù)據(jù)的長度
? ? ? ? ? ? ? ? int length = -1;
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? length = socketClient.Receive(arrMsgRev);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ShowMsg("客戶端接收消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("客戶端接收消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //此時(shí)是將數(shù)組的所有元素(每個(gè)字節(jié))都轉(zhuǎn)成字符串,而真正接收到只有服務(wù)端發(fā)來的幾個(gè)字符
? ? ? ? ? ? ? ? string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 0, length);
? ? ? ? ? ? ? ? Console.WriteLine(strMsgReceive);
? ? ? ? ? ? ? ? ShowMsg(strMsgReceive);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}客戶端

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.Threading.Tasks;
using System.Windows.Forms;
namespace FinalChatRoomClient
{
? ? public partial class Client : Form
? ? {
? ? ? ? //客戶端負(fù)責(zé)接收服務(wù)端發(fā)來的數(shù)據(jù)消息的線程
? ? ? ? Thread threadClient = null;
? ? ? ? //創(chuàng)建客戶端套接字,負(fù)責(zé)連接服務(wù)器
? ? ? ? Socket socketClient = null;
? ? ? ? public Client()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? //關(guān)閉對文本框跨線程操作的檢查
? ? ? ? ? ? TextBox.CheckForIllegalCrossThreadCalls = false;
? ? ? ? }
? ? ? ? private void start_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //獲得文本框中的IP地址對象
? ? ? ? ? ? IPAddress address = IPAddress.Parse(txtIp.Text.Trim());
? ? ? ? ? ? //創(chuàng)建包含IP和端口的網(wǎng)絡(luò)節(jié)點(diǎn)對象
? ? ? ? ? ? IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim()));
? ? ? ? ? ? //創(chuàng)建客戶端套接字,負(fù)責(zé)連接服務(wù)器
? ? ? ? ? ? socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //客戶端連接到服務(wù)器
? ? ? ? ? ? ? ? socketClient.Connect(endPoint);
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器成功");
? ? ? ? ? ? }
? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端連接服務(wù)器發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? threadClient = new Thread(ReceiveMsg);
? ? ? ? ? ? threadClient.IsBackground = true;
? ? ? ? ? ? threadClient.Start();
? ? ? ? }
? ? ? ? private void btnSend_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? string strMsg = txtMsg.Text.Trim();
? ? ? ? ? ? //將字符串轉(zhuǎn)成方便網(wǎng)絡(luò)傳送的二進(jìn)制數(shù)組
? ? ? ? ? ? byte[] arrMsg = Encoding.UTF8.GetBytes(strMsg);
? ? ? ? ? ? byte[] arrMsgSend = new byte[arrMsg.Length + 1];
? ? ? ? ? ? arrMsgSend[0] = 0;//設(shè)置標(biāo)識(shí)位,0代表發(fā)送的是文字
? ? ? ? ? ? Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? socketClient.Send(arrMsgSend);
? ? ? ? ? ? ? ? //清空發(fā)送消息文本框中的消息
? ? ? ? ? ? ? ? this.txtMsg.Text = "";
? ? ? ? ? ? }
? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端發(fā)送消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ShowMsg("客戶端發(fā)送消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void ShowMsg(string msg)
? ? ? ? {
? ? ? ? ? ? txtRecord.AppendText(msg + "\r\n");
? ? ? ? }
? ? ? ? private void ReceiveMsg()
? ? ? ? {
? ? ? ? ? ? while (true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個(gè)接收消息用的字節(jié)數(shù)組緩沖區(qū)(2M大小)
? ? ? ? ? ? ? ? byte[] arrMsgRev = new byte[1024 * 1024 * 2];
? ? ? ? ? ? ? ? //將接收到的數(shù)據(jù)存入arrMsgRev,并返回真正接收到數(shù)據(jù)的長度
? ? ? ? ? ? ? ? int length = -1;
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? length = socketClient.Receive(arrMsgRev);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ShowMsg("客戶端接收消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("客戶端接收消息時(shí)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //此時(shí)是將數(shù)組的所有元素(每個(gè)字節(jié))都轉(zhuǎn)成字符串,而真正接收到只有服務(wù)端發(fā)來的幾個(gè)字符
? ? ? ? ? ? ? ? string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 0, length);
? ? ? ? ? ? ? ? Console.WriteLine(strMsgReceive);
? ? ? ? ? ? ? ? ShowMsg(strMsgReceive);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Winform學(xué)生信息管理系統(tǒng)各子窗體剖析(3)
這篇文章主要針對Winform學(xué)生信息管理系統(tǒng)各子窗體進(jìn)行剖析,感興趣的小伙伴們可以參考一下2016-05-05
C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng)
這篇文章主要介紹了C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng),類似DVD管理器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
C#實(shí)現(xiàn)json的序列化和反序列化實(shí)例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)json的序列化和反序列化實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
C#?App.config和Web.config加密的實(shí)現(xiàn)步驟
本文介紹了如何使用C#對App.config和Web.config文件進(jìn)行加密,通過使用ConfigurationSection類和SymmetricAlgorithm類,我們可以保護(hù)配置文件中的敏感數(shù)據(jù),確保只有授權(quán)人員可以訪問2023-08-08
httpwebreqeust讀取httponly的cookie方法
下面小編就為大家?guī)硪黄猦ttpwebreqeust讀取httponly的cookie方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Unity3D運(yùn)行報(bào)DllNotFoundException錯(cuò)誤的解決方案
這篇文章主要介紹了Unity3D運(yùn)行報(bào)DllNotFoundException錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

