資源簡介
類似于QQ的簡易聊天工具,QQ的主要通信功能以實現,適合新手練習的,

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;//網絡數據傳輸
using?System.Net.Sockets;
namespace?Client
{
????public?partial?class?ClientMain?:?Form
????{
????????public?ClientMain()
????????{
????????????InitializeComponent();
????????}
????????private?IPEndPoint?ServerInfo;
????????private?Socket?ClientSocket;
????????//信息接收緩存
????????private?Byte[]?MsgBuffer;
????????//信息發送存儲
????????private?Byte[]?MsgSend;
????????private?void?ClientMain_Load(object?sender?EventArgs?e)
????????{
????????????this.CmdSend.Enabled?=?false;
????????????this.CmdExit.Enabled?=?false;
????????????//定義一個IPV4,TCP模式的Socket
????????????ClientSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????MsgBuffer?=?new?Byte[65535];
????????????MsgSend?=?new?Byte[65535];
????????????//允許子線程刷新數據
????????????CheckForIllegalCrossThreadCalls?=?false;
????????????this.RecieveMsg.Text?=?Environment.MachineName;
????????}
????????private?void?CmdEnter_Click(object?sender?EventArgs?e)
????????{
????????????//服務端IP和端口信息設定這里的IP可以是127.0.0.1,可以是本機局域網IP,也可以是本機網絡IP
????????????ServerInfo?=?new?IPEndPoint(IPAddress.Parse(this.ServerIP.Text)?Convert.ToInt32(this.ServerPort.Text));
????????????try
????????????{
????????????????//客戶端連接服務端指定IP端口,Sockket
????????????????ClientSocket.Connect(ServerInfo);
????????????????//將用戶登錄信息發送至服務器,由此可以讓其他客戶端獲知
????????????????ClientSocket.Send(Encoding.Unicode.GetBytes(“用戶:?“?+?this.RecieveMsg.Text?+?“?進入系統!\n“));
????????????????//開始從連接的Socket異步讀取數據。接收來自服務器,其他客戶端轉發來的信息
????????????????//AsyncCallback引用在異步操作完成時調用的回調方法
????????????????ClientSocket.BeginReceive(MsgBuffer?0?MsgBuffer.Length?SocketFlags.None?new?AsyncCallback(ReceiveCallBack)?null);
????????????????this.SysMsg.Text?+=?“登錄服務器成功!\n“;
????????????????this.CmdSend.Enabled?=?true;
????????????????this.CmdEnter.Enabled?=?false;
????????????????this.CmdExit.Enabled?=?true;
????????????}
????????????catch
????????????{
????????????????MessageBox.Show(“登錄服務器失敗,請確認服務器是否正常工作!“);
????????????}
????????}
????????private?void?ReceiveCallBack(IAsyncResult?AR)
????????{
????????????try
????????????{
????????????????//結束掛起的異步讀取,返回接收到的字節數。?AR,它存儲此異步操作的狀態信息以及所有用戶定義數據
????????????????int?REnd?=?ClientSocket.EndReceive(AR);
????????????????lock?(this.RecieveMsg)
????????????????{????????????????????
????????????????????this.RecieveMsg.AppendText(Encoding.Unicode.GetString(MsgBuffer?0?REnd));
????????????????}
????????????????ClientSocket.BeginReceive(MsgBuffer?0?MsgBuffer.Length?0?new?AsyncCallback(ReceiveCallBack)?null);
????????????}
????????????catch
????????????{
????????????????MessageBox.Show(“已經與服務器斷開連接!“);
????????????????this.Close();
????????????}
????????}
????????//點擊發送之后沒有直接添加到信息列表中,而是傳到服務器,由服務器轉發給每個客戶端
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????28958??2012-09-28?11:00??5.Socket\C#?Socket?.txt
?????文件??????12800??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.exe
?????文件??????24064??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.pdb
?????文件??????11600??2017-07-25?16:14??5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.vshost.exe
?????文件????????490??2015-06-04?12:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\bin\Debug\Client.vshost.exe.manifest
?????文件???????3696??2012-09-24?17:53??5.Socket\Prj_Socket_Chat_聊天通信\Client\Client.csproj
?????文件???????5195??2012-09-28?11:52??5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.cs
?????文件???????8198??2012-09-24?17:58??5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.Designer.cs
?????文件???????5817??2012-09-24?17:58??5.Socket\Prj_Socket_Chat_聊天通信\Client\ClientMain.resx
?????文件????????180??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.ClientMain.resources
?????文件???????2002??2017-07-25?16:14??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.csproj.FileListAbsolute.txt
?????文件??????12800??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.exe
?????文件??????24064??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.pdb
?????文件????????180??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\Client.Properties.Resources.resources
?????文件???????2152??2012-09-28?11:17??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6426??2017-07-25?16:14??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????578??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\GenerateResource.read.1.tlog
?????文件???????1346??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Client\obj\x86\Debug\GenerateResource.write.1.tlog
?????文件????????492??2012-09-24?17:35??5.Socket\Prj_Socket_Chat_聊天通信\Client\Program.cs
?????文件???????1364??2012-09-24?17:34??5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\AssemblyInfo.cs
?????文件???????2862??2012-09-24?17:34??5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Resources.Designer.cs
?????文件???????5612??2012-09-24?17:34??5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Resources.resx
?????文件???????1091??2012-09-24?17:34??5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Settings.Designer.cs
?????文件????????249??2012-09-24?17:34??5.Socket\Prj_Socket_Chat_聊天通信\Client\Properties\Settings.settings
?????文件???????1300??2012-09-24?17:39??5.Socket\Prj_Socket_Chat_聊天通信\Prj_Socket.sln
????..A..H.?????37376??2012-09-28?11:52??5.Socket\Prj_Socket_Chat_聊天通信\Prj_Socket.suo
?????文件??????11776??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.exe
?????文件??????34304??2012-09-24?20:48??5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.pdb
?????文件??????11600??2012-09-24?17:45??5.Socket\Prj_Socket_Chat_聊天通信\Server\bin\Debug\Server.vshost.exe
?????文件???????2152??2012-09-24?17:33??5.Socket\Prj_Socket_Chat_聊天通信\Server\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
............此處省略119個文件信息
- 上一篇:加油站系統
- 下一篇:《鍵控數據采集電路設計》課程設計方案
評論
共有 條評論