diff --git a/.gitignore b/.gitignore index d8007ef..ea43584 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,269 @@ +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +#NLog# +**/nlogs/ +*log.*.txt +*.modelproj + +#kdiff3 +*.org + /异步/异步/web服务/bin /异步/异步/web服务/obj /异步/异步/窗体程序/bin diff --git a/IoTClient/ModbusTCP/ModbusTcpClient/ModbusTcpClient.csproj b/IoTClient/ModbusTCP/ModbusTcpClient/ModbusTcpClient.csproj new file mode 100644 index 0000000..958d2f1 --- /dev/null +++ b/IoTClient/ModbusTCP/ModbusTcpClient/ModbusTcpClient.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git a/IoTClient/ModbusTCP/ModbusTcpClient/Program.cs b/IoTClient/ModbusTCP/ModbusTcpClient/Program.cs new file mode 100644 index 0000000..9834b2c --- /dev/null +++ b/IoTClient/ModbusTCP/ModbusTcpClient/Program.cs @@ -0,0 +1,137 @@ +using System; +using System.Linq; +using System.Net; +using System.Net.Sockets; + +namespace ModbusTCP +{ + class Program + { + //Modbus服务端IP + static string ip = ""; + //Modbus服务端端口 + static int port = 502; + static void Main(string[] args) + { + //读取数据 + Read(); + + //写入数据 + Write(); + } + + static void Read() + { + //1 创建Socket + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 建立连接 + socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); + + //3 获取命令(寄存器起始地址、站号、功能码、读取寄存器长度) + byte[] command = GetReadCommand(4, 2, 3, 1); + + //4 发送命令 + socket.Send(command); + + //5 读取响应 + byte[] buffer1 = new byte[8];//先读取前面八个字节(Map报文头) + socket.Receive(buffer1, 0, buffer1.Length, SocketFlags.None); + + //5.1 获取将要读取的数据长度 + int length = buffer1[4] * 256 + buffer1[5] - 2;//减2是因为这个长度数据包括了单元标识符和功能码,占两个字节 + + //5.2 读取数据 + byte[] buffer2 = new byte[length]; + var readLength2 = socket.Receive(buffer2, 0, buffer2.Length, SocketFlags.None); + + byte[] buffer3 = new byte[readLength2 - 1]; + //5.3 过滤第一个字节(第一个字节代表数据的字节个数) + Array.Copy(buffer2, 1, buffer3, 0, buffer3.Length); + var buffer3Reverse = buffer3.Reverse().ToArray(); + var value = BitConverter.ToInt16(buffer3Reverse, 0); + + //6 关闭连接 + socket.Shutdown(SocketShutdown.Both); + socket.Close(); + } + + static void Write() + { + //1 创建Socket + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 建立连接 + socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); + + //值的转换 + short value = 32; + var values = BitConverter.GetBytes(value).Reverse().ToArray(); + + //3 获取并发送命令(寄存器起始地址、站号、功能码) + var command = GetWriteCommand(4, values, 2, 16); + socket.Send(command); + + //4 关闭连接 + socket.Shutdown(SocketShutdown.Both); + socket.Close(); + } + + #region 获取命令 + + /// + /// 获取读取命令 + /// + /// 寄存器起始地址 + /// 站号 + /// 功能码 + /// 读取长度 + /// + public static byte[] GetReadCommand(ushort address, byte stationNumber, byte functionCode, ushort length) + { + byte[] buffer = new byte[12]; + buffer[0] = 0x19; + buffer[1] = 0xB2;//Client发出的检验信息 + buffer[2] = 0x00; + buffer[3] = 0x00;//表示tcp/ip 的协议的modbus的协议 + buffer[4] = 0x00; + buffer[5] = 0x06;//表示的是该字节以后的字节长度 + + buffer[6] = stationNumber; //站号 + buffer[7] = functionCode; //功能码 + buffer[8] = BitConverter.GetBytes(address)[1]; + buffer[9] = BitConverter.GetBytes(address)[0];//寄存器地址 + buffer[10] = BitConverter.GetBytes(length)[1]; + buffer[11] = BitConverter.GetBytes(length)[0];//表示request 寄存器的长度(寄存器个数) + return buffer; + } + + /// + /// 获取写入命令 + /// + /// 寄存器地址 + /// + /// 站号 + /// 功能码 + /// + public static byte[] GetWriteCommand(ushort address, byte[] values, byte stationNumber, byte functionCode) + { + byte[] buffer = new byte[13 + values.Length]; + buffer[0] = 0x19; + buffer[1] = 0xB2;//检验信息,用来验证response是否串数据了 + buffer[4] = BitConverter.GetBytes(7 + values.Length)[1]; + buffer[5] = BitConverter.GetBytes(7 + values.Length)[0];//表示的是header handle后面还有多长的字节 + + buffer[6] = stationNumber; //站号 + buffer[7] = functionCode; //功能码 + buffer[8] = BitConverter.GetBytes(address)[1]; + buffer[9] = BitConverter.GetBytes(address)[0];//寄存器地址 + buffer[10] = (byte)(values.Length / 2 / 256); + buffer[11] = (byte)(values.Length / 2 % 256);//写寄存器数量(除2是两个字节一个寄存器,寄存器16位。除以256是byte最大存储255。) + buffer[12] = (byte)(values.Length); //写字节的个数 + values.CopyTo(buffer, 13); //把目标值附加到数组后面 + return buffer; + } + #endregion + } +} diff --git a/IoTClient/ModbusTCP/ModbusTcpServer/DataPersist.cs b/IoTClient/ModbusTCP/ModbusTcpServer/DataPersist.cs new file mode 100644 index 0000000..d020753 --- /dev/null +++ b/IoTClient/ModbusTCP/ModbusTcpServer/DataPersist.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace ModbusTcpServer +{ + public class DataPersist + { + string prefix; + static Dictionary data = new Dictionary(); + + /// + /// + /// + /// 前缀 + public DataPersist(string prefix) + { + this.prefix = $"iot_{prefix}_"; + } + + /// + /// 读 + /// + /// + /// + public string Read(string key) + { + key = prefix + key; + if (data.Keys.Contains(key)) + { + return data[key]; + } + return string.Empty; + } + + public string Read(int key) + { + return Read(key.ToString()); + } + + /// + /// 写 + /// + /// + /// + public void Write(string key, string value) + { + key = prefix + key; + if (data.Keys.Contains(key)) + { + data[key] = value; + } + else + { + data.Add(key, value); + } + } + + public void Write(int key, string value) + { + Write(key.ToString(), value); + } + } +} diff --git a/IoTClient/ModbusTCP/ModbusTcpServer/ModbusTcpServer.csproj b/IoTClient/ModbusTCP/ModbusTcpServer/ModbusTcpServer.csproj new file mode 100644 index 0000000..958d2f1 --- /dev/null +++ b/IoTClient/ModbusTCP/ModbusTcpServer/ModbusTcpServer.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git a/IoTClient/ModbusTCP/ModbusTcpServer/Program.cs b/IoTClient/ModbusTCP/ModbusTcpServer/Program.cs new file mode 100644 index 0000000..bcbcaef --- /dev/null +++ b/IoTClient/ModbusTCP/ModbusTcpServer/Program.cs @@ -0,0 +1,98 @@ +using System; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace ModbusTcpServer +{ + /// + /// 为了减低代码量,便于理解,这里只能对short类型读写。 + /// 完整实现请参考 https://github.com/zhaopeiym/IoTClient/blob/master/IoTServer/Servers/ModBus/ModBusTcpServer.cs + /// + class Program + { + static void Main(string[] args) + { + new Program().Start(); + Console.WriteLine("ModbusTcpServer 已开启"); + Console.Read(); + } + + //启动服务 + public void Start() + { + //1 创建Socket对象 + var socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 绑定ip和端口 + IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 502); + socketServer.Bind(ipEndPoint); + + //3、开启侦听(等待客户机发出的连接),并设置最大客户端连接数为10 + socketServer.Listen(10); + + Task.Run(() => { Accept(socketServer); }); + } + + //客户端连接到服务端 + void Accept(Socket socket) + { + while (true) + { + //阻塞等待客户端连接 + Socket newSocket = socket.Accept(); + Task.Run(() => { Receive(newSocket); }); + } + } + + //接收客户端发送的消息 + void Receive(Socket newSocket) + { + while (newSocket.Connected) + { + byte[] requetData1 = new byte[8]; + //读取客户端发送报文 报文头 + int readLeng = newSocket.Receive(requetData1, 0, requetData1.Length, SocketFlags.None); + byte[] requetData2 = new byte[requetData1[5] - 2]; + //读取客户端发送报文 报文数据 + readLeng = newSocket.Receive(requetData2, 0, requetData2.Length, SocketFlags.None); + var requetData = requetData1.Concat(requetData2).ToArray(); + + byte[] responseData1 = new byte[8]; + //复制请求报文中的报文头 + Buffer.BlockCopy(requetData, 0, responseData1, 0, responseData1.Length); + //这里可以自己实现一个对象,用来存储客户端写入的数据(也可以用redis等实现数据的持久化) + DataPersist data = new DataPersist(""); + + //根据协议,报文的第八个字节是功能码(前面我们有说过 03:读保持寄存器 16:写多个寄存器) + switch (requetData[7]) + { + //读保持寄存器 + case 3: + { + var value = data.Read(requetData[9]); + short.TryParse(value, out short resultValue); + var bytes = BitConverter.GetBytes(resultValue); + //当前位置到最后的长度 + responseData1[5] = (byte)(3 + bytes.Length); + byte[] responseData2 = new byte[3] { (byte)bytes.Length, bytes[1], bytes[0] }; + var responseData = responseData1.Concat(responseData2).ToArray(); + newSocket.Send(responseData); + } + break; + //写多个寄存器 + case 16: + { + data.Write(requetData[9], requetData[requetData.Length - 1].ToString()); + var responseData = new byte[12]; + Buffer.BlockCopy(requetData, 0, responseData, 0, responseData.Length); + responseData[5] = 6; + newSocket.Send(responseData); + } + break; + } + } + } + } +} diff --git a/IoTClient/ModbusTCP/SocketChat.sln b/IoTClient/ModbusTCP/SocketChat.sln new file mode 100644 index 0000000..a7acada --- /dev/null +++ b/IoTClient/ModbusTCP/SocketChat.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29324.140 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModbusTcpClient", "ModbusTcpClient\ModbusTcpClient.csproj", "{82E5997C-3728-4417-AA78-81E8BEEADA93}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusTcpServer", "ModbusTcpServer\ModbusTcpServer.csproj", "{F0BADBCB-DC4D-443A-8A66-581BCEC1A7AA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {82E5997C-3728-4417-AA78-81E8BEEADA93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82E5997C-3728-4417-AA78-81E8BEEADA93}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82E5997C-3728-4417-AA78-81E8BEEADA93}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82E5997C-3728-4417-AA78-81E8BEEADA93}.Release|Any CPU.Build.0 = Release|Any CPU + {F0BADBCB-DC4D-443A-8A66-581BCEC1A7AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0BADBCB-DC4D-443A-8A66-581BCEC1A7AA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0BADBCB-DC4D-443A-8A66-581BCEC1A7AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0BADBCB-DC4D-443A-8A66-581BCEC1A7AA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5C201C99-8DB1-46F2-994F-AFCD7098B518} + EndGlobalSection +EndGlobal diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketChat.sln" "b/Socket\347\274\226\347\250\213/1Socket/SocketChat.sln" new file mode 100644 index 0000000..c169e34 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketChat.sln" @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29324.140 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Socket服务端", "SocketServer\Socket服务端.csproj", "{2D65F919-F44A-4951-A107-4919A7B22B2E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Socket客户端", "SocketClient\Socket客户端.csproj", "{419EB976-7A53-4CD6-B7C2-5C3A589BD575}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tcp客户端", "TcpClientForm\Tcp客户端.csproj", "{A4783856-A740-4221-B254-97690423510B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tcp服务端", "TcpServerConsole\Tcp服务端.csproj", "{E3B4DA6D-142F-482B-B84F-793C1571F3C3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2D65F919-F44A-4951-A107-4919A7B22B2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D65F919-F44A-4951-A107-4919A7B22B2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D65F919-F44A-4951-A107-4919A7B22B2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D65F919-F44A-4951-A107-4919A7B22B2E}.Release|Any CPU.Build.0 = Release|Any CPU + {419EB976-7A53-4CD6-B7C2-5C3A589BD575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {419EB976-7A53-4CD6-B7C2-5C3A589BD575}.Debug|Any CPU.Build.0 = Debug|Any CPU + {419EB976-7A53-4CD6-B7C2-5C3A589BD575}.Release|Any CPU.ActiveCfg = Release|Any CPU + {419EB976-7A53-4CD6-B7C2-5C3A589BD575}.Release|Any CPU.Build.0 = Release|Any CPU + {A4783856-A740-4221-B254-97690423510B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4783856-A740-4221-B254-97690423510B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4783856-A740-4221-B254-97690423510B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4783856-A740-4221-B254-97690423510B}.Release|Any CPU.Build.0 = Release|Any CPU + {E3B4DA6D-142F-482B-B84F-793C1571F3C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E3B4DA6D-142F-482B-B84F-793C1571F3C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E3B4DA6D-142F-482B-B84F-793C1571F3C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E3B4DA6D-142F-482B-B84F-793C1571F3C3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5C201C99-8DB1-46F2-994F-AFCD7098B518} + EndGlobalSection +EndGlobal diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/App.config" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/App.config" new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/App.config" @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.Designer.cs" new file mode 100644 index 0000000..6c10150 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.Designer.cs" @@ -0,0 +1,145 @@ +namespace SocketClient +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.button1 = new System.Windows.Forms.Button(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.textBox3 = new System.Windows.Forms.TextBox(); + this.button2 = new System.Windows.Forms.Button(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.txtMsg = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(421, 28); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 9; + this.button1.Text = "连接"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(95, 30); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(142, 21); + this.textBox1.TabIndex = 6; + this.textBox1.Text = "127.0.0.1"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(48, 35); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(29, 12); + this.label1.TabIndex = 5; + this.label1.Text = "IP:"; + // + // textBox3 + // + this.textBox3.Location = new System.Drawing.Point(50, 258); + this.textBox3.Multiline = true; + this.textBox3.Name = "textBox3"; + this.textBox3.Size = new System.Drawing.Size(349, 50); + this.textBox3.TabIndex = 10; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(421, 258); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(75, 50); + this.button2.TabIndex = 11; + this.button2.Text = "发送"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(291, 30); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(108, 21); + this.textBox2.TabIndex = 13; + this.textBox2.Text = "50001"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(244, 35); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(41, 12); + this.label2.TabIndex = 12; + this.label2.Text = "端口:"; + // + // txtMsg + // + this.txtMsg.Location = new System.Drawing.Point(50, 71); + this.txtMsg.Multiline = true; + this.txtMsg.Name = "txtMsg"; + this.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.txtMsg.Size = new System.Drawing.Size(446, 169); + this.txtMsg.TabIndex = 14; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(541, 330); + this.Controls.Add(this.txtMsg); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.label2); + this.Controls.Add(this.button2); + this.Controls.Add(this.textBox3); + this.Controls.Add(this.button1); + this.Controls.Add(this.textBox1); + this.Controls.Add(this.label1); + this.Name = "Form1"; + this.Text = "Form1"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button button1; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox textBox3; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox txtMsg; + } +} + diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.cs" new file mode 100644 index 0000000..cbe8373 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.cs" @@ -0,0 +1,73 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SocketClient +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + CheckForIllegalCrossThreadCalls = false; + Text = "未连接"; + } + + private Socket socketClient; + private void button1_Click(object sender, EventArgs e) + { + //1 创建Socket对象 + socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 连接服务端 + IPAddress ip = IPAddress.Parse(textBox1.Text); + IPEndPoint ipEndPoint = new IPEndPoint(ip, int.Parse(textBox2.Text)); + socketClient.Connect(ipEndPoint); + Text = "已经连接"; + //接收来自服务端的消息 + Task.Run(() => { Receive(socketClient); }); + } + + /// + /// 接收来自服务端的消息 + /// + /// + void Receive(Socket socketClient) + { + byte[] data = new byte[1024 * 1024]; + while (true) + { + //读取客户端发送过来的数据 + int readLeng = socketClient.Receive(data, 0, data.Length, SocketFlags.None); + if (readLeng == 0)//客户端断开连接 + { + txtMsg.Text += $"{socketClient.RemoteEndPoint}强行断开连接\r\n"; + //socketClient.Shutdown(SocketShutdown.Both); + //socketClient.Close(); + return; + } + txtMsg.Text += $"{socketClient.RemoteEndPoint}:{Encoding.UTF8.GetString(data, 0, readLeng)}\r\n"; + } + } + + /// + /// 发送消息到服务端 + /// + /// + /// + private void button2_Click(object sender, EventArgs e) + { + socketClient.Send(Encoding.UTF8.GetBytes(textBox3.Text)); + } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + if (socketClient.Connected) + socketClient?.Shutdown(SocketShutdown.Both); + socketClient?.Close(); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.resx" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.resx" new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Form1.resx" @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Program.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Program.cs" new file mode 100644 index 0000000..97731f2 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Program.cs" @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SocketClient +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/AssemblyInfo.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/AssemblyInfo.cs" new file mode 100644 index 0000000..deea646 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/AssemblyInfo.cs" @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SocketClient")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SocketClient")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("419eb976-7a53-4cd6-b7c2-5c3a589bd575")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.Designer.cs" new file mode 100644 index 0000000..33a7440 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.Designer.cs" @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace SocketClient.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SocketClient.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.resx" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.resx" new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Resources.resx" @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.Designer.cs" new file mode 100644 index 0000000..14f4de0 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.Designer.cs" @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SocketClient.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.settings" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.settings" new file mode 100644 index 0000000..3964565 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Properties/Settings.settings" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketClient/Socket\345\256\242\346\210\267\347\253\257.csproj" "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Socket\345\256\242\346\210\267\347\253\257.csproj" new file mode 100644 index 0000000..2690f52 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketClient/Socket\345\256\242\346\210\267\347\253\257.csproj" @@ -0,0 +1,83 @@ + + + + + Debug + AnyCPU + {419EB976-7A53-4CD6-B7C2-5C3A589BD575} + WinExe + SocketClient + SocketClient + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/App.config" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/App.config" new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/App.config" @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.Designer.cs" new file mode 100644 index 0000000..a63ca89 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.Designer.cs" @@ -0,0 +1,157 @@ +namespace SocketServer +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.button1 = new System.Windows.Forms.Button(); + this.listBox1 = new System.Windows.Forms.ListBox(); + this.txtMsg = new System.Windows.Forms.TextBox(); + this.txtContent = new System.Windows.Forms.TextBox(); + this.button2 = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(34, 28); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(29, 12); + this.label1.TabIndex = 0; + this.label1.Text = "IP:"; + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(81, 23); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(142, 21); + this.textBox1.TabIndex = 1; + this.textBox1.Text = "127.0.0.1"; + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(307, 23); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(100, 21); + this.textBox2.TabIndex = 3; + this.textBox2.Text = "50001"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(246, 28); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(41, 12); + this.label2.TabIndex = 2; + this.label2.Text = "端口:"; + // + // button1 + // + this.button1.Location = new System.Drawing.Point(537, 21); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 4; + this.button1.Text = "启动服务"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // listBox1 + // + this.listBox1.FormattingEnabled = true; + this.listBox1.ItemHeight = 12; + this.listBox1.Location = new System.Drawing.Point(25, 66); + this.listBox1.Name = "listBox1"; + this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple; + this.listBox1.Size = new System.Drawing.Size(143, 292); + this.listBox1.TabIndex = 5; + // + // txtMsg + // + this.txtMsg.Location = new System.Drawing.Point(174, 66); + this.txtMsg.Multiline = true; + this.txtMsg.Name = "txtMsg"; + this.txtMsg.Size = new System.Drawing.Size(438, 292); + this.txtMsg.TabIndex = 6; + // + // txtContent + // + this.txtContent.Location = new System.Drawing.Point(25, 378); + this.txtContent.Multiline = true; + this.txtContent.Name = "txtContent"; + this.txtContent.Size = new System.Drawing.Size(492, 40); + this.txtContent.TabIndex = 7; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(523, 378); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(89, 40); + this.button2.TabIndex = 8; + this.button2.Text = "发送"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(643, 431); + this.Controls.Add(this.button2); + this.Controls.Add(this.txtContent); + this.Controls.Add(this.txtMsg); + this.Controls.Add(this.listBox1); + this.Controls.Add(this.button1); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.label2); + this.Controls.Add(this.textBox1); + this.Controls.Add(this.label1); + this.Name = "Form1"; + this.Text = "Form1"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.ListBox listBox1; + private System.Windows.Forms.TextBox txtMsg; + private System.Windows.Forms.TextBox txtContent; + private System.Windows.Forms.Button button2; + } +} + diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.cs" new file mode 100644 index 0000000..e29b520 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.cs" @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SocketServer +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + //https://www.cnblogs.com/Impulse/articles/9320539.html + //如果有两个或多个线程操作某一控件的状态,则可能会迫使该控件进入一种不一致的状态。 + //还可能出现其他与线程相关的 bug,以及不同线程争用控件引起的死锁问题。 + //正式环境应该使用委托(Invoke)的方式访问控件 + CheckForIllegalCrossThreadCalls = false; + } + private Socket socketServer; + private Dictionary dicSocket = new Dictionary(); + private void button1_Click(object sender, EventArgs e) + { + //1 创建Socket对象 + socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 绑定ip和端口 + IPAddress ip = IPAddress.Parse(textBox1.Text); + IPEndPoint ipEndPoint = new IPEndPoint(ip, int.Parse(textBox2.Text)); + socketServer.Bind(ipEndPoint); + + //3、开启侦听(等待客户机发出的连接),并设置最大客户端连接数为10 + socketServer.Listen(10); + txtMsg.Text = "启动服务\r\n"; + Text = "服务已启动"; + + Task.Run(() => { Accept(socketServer); }); + } + + /// + /// 客户端连接到服务端 + /// + /// + void Accept(Socket socket) + { + while (true) + { + //阻塞等待客户端连接 + Socket newSocket = socket.Accept(); + txtMsg.Text += $"{newSocket.RemoteEndPoint}上线了\r\n"; + listBox1.Items.Add(newSocket.RemoteEndPoint); + dicSocket.Add(newSocket.RemoteEndPoint.ToString(), newSocket); + Task.Run(() => { Receive(newSocket); }); + } + } + + /// + /// 接收客户端发送的消息 + /// + /// + void Receive(Socket newSocket) + { + byte[] data = new byte[1024 * 1024]; + while (newSocket.Connected) + { + //读取客户端发送过来的数据 + int readLeng = newSocket.Receive(data, 0, data.Length, SocketFlags.None); + if (readLeng == 0)//客户端断开连接 + { + txtMsg.Text += $"{newSocket.RemoteEndPoint}下线了\r\n"; + listBox1.Items.Remove(newSocket.RemoteEndPoint); + dicSocket.Remove(newSocket.RemoteEndPoint.ToString()); + //停止会话(禁用Socket上的发送和接收,该方法允许Socket对象一直等待,直到将内部缓冲区的数据发送完为止) + newSocket.Shutdown(SocketShutdown.Both); + //关闭连接 + newSocket.Close(); + return; + } + txtMsg.Text += $"{newSocket.RemoteEndPoint}:{Encoding.UTF8.GetString(data, 0, readLeng)}\r\n"; + } + } + + /// + /// 发送消息到客户端 + /// + /// + /// + private void button2_Click(object sender, EventArgs e) + { + foreach (var item in listBox1.SelectedItems) + { + //把消息内容转成字节数组后发送 + dicSocket[item.ToString()].Send(Encoding.UTF8.GetBytes(txtContent.Text)); + } + } + + /// + /// 关闭 + /// + /// + /// + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + foreach (var item in listBox1.Items) + { + dicSocket[item.ToString()].Shutdown(SocketShutdown.Both); + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.resx" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.resx" new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Form1.resx" @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Program.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Program.cs" new file mode 100644 index 0000000..30c9078 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Program.cs" @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SocketServer +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/AssemblyInfo.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/AssemblyInfo.cs" new file mode 100644 index 0000000..7be0fdd --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/AssemblyInfo.cs" @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SocketServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SocketServer")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("2d65f919-f44a-4951-a107-4919a7b22b2e")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.Designer.cs" new file mode 100644 index 0000000..a55c09f --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.Designer.cs" @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace SocketServer.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SocketServer.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.resx" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.resx" new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Resources.resx" @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.Designer.cs" new file mode 100644 index 0000000..dd5bdca --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.Designer.cs" @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SocketServer.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.settings" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.settings" new file mode 100644 index 0000000..3964565 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Properties/Settings.settings" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/Socket\347\274\226\347\250\213/1Socket/SocketServer/Socket\346\234\215\345\212\241\347\253\257.csproj" "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Socket\346\234\215\345\212\241\347\253\257.csproj" new file mode 100644 index 0000000..bb907c3 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/SocketServer/Socket\346\234\215\345\212\241\347\253\257.csproj" @@ -0,0 +1,83 @@ + + + + + Debug + AnyCPU + {2D65F919-F44A-4951-A107-4919A7B22B2E} + WinExe + SocketServer + SocketServer + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/App.config" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/App.config" new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/App.config" @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.Designer.cs" new file mode 100644 index 0000000..1031aab --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.Designer.cs" @@ -0,0 +1,145 @@ +namespace TcpClientForm +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.txtMsg = new System.Windows.Forms.TextBox(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.button2 = new System.Windows.Forms.Button(); + this.textBox3 = new System.Windows.Forms.TextBox(); + this.button1 = new System.Windows.Forms.Button(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // txtMsg + // + this.txtMsg.Location = new System.Drawing.Point(27, 60); + this.txtMsg.Multiline = true; + this.txtMsg.Name = "txtMsg"; + this.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.txtMsg.Size = new System.Drawing.Size(446, 169); + this.txtMsg.TabIndex = 22; + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(268, 19); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(108, 21); + this.textBox2.TabIndex = 21; + this.textBox2.Text = "9999"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(221, 24); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(41, 12); + this.label2.TabIndex = 20; + this.label2.Text = "端口:"; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(398, 247); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(75, 50); + this.button2.TabIndex = 19; + this.button2.Text = "发送"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // textBox3 + // + this.textBox3.Location = new System.Drawing.Point(27, 247); + this.textBox3.Multiline = true; + this.textBox3.Name = "textBox3"; + this.textBox3.Size = new System.Drawing.Size(349, 50); + this.textBox3.TabIndex = 18; + // + // button1 + // + this.button1.Location = new System.Drawing.Point(398, 17); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 17; + this.button1.Text = "连接"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(72, 19); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(142, 21); + this.textBox1.TabIndex = 16; + this.textBox1.Text = "127.0.0.1"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(25, 24); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(29, 12); + this.label1.TabIndex = 15; + this.label1.Text = "IP:"; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(514, 320); + this.Controls.Add(this.txtMsg); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.label2); + this.Controls.Add(this.button2); + this.Controls.Add(this.textBox3); + this.Controls.Add(this.button1); + this.Controls.Add(this.textBox1); + this.Controls.Add(this.label1); + this.Name = "Form1"; + this.Text = "Form1"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox txtMsg; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.TextBox textBox3; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label1; + } +} + diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.cs" new file mode 100644 index 0000000..503592d --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.cs" @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TcpClientForm +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + CheckForIllegalCrossThreadCalls = false; + } + + private TcpClient tcpClient; + private NetworkStream networkStream; + private void button1_Click(object sender, EventArgs e) + { + tcpClient = new TcpClient(); + tcpClient.Connect(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)); + Text = "连接成功"; + networkStream = tcpClient.GetStream(); + //读取 + Task.Run(() => { Read(); }); + } + + /// + /// 发送数据 + /// + /// + /// + private void button2_Click(object sender, EventArgs e) + { + byte[] buffer = Encoding.UTF8.GetBytes(textBox3.Text); + if (networkStream.CanWrite) + { + //BinaryWriter binaryWriter = new BinaryWriter(networkStream); + networkStream.Write(buffer, 0, buffer.Length); + } + } + + /// + /// 接收数据 + /// + void Read() + { + while (networkStream.CanRead) + { + try + { + byte[] buffer = new byte[1024 * 1024]; + var readLeng = networkStream.Read(buffer, 0, buffer.Length); + if (readLeng == 0) + { + txtMsg.AppendText("强行断开连接"); + networkStream.Close(); + return; + } + txtMsg.Text += $"{Encoding.UTF8.GetString(buffer)}\r\n"; + } + catch (Exception) { } + } + } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + networkStream.Close(); + tcpClient?.Close(); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.resx" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.resx" new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Form1.resx" @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Program.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Program.cs" new file mode 100644 index 0000000..fc53931 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Program.cs" @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TcpClientForm +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/AssemblyInfo.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/AssemblyInfo.cs" new file mode 100644 index 0000000..918faaf --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/AssemblyInfo.cs" @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("TcpClientForm")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TcpClientForm")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("a4783856-a740-4221-b254-97690423510b")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.Designer.cs" new file mode 100644 index 0000000..922a0b1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.Designer.cs" @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace TcpClientForm.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TcpClientForm.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.resx" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.resx" new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Resources.resx" @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.Designer.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.Designer.cs" new file mode 100644 index 0000000..bc51a97 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.Designer.cs" @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TcpClientForm.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.settings" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.settings" new file mode 100644 index 0000000..3964565 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Properties/Settings.settings" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Tcp\345\256\242\346\210\267\347\253\257.csproj" "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Tcp\345\256\242\346\210\267\347\253\257.csproj" new file mode 100644 index 0000000..685044c --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpClientForm/Tcp\345\256\242\346\210\267\347\253\257.csproj" @@ -0,0 +1,83 @@ + + + + + Debug + AnyCPU + {A4783856-A740-4221-B254-97690423510B} + WinExe + TcpClientForm + TcpClientForm + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Program.cs" "b/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Program.cs" new file mode 100644 index 0000000..292a670 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Program.cs" @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace TcpServerConsole +{ + class Program + { + static TcpListener tcpListener; + static List networkStreams = new List(); + static void Main(string[] args) + { + + tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9999); + tcpListener.Start(10); + Console.WriteLine("启动服务(IP:127.0.0.1 端口:9999),等待客户端连接!"); + Task.Run(() => { Accept(); }); + + while (true) + { + //群发 + var msg = Console.ReadLine(); + foreach (var item in networkStreams) + { + item.Write(Encoding.UTF8.GetBytes(msg)); + } + } + } + + /// + /// 等待客户端的连接 + /// + static void Accept() + { + while (true) + { + TcpClient tcpClient = tcpListener.AcceptTcpClient(); + NetworkStream networkStream = tcpClient.GetStream(); + Console.WriteLine($"{tcpClient.Client.RemoteEndPoint}上线"); + networkStreams.Add(networkStream); + Task.Run(() => { Read(networkStream, tcpClient); }); + } + } + + /// + /// 接收消息 + /// + /// + static void Read(NetworkStream networkStream, TcpClient tcpClient) + { + while (true) + { + try + { + byte[] buffer = new byte[1024 * 1024]; + //BinaryReader binaryReader = new BinaryReader(networkStream); + var readLen = networkStream.Read(buffer, 0, buffer.Length); + if (readLen == 0) + { + Console.WriteLine($"{tcpClient.Client.RemoteEndPoint}下线"); + networkStreams.Remove(networkStream); + networkStream.Close(); + tcpClient.Close(); + return; + } + Console.WriteLine(tcpClient.Client.RemoteEndPoint + ":" + Encoding.UTF8.GetString(buffer, 0, readLen)); + } + catch (Exception) { } + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Tcp\346\234\215\345\212\241\347\253\257.csproj" "b/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Tcp\346\234\215\345\212\241\347\253\257.csproj" new file mode 100644 index 0000000..958d2f1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/1Socket/TcpServerConsole/Tcp\346\234\215\345\212\241\347\253\257.csproj" @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git "a/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index.html" "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index.html" new file mode 100644 index 0000000..c16293d --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index.html" @@ -0,0 +1,15 @@ + + + + + + 模拟服务器响应浏览器 + + +
你好,农码一生
+
你也可以写个web服务器,你看我不就是个Web服务器吗。
+ + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index2.html" "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index2.html" new file mode 100644 index 0000000..ecf22e6 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Index2.html" @@ -0,0 +1,15 @@ + + + + + + 模拟服务器响应浏览器2 + + +
你好,农码一生
+
我是第二个页面了
+ + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Program.cs" "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Program.cs" new file mode 100644 index 0000000..adfcaab --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/Program.cs" @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace HTTPServer +{ + class Program + { + static void Main(string[] args) + { + //1 创建Socket对象 + Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //2 绑定ip和端口 + IPAddress ip = IPAddress.Parse("127.0.0.1"); + IPEndPoint ipEndPoint = new IPEndPoint(ip, 80); + socketServer.Bind(ipEndPoint); + + //3、开启侦听(等待客户机发出的连接),并设置最大客户端连接数为10 + socketServer.Listen(10); + + Console.WriteLine("服务已启动..."); + Console.WriteLine(); + Task.Run(() => { Accept(socketServer); }); + + Console.ReadKey(); + } + + //4 阻塞等待客户端连接 + private static void Accept(Socket socketServer) + { + while (true) + { + //阻塞等待客户端连接 + Socket newSocket = socketServer.Accept(); + Task.Run(() => { Receive(newSocket); }); + } + } + + //5 读取客户端发送过来的报文 + private static void Receive(Socket newSocket) + { + byte[] data = new byte[1024 * 1024]; + while (newSocket.Connected) + { + //读取客户端发送过来的数据 + int readLeng = newSocket.Receive(data, 0, data.Length, SocketFlags.None); + if (readLeng == 0)//客户端断开连接 + { + //停止会话(禁用Socket上的发送和接收,该方法允许Socket对象一直等待,直到将内部缓冲区的数据发送完为止) + newSocket.Shutdown(SocketShutdown.Both); + //关闭连接 + newSocket.Close(); + return; + } + + //读取客户端发来的请求报文 + var requst = Encoding.UTF8.GetString(data, 0, readLeng); + Console.WriteLine("收到请求报文:"); + Console.WriteLine(requst); + + //解析请求报文的请求路径(可以解析请求路径、请求文件、文件类型) + var requstFile = requst.Split("\r\n")[0].Split(" ")[1]; + + //回复客户端响应报文 + Send(newSocket, requstFile); + } + } + + //6 回复客户端响应报文 + private static void Send(Socket newSocket, string requstFile) + { + //这里如果请求的根目录,默认显示Index.html + if (requstFile == "/" ) requstFile = "/Index.html"; + + var msg = File.ReadAllText(Directory.GetCurrentDirectory() + requstFile); + //把消息内容转成字节数组后发送 + newSocket.Send(Encoding.UTF8.GetBytes(msg)); + Console.WriteLine("回复响应报文:"); + Console.WriteLine(msg); + Console.WriteLine(); + + //回复响应后马上关闭连接 + newSocket.Shutdown(SocketShutdown.Both); + newSocket.Close(); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/favicon.ico" "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/favicon.ico" new file mode 100644 index 0000000..54aa837 Binary files /dev/null and "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/favicon.ico" differ diff --git "a/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/\346\250\241\346\213\237Web\346\234\215\345\212\241\345\231\250.csproj" "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/\346\250\241\346\213\237Web\346\234\215\345\212\241\345\231\250.csproj" new file mode 100644 index 0000000..54a0bef --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/HTTPServer/\346\250\241\346\213\237Web\346\234\215\345\212\241\345\231\250.csproj" @@ -0,0 +1,20 @@ + + + + Exe + netcoreapp3.0 + + + + + Always + + + Always + + + Always + + + + diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/App.config" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/App.config" new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/App.config" @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.Designer.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.Designer.cs" new file mode 100644 index 0000000..2e6ddfc --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.Designer.cs" @@ -0,0 +1,97 @@ +namespace SimulateHttpGet +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.button1 = new System.Windows.Forms.Button(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(635, 34); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 0; + this.button1.Text = "请求"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(191, 36); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(412, 21); + this.textBox1.TabIndex = 1; + this.textBox1.Text = "http://fanyi-pro.baidu.com"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 41); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(173, 12); + this.label1.TabIndex = 2; + this.label1.Text = "Url地址(http地址非https):"; + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(14, 81); + this.textBox2.Multiline = true; + this.textBox2.Name = "textBox2"; + this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.textBox2.Size = new System.Drawing.Size(696, 344); + this.textBox2.TabIndex = 3; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(736, 450); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBox1); + this.Controls.Add(this.button1); + this.Name = "Form1"; + this.Text = "Form1"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button button1; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox textBox2; + } +} + diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.cs" new file mode 100644 index 0000000..bd3c4b7 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.cs" @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SimulateHttpGet +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + CheckForIllegalCrossThreadCalls = false; + } + + private void button1_Click(object sender, EventArgs e) + { + //得到主机信息 + IPHostEntry ipInfo = Dns.GetHostEntry(new Uri(textBox1.Text).Host); + //取得IPAddress[] + IPAddress[] ipAddr = ipInfo.AddressList; + //得到ip + IPAddress ip = ipAddr[0]; + //组合出远程终结点 + IPEndPoint ipEndPoint = new IPEndPoint(ip, 80); + //创建Socket 实例 + Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //尝试连接 + socketClient.Connect(ipEndPoint); + //发送请求 + Send(socketClient); + + Task.Run(() => + { + //接收服务器的响应 + Receive(socketClient); + }); + } + + /// + /// 接收来自服务端的消息 + /// + /// + void Receive(Socket socketClient) + { + byte[] data = new byte[1024 * 1024]; + while (true) + { + //读取客户端发送过来的数据 + int readLeng = socketClient.Receive(data, 0, data.Length, SocketFlags.None); + if (readLeng == 0)//客户端断开连接 + { + textBox2.Text += $"{socketClient.RemoteEndPoint}强行断开连接\r\n"; + return; + } + textBox2.AppendText($"{socketClient.RemoteEndPoint}:{Encoding.UTF8.GetString(data, 0, readLeng)}\r\n"); + } + } + + /// + /// 发送消息到服务端 + /// + /// + /// + void Send(Socket socketClient) + { + var msg = $"GET / HTTP/1.1\r\nHost: {new Uri(textBox1.Text).Host}\r\n\r\n"; + socketClient.Send(Encoding.UTF8.GetBytes(msg)); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.resx" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.resx" new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Form1.resx" @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Program.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Program.cs" new file mode 100644 index 0000000..4868d21 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Program.cs" @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SimulateHttpGet +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/AssemblyInfo.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/AssemblyInfo.cs" new file mode 100644 index 0000000..de14758 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/AssemblyInfo.cs" @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SimulateHttpGet")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SimulateHttpGet")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("b183fd73-5315-46d0-b737-4be566079a6f")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.Designer.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.Designer.cs" new file mode 100644 index 0000000..ed7f5c8 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.Designer.cs" @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace SimulateHttpGet.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SimulateHttpGet.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.resx" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.resx" new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Resources.resx" @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.Designer.cs" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.Designer.cs" new file mode 100644 index 0000000..ee2bf77 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.Designer.cs" @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SimulateHttpGet.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.settings" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.settings" new file mode 100644 index 0000000..3964565 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/Properties/Settings.settings" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/\346\250\241\346\213\237\346\265\217\350\247\210\345\231\250\347\232\204get\350\257\267\346\261\202.csproj" "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/\346\250\241\346\213\237\346\265\217\350\247\210\345\231\250\347\232\204get\350\257\267\346\261\202.csproj" new file mode 100644 index 0000000..f443339 --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SimulateHttpGet/\346\250\241\346\213\237\346\265\217\350\247\210\345\231\250\347\232\204get\350\257\267\346\261\202.csproj" @@ -0,0 +1,83 @@ + + + + + Debug + AnyCPU + {B183FD73-5315-46D0-B737-4BE566079A6F} + WinExe + SimulateHttpGet + SimulateHttpGet + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git "a/Socket\347\274\226\347\250\213/2HTTP/SocketChat.sln" "b/Socket\347\274\226\347\250\213/2HTTP/SocketChat.sln" new file mode 100644 index 0000000..216ee1a --- /dev/null +++ "b/Socket\347\274\226\347\250\213/2HTTP/SocketChat.sln" @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29324.140 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "模拟浏览器的get请求", "SimulateHttpGet\模拟浏览器的get请求.csproj", "{B183FD73-5315-46D0-B737-4BE566079A6F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "模拟Web服务器", "HTTPServer\模拟Web服务器.csproj", "{43EDAB42-A779-4AFF-85D2-C6E12E0BA3FA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B183FD73-5315-46D0-B737-4BE566079A6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B183FD73-5315-46D0-B737-4BE566079A6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B183FD73-5315-46D0-B737-4BE566079A6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B183FD73-5315-46D0-B737-4BE566079A6F}.Release|Any CPU.Build.0 = Release|Any CPU + {43EDAB42-A779-4AFF-85D2-C6E12E0BA3FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43EDAB42-A779-4AFF-85D2-C6E12E0BA3FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43EDAB42-A779-4AFF-85D2-C6E12E0BA3FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43EDAB42-A779-4AFF-85D2-C6E12E0BA3FA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5C201C99-8DB1-46F2-994F-AFCD7098B518} + EndGlobalSection +EndGlobal