資源簡介
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++;
????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\
?????文件???????10147??2018-01-19?16:48??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Program.cs
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Properties\
?????文件????????1378??2017-09-29?16:35??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Properties\AssemblyInfo.cs
?????目錄???????????0??2019-01-16?09:27??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\Service?References\
?????文件????????4919??2017-12-21?11:30??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\WcfConsoleTest.csproj
?????文件?????????452??2017-12-05?17:30??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\WcfConsoleTest.csproj.user
?????文件?????????147??2017-09-30?09:37??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\app.config
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\bin\Debug\
?????文件????????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
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\
?????文件????????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
?????目錄???????????0??2018-08-29?14:45??C#使用Socket實現服務器與多個客戶端通信\客戶端\WcfConsoleTest\WcfConsoleTest\obj\x86\Debug\TempPE\
?????文件????????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
............此處省略40個文件信息
- 上一篇:用C#語言編寫計算器
- 下一篇:學生信息管理系統源代碼 內含C#編寫源代碼
評論
共有 條評論