資源簡介
C# Socket服務端向指定的客戶端發送消息(包含服務器)

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Net.Sockets;
using?System.Net;
using?System.Threading;
using?System.Windows.Forms;
using?System.IO;
namespace?C0710_CharRoom_Client
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????????TextBox.CheckForIllegalCrossThreadCalls?=?false;
????????}
????????Socket?sokClient?=?null;//負責與服務端通信的套接字
????????Thread?threadClient?=?null;//負責?監聽?服務端發送來的消息的線程
????????bool?isRec?=?true;//是否循環接收服務端數據
????????private?void?btnConnect_Click(object?sender?EventArgs?e)
????????{
????????????//實例化?套接字
????????????sokClient?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????//創建?ip對象
????????????IPAddress?address?=?IPAddress.Parse(txtIP.Text.Trim());
????????????//創建網絡節點對象?包含?ip和port
????????????IPEndPoint?endpoint?=?new?IPEndPoint(address?int.Parse(txtPort.Text.Trim()));
????????????//連接?服務端監聽套接字
????????????sokClient.Connect(endpoint);
????????????
????????????//創建負責接收?服務端發送來數據的?線程
????????????threadClient?=?new?Thread(ReceiveMsg);
????????????threadClient.IsBackground?=?true;
????????????//如果在win7下要通過?某個線程?來調用?文件選擇框的代碼,就需要設置如下
????????????threadClient.SetApartmentState(ApartmentState.STA);
????????????threadClient.Start();
????????}
????????///?
????????///?接收服務端發送來的消息數據
????????///?
????????void?ReceiveMsg()
????????{
????????????while?(isRec)
????????????{
????????????????byte[]?msgArr?=?new?byte[1024?*?1024?*?1];//接收到的消息的緩沖區
????????????????int?length=0;
????????????????//接收服務端發送來的消息數據
????????????????length?=sokClient.Receive(msgArr);//Receive會阻斷線程
????????????????if?(msgArr[0]?==?0)//發送來的是文字
????????????????{
????????????????????string?strMsg?=?System.Text.Encoding.UTF8.GetString(msgArr?1?length?-?1);
????????????????????txtShow.AppendText(strMsg?+?“\r\n“);
????????????????}
????????????????else?if?(msgArr[0]?==?1)?{?//發送來的是文件
????????????????????SaveFileDialog?sfd?=?new?SaveFileDialog();
????????????????????//彈出文件保存選擇框
????????????????????if?(sfd.ShowDialog()?==?System.Windows.Forms.DialogResult.OK)
????????????????????{
????????????????????????//創建文件流
????????????????????????using?(FileStream?fs?=?new?FileStream(sfd.FileName?FileMode.OpenOrCreate))
????????????????????????{
????????????????????????????fs.Write(msgArr?1?length?-?1);
????????????????????????????MessageBox.Show(“文件保存成功!“);
????????????????????????}
????????????????????}
????????????????}
????????????????else?if?(msgArr[0]?==?2)?{
????????????????????ShakeWindow();
????????????????}
????????????}
????????}
????????///?
????????///?閃屏
????????///?
????????private?void?ShakeWindow()
????????{
????????????Random?ran?=?new?Random();
????????????//保存?窗體原坐標
????????????System.Drawing.Point?point?=?this.Location;
????????????for?(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????11776??2011-08-22?16:39??C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.exe
?????文件??????28160??2011-08-22?16:39??C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.pdb
?????文件??????11608??2011-10-17?18:38??C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.vshost.exe
?????文件????????490??2010-03-17?22:39??C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.vshost.exe.manifest
?????文件???????3594??2011-08-20?16:56??C0710_CharRoom_Client\C0710_CharRoom_Client.csproj
?????文件???????4074??2011-08-22?16:11??C0710_CharRoom_Client\Form1.cs
?????文件???????7608??2011-08-22?11:14??C0710_CharRoom_Client\Form1.Designer.cs
?????文件???????5817??2011-08-22?11:14??C0710_CharRoom_Client\Form1.resx
?????文件???????1531??2011-10-17?18:38??C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.csproj.FileListAbsolute.txt
?????文件??????11776??2011-08-22?16:39??C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.exe
?????文件????????180??2011-08-22?11:28??C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.Form1.resources
?????文件??????28160??2011-08-22?16:39??C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.pdb
?????文件????????180??2011-08-20?16:56??C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.Properties.Resources.resources
?????文件???????6065??2011-10-17?18:27??C0710_CharRoom_Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????574??2011-08-22?11:28??C0710_CharRoom_Client\obj\x86\Debug\GenerateResource-ResGen.read.1.tlog
?????文件????????986??2011-08-22?11:28??C0710_CharRoom_Client\obj\x86\Debug\GenerateResource-ResGen.write.1.tlog
?????文件????????502??2011-08-20?16:52??C0710_CharRoom_Client\Program.cs
?????文件???????1386??2011-08-20?16:52??C0710_CharRoom_Client\Properties\AssemblyInfo.cs
?????文件???????2892??2011-08-20?16:52??C0710_CharRoom_Client\Properties\Resources.Designer.cs
?????文件???????5612??2011-08-20?16:52??C0710_CharRoom_Client\Properties\Resources.resx
?????文件???????1106??2011-08-20?16:52??C0710_CharRoom_Client\Properties\Settings.Designer.cs
?????文件????????249??2011-08-20?16:52??C0710_CharRoom_Client\Properties\Settings.settings
?????文件??????15360??2011-08-22?16:39??C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.exe
?????文件??????36352??2011-08-22?16:39??C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.pdb
?????文件??????11608??2011-10-17?18:38??C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.vshost.exe
?????文件????????490??2010-03-17?22:39??C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.vshost.exe.manifest
?????文件???????3681??2011-08-22?11:28??C0710_ChatRoom_Server\C0710_ChatRoom_Server.csproj
?????文件???????4781??2011-08-22?16:10??C0710_ChatRoom_Server\ConnectionClient.cs
?????文件????????186??2011-08-22?11:29??C0710_ChatRoom_Server\DGShowMsg.cs
?????文件???????6988??2011-08-22?15:46??C0710_ChatRoom_Server\Form1.cs
............此處省略36個文件信息
評論
共有 條評論