資源簡介
C#使用Socket實現服務器與多個客戶端通信(簡單的聊天系統)

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Net;
using?System.IO;
using?System.Web;
using?System.Data;
using?System.Collections;
using?System.Data.SqlClient;
using?System.Net.Sockets;
using?System.Threading;
namespace?WcfConsoleTest
{
????class?Program
????{
????????//創建1個客戶端套接字和1個負責監聽服務端請求的線程??
????????static?Thread?ThreadClient?=?null;
????????static?Socket?SocketClient?=?null;
????????static?void?Main(string[]?args)
????????{
????????????try
????????????{
????????????????int?port?=?6000;
????????????????string?host?=?“127.0.0.1“;//服務器端ip地址
????????????????IPAddress?ip?=?IPAddress.Parse(host);
????????????????IPEndPoint?ipe?=?new?IPEndPoint(ip?port);
????????????????//定義一個套接字監聽??
????????????????SocketClient?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????try
????????????????{
????????????????????//客戶端套接字連接到網絡節點上,用的是Connect??
????????????????????SocketClient.Connect(ipe);
????????????????}
????????????????catch?(Exception)
????????????????{
????????????????????Console.WriteLine(“連接失敗!\r\n“);
????????????????????Console.ReadLine();
????????????????????return;
????????????????}
????????????????ThreadClient?=?new?Thread(Recv);
????????????????ThreadClient.IsBackground?=?true;
????????????????ThreadClient.Start();
????????????????Thread.Sleep(1000);
????????????????Console.WriteLine(“請輸入內容<按Enter鍵發送>:\r\n“);
????????????????while(true)
????????????????{
????????????????????string?sendStr?=?Console.ReadLine();
????????????????????ClientSendMsg(sendStr);
????????????????}
????????????????//int?i?=?1;
????????????????//while?(true)
????????????????//{
????????????????//????Console.Write(“請輸入內容:“);
????????????????//????string?sendStr?=?Console.ReadLine();
????????????????//????Socket?clientSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????//????clientSocket.Connect(ipe);
????????????????//????//send?message
????????????????//????//byte[]?sendBytes?=?Encoding.ASCII.GetBytes(sendStr);
????????????????//????byte[]?sendBytes?=?Encoding.GetEncoding(“utf-8“).GetBytes(sendStr);
????????????????//????//Thread.Sleep(4000);
????????????????//????clientSocket.Send(sendBytes);
????????????????//????//receive?message
????????????????//????string?recStr?=?““;?
????????????????//????byte[]?recBytes?=?new?byte[4096];
????????????????//????int?bytes?=?clientSocket.Receive(recBytes?recBytes.Length?0);
????????????????//????//recStr?+=?Encoding.ASCII.GetString(recBytes?0?bytes);
????????????????//????recStr?+=?Encoding.GetEncoding(“utf-8“).GetString(recBytes?0?bytes);
????????????????//????Console.WriteLine(recStr);
????????????????//????clientSocket.Close();
????????????????//????if?(i?>=?100)
????????????????//????{
????????????????//????????break;
????????????????//????}
????????????????//????i++;
????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????147??2017-09-30?09:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\app.config
?????文件???????1829??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.application
?????文件???????7680??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.exe
?????文件????????147??2017-09-30?09:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.exe.config
?????文件???????3419??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.exe.manifest
?????文件??????17920??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.pdb
?????文件???????1829??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.vshost.application
?????文件??????11600??2018-01-22?08:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.vshost.exe
?????文件????????147??2017-09-30?09:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.vshost.exe.config
?????文件???????3419??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\WcfConsoleTest.vshost.exe.manifest
?????文件???????1754??2017-10-26?11:28??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6172??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????5120??2017-09-29?16:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\TempPE\Service?References.MyWCFReference.Reference.cs.dll
?????文件???????3584??2017-10-26?11:28??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\TempPE\Service?References.ServiceReference1.Reference.cs.dll
?????文件???????3584??2017-10-09?16:27??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\TempPE\Service?References.UserInfoClientNS.Reference.cs.dll
?????文件???????1829??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.application
?????文件???????1104??2018-01-22?08:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.csproj.FileListAbsolute.txt
?????文件???????2215??2017-12-22?13:51??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.csprojResolveAssemblyReference.cache
?????文件???????7680??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.exe
?????文件???????3419??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.exe.manifest
?????文件??????17920??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\WcfConsoleTest.pdb
?????文件???????6177??2017-12-05?17:31??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????1829??2017-12-05?17:31??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\WcfConsoleTest.application
?????文件????????984??2017-12-05?17:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\WcfConsoleTest.csproj.FileListAbsolute.txt
?????文件???????6656??2017-12-05?17:31??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\WcfConsoleTest.exe
?????文件???????3419??2017-12-05?17:31??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\WcfConsoleTest.exe.manifest
?????文件??????15872??2017-12-05?17:31??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Release\WcfConsoleTest.pdb
?????文件??????10147??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Program.cs
?????文件???????1378??2017-09-29?16:35??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Properties\AssemblyInfo.cs
?????文件???????4919??2017-12-21?11:30??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\WcfConsoleTest.csproj
............此處省略44個文件信息
評論
共有 條評論