資源簡介
C/S模式的TCP聊天室程序
本程序用C#實現,代碼附帶注釋
客戶端同時也是服務器
用于局域網內各臺機器間通信
提醒:本程序為點對點通信,不是聊天室,還望加以區分
(俺不能為了分而模糊俺的東東。。)

代碼片段和文件信息
using?System;
using?System.Net;
using?System.Threading;
using?System.Net.Sockets;
using?System.IO;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?SocketsTest
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????????this.skinEngine1.SkinFile?=?“MSN.ssk“;??
????????????IPHostEntry?ipHostEntry?=?Dns.GetHostByName(Dns.GetHostName());??//獲得本機IP
????????????IPAddress?addr?=?new?IPAddress(ipHostEntry.AddressList[0].Address);??//轉化為*.*.*.*格式
????????????this.toolStripStatusLabel3.Text?=?addr.ToString();
????????????
????????}
????????private?Thread?listenThread;??//監聽線程
????????private?bool?listen?=?false;?????//監聽狀態
????????private?TcpListener?tcpListener;?????//網絡監聽對象
????????private?void?StartListen()
????????{
????????????try
????????????{
????????????????tcpListener?=?new?TcpListener(1314);??//?自定義端口
????????????????tcpListener.Start();?????????//開始監聽
????????????????toolStripStatusLabel1.Text?=?“開始監聽.......“;
????????????????this.listen?=?true;???????
????????????????while?(listen)
????????????????{
????????????????????Socket?s?=?tcpListener.AcceptSocket();??//接收掛起的鏈接請求
????????????????????Byte[]?stream?=?new?Byte[100];?????????????//設置緩沖區
????????????????????int?i?=?s.Receive(stream);???????????????????????//int
????????????????????string?message?=?System.Text.Encoding.UTF8.GetString(stream);//將獲得的流轉化為字符串
????????????????????this.richTextBox2.AppendText(message);
????????????????}
????????????}
????????????catch?(Exception?error)
????????????{
????????????????MessageBox.Show(error.Message);
????????????????toolStripStatusLabel1.Text?=?“已停止監聽!“;
????????????}
????????}
????????//================================================================
????????private?void?button1_Click(object?sender?EventArgs?e)??????????????????//發送
????????{
????????????try
????????????{
????????????????string?msg?=?“<“?+?this.textBox1.Text.Trim()?+?“>:“?+?this.richTextBox1.Text?+?“\n“;
????????????????TcpClient?tcpClient?=?new?TcpClient(textBox2.Text?1314);?????????//根據目標計算機地址建立連接???IP:PORT
????????????????NetworkStream?tcpStream?=?tcpClient.GetStream();??????????????????//獲得用于網絡訪問的數據流
????????????????StreamWriter?streamW?=?new?StreamWriter(tcpStream);
????????????????streamW.Write(msg);????????????????????????????????????????????????????????????????//將數據寫入數據流
????????????????streamW.Flush();??????????????????????????????????????????????????????????????????????//將緩沖數據寫入網絡流,同時清空緩沖區
????????????????tcpStream.Close();
????????????????tcpClient.Close();
????????????????this.richTextBox2.AppendText(msg);
????????????????this.richTextBox1.Clear();
????????????????toolStripStatusLabel1.Text?=?“開始監聽.......“;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.M
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5612??2009-12-21?19:04??SocketsTest\SocketsTest\Properties\Resources.resx
?????文件????????249??2009-12-21?19:04??SocketsTest\SocketsTest\Properties\Settings.settings
?????文件???????1354??2009-12-21?19:04??SocketsTest\SocketsTest\Properties\AssemblyInfo.cs
?????文件???????2872??2009-12-21?19:04??SocketsTest\SocketsTest\Properties\Resources.Designer.cs
?????文件???????1096??2009-12-21?19:04??SocketsTest\SocketsTest\Properties\Settings.Designer.cs
?????文件???????3917??2009-12-26?13:20??SocketsTest\SocketsTest\SocketsTest.csproj
?????文件????????490??2009-06-11?05:14??SocketsTest\SocketsTest\bin\Debug\SocketsTest.vshost.exe.manifest
?????文件??????14328??2010-01-09?17:03??SocketsTest\SocketsTest\bin\Debug\SocketsTest.vshost.exe
?????文件??????12800??2009-12-26?13:29??SocketsTest\SocketsTest\bin\Debug\SocketsTest.exe
?????文件??????26112??2009-12-26?13:29??SocketsTest\SocketsTest\bin\Debug\SocketsTest.pdb
?????文件?????516096??2006-08-18?11:50??SocketsTest\SocketsTest\bin\Debug\IrisSkin2.dll
?????文件??????16292??2006-03-22?05:55??SocketsTest\SocketsTest\bin\Debug\MSN.ssk
?????文件??????15156??2006-03-22?05:55??SocketsTest\SocketsTest\bin\Debug\MacOS.ssk
?????文件??????15928??2006-07-18?23:12??SocketsTest\SocketsTest\bin\Debug\Vista2_color7.ssk
?????文件????????180??2009-12-26?13:29??SocketsTest\SocketsTest\obj\Debug\SocketsTest.Form1.resources
?????文件????????180??2009-12-26?13:20??SocketsTest\SocketsTest\obj\Debug\SocketsTest.Properties.Resources.resources
?????文件??????12800??2009-12-26?13:29??SocketsTest\SocketsTest\obj\Debug\SocketsTest.exe
?????文件????????536??2010-01-09?17:03??SocketsTest\SocketsTest\obj\Debug\SocketsTest.csproj.FileListAbsolute.txt
?????文件??????26112??2009-12-26?13:29??SocketsTest\SocketsTest\obj\Debug\SocketsTest.pdb
?????文件???????5661??2009-12-26?13:20??SocketsTest\SocketsTest\obj\Debug\ResolveAssemblyReference.cache
?????文件????????847??2009-12-26?13:29??SocketsTest\SocketsTest\obj\Debug\SocketsTest.csproj.GenerateResource.Cache
?????文件????????492??2009-12-21?19:04??SocketsTest\SocketsTest\Program.cs
?????文件???????6214??2009-12-26?13:29??SocketsTest\SocketsTest\Form1.resx
?????文件???????9144??2009-12-26?13:29??SocketsTest\SocketsTest\Form1.Designer.cs
?????文件???????4914??2009-12-26?13:29??SocketsTest\SocketsTest\Form1.cs
?????文件????????923??2009-12-21?19:04??SocketsTest\SocketsTest.sln
????..A..H.?????15360??2010-01-09?17:03??SocketsTest\SocketsTest.suo
?????目錄??????????0??2009-12-21?19:04??SocketsTest\SocketsTest\obj\Debug\TempPE
?????目錄??????????0??2009-12-21?19:04??SocketsTest\SocketsTest\bin\Debug
?????目錄??????????0??2009-12-21?19:04??SocketsTest\SocketsTest\obj\Debug
............此處省略8個文件信息
- 上一篇:wpf ruler 標尺控件 源碼
- 下一篇:C#做的隨機分組小程序亦可用于抽簽搖獎
評論
共有 條評論