資源簡(jiǎn)介
VS2010開發(fā)的,使用C#語(yǔ)言,采用多線程,基于Socket的聊天室程序,包括服務(wù)器端和客戶端,學(xué)習(xí)Socekt的代碼,大家互相交流。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Net.Sockets;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.Threading;
namespace?ChatRoomClient
{
????public?partial?class?FormChatClient?:?Form
????{
????????public?FormChatClient()
????????{
????????????InitializeComponent();
????????}
????????private?Thread?threadclient?=?null;//客戶端用于接收數(shù)據(jù)的線程
????????private?Socket?socketClient?=?null;//客戶端用于接收數(shù)據(jù)的套接字
????????private?void?btnConnect_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????IPAddress?ip?=?IPAddress.Parse(this.txtServerIp.Text.Trim());
????????????????IPEndPoint?point?=?new?IPEndPoint(ip?int.Parse(this.txtPort.Text.Trim()));
????????????????socketClient?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????socketClient.Connect(point);//不用監(jiān)聽直接創(chuàng)建連接
????????????????//開啟一個(gè)線程實(shí)例
????????????????threadclient?=?new?Thread(RecMsg);
????????????????//設(shè)為后臺(tái)線程
????????????????threadclient.IsBackground?=?true;
????????????????//啟動(dòng)線程
????????????????threadclient.Start();
????????????????ShowMsg(“---------服務(wù)器連接成功--------“);
????????????}
????????????catch?(Exception)
????????????{
????????????????ShowMsg(“---------服務(wù)器不在線--------“);
????????????}
????????????
????????}
????????//線程中用于接收數(shù)據(jù)的方法
????????void?RecMsg()
????????{
????????????while?(true)
????????????{
????????????????try
????????????????{
????????????????????//創(chuàng)建一個(gè)字節(jié)數(shù)組接收數(shù)據(jù)
????????????????????byte[]?arrMsgRec?=?new?byte[1024?*?1024?*?2];
????????????????????//接收數(shù)據(jù)
????????????????????int?length?=?socketClient.Receive(arrMsgRec);
????????????????????//將字節(jié)數(shù)組轉(zhuǎn)換成字符串此時(shí)是將所有的元素都轉(zhuǎn)成字符串了,而真正接收到的只有服務(wù)端發(fā)來(lái)的幾個(gè)字符。
????????????????????string?strMsgRec?=?System.Text.Encoding.UTF8.GetString(arrMsgRec?0?length);
????????????????????//將字符串顯示到界面上
????????????????????ShowMsg(“服務(wù)器:“+strMsgRec);
????????????????}
????????????????catch(Exception?e)
????????????????{
????????????????????ShowMsg(“-----服務(wù)器已經(jīng)斷開連接!-----“);
????????????????????socketClient.Close();
????????????????????//結(jié)束當(dāng)前線程
????????????????????Thread.CurrentThread.Abort();
????????????????}
????????????}
????????}
????????//顯示信息
????????private?delegate?void?changeText(string?msg);
????????///?
????????///?添加信息到文本框
????????///?
????????///?
????????void?ShowMsg(string?msg)
????????{
????????????if?(this.InvokeRequired)
????????????{
????????????????this.BeginInvoke(new?changeText(ShowMsg)?msg);
????????????}
????????????else
????????????{
????????????????txtMsg.AppendText(msg?+?“\r\n“);
????????????}
????????}
????????///?
????????///?往服務(wù)器發(fā)送數(shù)據(jù)
????????///?
????????///?
????????///?
????????private?void?btnSend_Click(object?sen
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1351??2012-05-21?17:24??聊天室.sln
????.....H.??????2054??2012-05-21?21:55??聊天室.sln.docstates.suo
????..A..H.?????29184??2012-05-21?21:55??聊天室.suo
?????文件??????????8??2012-05-21?21:55??_ReSharper.聊天室\AspFileDataCache.dat
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\Buildsc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\Buildsc
?????文件?????????28??2012-05-21?15:56??_ReSharper.聊天室\Buildsc
?????文件???????2260??2012-05-21?21:55??_ReSharper.聊天室\CompletionStatistics\Statistics.dat
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\ConditionalNames\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\ConditionalNames\.version
?????文件????????576??2012-05-21?15:56??_ReSharper.聊天室\ConditionalNames\2\74cbf11.dat
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\ja
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\ja
?????文件??????52629??2012-05-21?15:56??_ReSharper.聊天室\ja
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\JSTest\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\JSTest\.version
?????文件????????209??2012-05-21?17:20??_ReSharper.聊天室\ModuleIds.xm
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\MVC\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\MVC\.version
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\NamedArguments\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\NamedArguments\.version
?????文件????????472??2012-05-21?15:56??_ReSharper.聊天室\NamedArguments\2\74cbf11.dat
?????文件????????922??2012-05-21?15:56??_ReSharper.聊天室\PersistenIdIndex.bin
?????文件???????3457??2012-05-21?21:55??_ReSharper.聊天室\RecentItems\RecentFiles.dat
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\Resources\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\Resources\.version
?????文件?????????28??2012-05-21?15:56??_ReSharper.聊天室\Resources\3\115c85de.dat
?????文件???????1502??2012-05-21?15:56??_ReSharper.聊天室\SymbolCache.bin
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\TagPrefixes\.crc
?????文件??????????4??2012-05-21?15:56??_ReSharper.聊天室\TagPrefixes\.version
............此處省略263個(gè)文件信息
評(píng)論
共有 條評(píng)論