Socket\SocketClient\AsynSocketClient.cs
| |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading; |
| using System.Threading.Tasks; |
| |
| namespace SocketClient |
| { |
| public class StateObject |
| { |
| public Socket workSocket = null; |
| public const int BufferSize = 256; |
| public byte[] buffer = new byte[BufferSize]; |
| public StringBuilder sb = new StringBuilder(); |
| } |
| |
| class AsynSocketClient |
| { |
| private const int port = 11000; |
| |
| private static ManualResetEvent connetDone = new ManualResetEvent(false); |
| |
| private static ManualResetEvent sendDone = new ManualResetEvent(false); |
| |
| private static ManualResetEvent receiveDone = new ManualResetEvent(false); |
| |
| private static String response = String.Empty; |
| |
| public static void StartClient() |
| { |
| try |
| { |
| IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
| IPAddress ipAddress = ipHostInfo.AddressList[0]; |
| IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); |
| |
| Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); |
| client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); |
| connetDone.WaitOne(); |
| |
| Send(client, "This is a test<EOF>"); |
| sendDone.WaitOne(); |
| |
| Receive(client); |
| receiveDone.WaitOne(); |
| |
| Console.WriteLine("Response received : {0}", response); |
| |
| client.Shutdown(SocketShutdown.Both); |
| client.Close(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| finally |
| { |
| } |
| |
| } |
| private static void ConnectCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| Socket client = (Socket)asyncResult.AsyncState; |
| |
| client.EndConnect(asyncResult); |
| Console.WriteLine("socket connect successfull"); |
| connetDone.Set(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| |
| private static void Send(Socket client, String data) |
| { |
| byte[] byteData = Encoding.ASCII.GetBytes(data); |
| client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); |
| } |
| private static void SendCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| Socket client = (Socket)asyncResult.AsyncState; |
| int byteSent = client.EndSend(asyncResult); |
| Console.WriteLine("send data"); |
| sendDone.Set(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| |
| private static void Receive(Socket client) |
| { |
| try |
| { |
| StateObject state = new StateObject(); |
| state.workSocket = client; |
| client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| private static void ReceiveCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| StateObject state = (StateObject)asyncResult.AsyncState; |
| Socket client = state.workSocket; |
| int bytesRead = client.EndReceive(asyncResult); |
| if (bytesRead > 0) |
| { |
| |
| state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); |
| |
| |
| client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReceiveCallback), state); |
| Console.WriteLine("receive data"); |
| } |
| else |
| { |
| |
| if (state.sb.Length > 1) |
| { |
| response = state.sb.ToString(); |
| } |
| |
| receiveDone.Set(); |
| } |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| } |
| |
| } |
Socket\SocketClient\Program.cs
| |
| using System; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading.Tasks; |
| |
| namespace SocketClient |
| { |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| AsynSocketClient.StartClient(); |
| |
| Console.Read(); |
| } |
| } |
| } |
| |
| |
| |
Socket\SocketClient\SocketClient.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>Exe</OutputType> |
| <TargetFramework>net6.0</TargetFramework> |
| </PropertyGroup> |
| |
| </Project> |
| |
Socket\SocketServer\AsynSocketServer.cs
| |
| |
| |
| |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading; |
| using System.Threading.Tasks; |
| |
| namespace SocketServer |
| { |
| public class StateObject |
| { |
| public const int BufferSize = 1024; |
| public byte[] buffer = new byte[BufferSize]; |
| |
| public StringBuilder sb = new StringBuilder(); |
| |
| public Socket workSocket = null; |
| } |
| class AsynSocketServer |
| { |
| |
| public static ManualResetEvent allDone = new ManualResetEvent(false); |
| |
| public AsynSocketServer() |
| { |
| |
| } |
| public static void StartListening() |
| { |
| |
| |
| |
| IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
| IPAddress ipAddress = ipHostInfo.AddressList[0]; |
| IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); |
| |
| |
| Socket listener = new Socket(ipAddress.AddressFamily, |
| SocketType.Stream, ProtocolType.Tcp); |
| |
| |
| try |
| { |
| listener.Bind(localEndPoint); |
| listener.Listen(100); |
| |
| while (true) |
| { |
| |
| allDone.Reset(); |
| |
| |
| Console.WriteLine("Waiting for a connection..."); |
| listener.BeginAccept( |
| new AsyncCallback(AcceptCallback), |
| listener); |
| |
| allDone.WaitOne(); |
| } |
| |
| } |
| catch (Exception e) |
| { |
| Console.WriteLine(e.ToString()); |
| } |
| |
| Console.WriteLine("\nPress ENTER to continue..."); |
| Console.Read(); |
| } |
| public static void AcceptCallback(IAsyncResult asyncResult) |
| { |
| allDone.Set(); |
| Socket listener = (Socket)asyncResult.AsyncState; |
| Socket handler = listener.EndAccept(asyncResult); |
| |
| StateObject state = new StateObject(); |
| state.workSocket = handler; |
| handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReadCallback), state); |
| } |
| public static void ReadCallback(IAsyncResult asyncResult) |
| { |
| String content = String.Empty; |
| |
| |
| StateObject state = (StateObject)asyncResult.AsyncState; |
| Socket handler = state.workSocket; |
| |
| |
| int bytesRead = handler.EndReceive(asyncResult); |
| |
| if (bytesRead > 0) |
| { |
| |
| state.sb.Append(Encoding.ASCII.GetString( |
| state.buffer, 0, bytesRead)); |
| |
| |
| |
| content = state.sb.ToString(); |
| if (content.IndexOf("<EOF>") > -1) |
| { |
| |
| |
| Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", |
| content.Length, content); |
| |
| Send(handler, content); |
| } |
| else |
| { |
| |
| handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReadCallback), state); |
| } |
| } |
| } |
| private static void Send(Socket handler, String data) |
| { |
| |
| byte[] byteData = Encoding.ASCII.GetBytes(data); |
| |
| |
| handler.BeginSend(byteData, 0, byteData.Length, 0, |
| new AsyncCallback(SendCallback), handler); |
| } |
| |
| private static void SendCallback(IAsyncResult ar) |
| { |
| try |
| { |
| |
| Socket handler = (Socket)ar.AsyncState; |
| |
| |
| int bytesSent = handler.EndSend(ar); |
| Console.WriteLine("Sent {0} bytes to client.", bytesSent); |
| |
| handler.Shutdown(SocketShutdown.Both); |
| handler.Close(); |
| |
| } |
| catch (Exception e) |
| { |
| Console.WriteLine(e.ToString()); |
| } |
| } |
| |
| } |
| } |
Socket\SocketServer\Program.cs
| |
| using System; |
| |
| namespace SocketServer |
| { |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| AsynSocketServer.StartListening(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
Socket\SocketServer\SocketServer.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>Exe</OutputType> |
| <TargetFramework>net6.0</TargetFramework> |
| </PropertyGroup> |
| |
| </Project> |
| |
Socket\Socket.sln
| |
| |
| Microsoft Visual Studio Solution File, Format Version 12.00 |
| # Visual Studio Version 17 |
| VisualStudioVersion = 17.5.002.0 |
| MinimumVisualStudioVersion = 10.0.40219.1 |
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketClient", "SocketClient\SocketClient.csproj", "{1D80C1A7-2F06-4201-BA8C-36261BAA3068}" |
| EndProject |
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketServer", "SocketServer\SocketServer.csproj", "{9F738BE4-9119-4357-8048-49521B79407B}" |
| EndProject |
| Global |
| GlobalSection(SolutionConfigurationPlatforms) = preSolution |
| Debug|Any CPU = Debug|Any CPU |
| Release|Any CPU = Release|Any CPU |
| EndGlobalSection |
| GlobalSection(ProjectConfigurationPlatforms) = postSolution |
| {1D80C1A7-2F06-4201-BA8C-36261BAA3068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
| {1D80C1A7-2F06-4201-BA8C-36261BAA3068}.Debug|Any CPU.Build.0 = Debug|Any CPU |
| {1D80C1A7-2F06-4201-BA8C-36261BAA3068}.Release|Any CPU.ActiveCfg = Release|Any CPU |
| {1D80C1A7-2F06-4201-BA8C-36261BAA3068}.Release|Any CPU.Build.0 = Release|Any CPU |
| {9F738BE4-9119-4357-8048-49521B79407B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
| {9F738BE4-9119-4357-8048-49521B79407B}.Debug|Any CPU.Build.0 = Debug|Any CPU |
| {9F738BE4-9119-4357-8048-49521B79407B}.Release|Any CPU.ActiveCfg = Release|Any CPU |
| {9F738BE4-9119-4357-8048-49521B79407B}.Release|Any CPU.Build.0 = Release|Any CPU |
| EndGlobalSection |
| GlobalSection(SolutionProperties) = preSolution |
| HideSolutionNode = FALSE |
| EndGlobalSection |
| GlobalSection(ExtensibilityGlobals) = postSolution |
| SolutionGuid = {90CD277E-5715-49CD-8516-BFE42B92EE87} |
| EndGlobalSection |
| EndGlobal |
| |
Socket\zz.bat
| |
| set "scriptPath=%cd%" |
| D: && cd D:\dotnet-script-new\App\bin\Debug\net8.0\ && D:\dotnet-script-new\App\bin\Debug\net8.0\App.exe "%scriptPath%" "Question" "other" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Socket\SocketClient\Properties\launchSettings.json
| |
| { |
| "profiles": { |
| "SocketClient": { |
| "commandName": "Project", |
| "launchBrowser": true, |
| "environmentVariables": { |
| "ASPNETCORE_ENVIRONMENT": "Development" |
| }, |
| "applicationUrl": "https://localhost:59518;http://localhost:59519" |
| } |
| } |
| } |
Socket\SocketServer\Properties\launchSettings.json
| |
| { |
| "profiles": { |
| "SocketServer": { |
| "commandName": "Project", |
| "launchBrowser": true, |
| "environmentVariables": { |
| "ASPNETCORE_ENVIRONMENT": "Development" |
| }, |
| "applicationUrl": "https://localhost:59526;http://localhost:59527" |
| } |
| } |
| } |
Socket\SocketClient\AsynSocketClient.cs
| |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading; |
| using System.Threading.Tasks; |
| |
| namespace SocketClient |
| { |
| public class StateObject |
| { |
| public Socket workSocket = null; |
| public const int BufferSize = 256; |
| public byte[] buffer = new byte[BufferSize]; |
| public StringBuilder sb = new StringBuilder(); |
| } |
| |
| class AsynSocketClient |
| { |
| private const int port = 11000; |
| |
| private static ManualResetEvent connetDone = new ManualResetEvent(false); |
| |
| private static ManualResetEvent sendDone = new ManualResetEvent(false); |
| |
| private static ManualResetEvent receiveDone = new ManualResetEvent(false); |
| |
| private static String response = String.Empty; |
| |
| public static void StartClient() |
| { |
| try |
| { |
| IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
| IPAddress ipAddress = ipHostInfo.AddressList[0]; |
| IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); |
| |
| Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); |
| client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); |
| connetDone.WaitOne(); |
| |
| Send(client, "This is a test<EOF>"); |
| sendDone.WaitOne(); |
| |
| Receive(client); |
| receiveDone.WaitOne(); |
| |
| Console.WriteLine("Response received : {0}", response); |
| |
| client.Shutdown(SocketShutdown.Both); |
| client.Close(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| finally |
| { |
| } |
| |
| } |
| private static void ConnectCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| Socket client = (Socket)asyncResult.AsyncState; |
| |
| client.EndConnect(asyncResult); |
| Console.WriteLine("socket connect successfull"); |
| connetDone.Set(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| |
| private static void Send(Socket client, String data) |
| { |
| byte[] byteData = Encoding.ASCII.GetBytes(data); |
| client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); |
| } |
| private static void SendCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| Socket client = (Socket)asyncResult.AsyncState; |
| int byteSent = client.EndSend(asyncResult); |
| Console.WriteLine("send data"); |
| sendDone.Set(); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| |
| private static void Receive(Socket client) |
| { |
| try |
| { |
| StateObject state = new StateObject(); |
| state.workSocket = client; |
| client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| private static void ReceiveCallback(IAsyncResult asyncResult) |
| { |
| try |
| { |
| StateObject state = (StateObject)asyncResult.AsyncState; |
| Socket client = state.workSocket; |
| int bytesRead = client.EndReceive(asyncResult); |
| if (bytesRead > 0) |
| { |
| |
| state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); |
| |
| |
| client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReceiveCallback), state); |
| Console.WriteLine("receive data"); |
| } |
| else |
| { |
| |
| if (state.sb.Length > 1) |
| { |
| response = state.sb.ToString(); |
| } |
| |
| receiveDone.Set(); |
| } |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.ToString()); |
| } |
| } |
| } |
| |
| } |
Socket\SocketClient\Program.cs
| |
| using System; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading.Tasks; |
| |
| namespace SocketClient |
| { |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| AsynSocketClient.StartClient(); |
| |
| Console.Read(); |
| } |
| } |
| } |
| |
| |
| |
Socket\SocketClient\SocketClient.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>Exe</OutputType> |
| <TargetFramework>net6.0</TargetFramework> |
| </PropertyGroup> |
| |
| </Project> |
| |
Socket\SocketClient\Properties\launchSettings.json
| |
| { |
| "profiles": { |
| "SocketClient": { |
| "commandName": "Project", |
| "launchBrowser": true, |
| "environmentVariables": { |
| "ASPNETCORE_ENVIRONMENT": "Development" |
| }, |
| "applicationUrl": "https://localhost:59518;http://localhost:59519" |
| } |
| } |
| } |
Socket\SocketServer\AsynSocketServer.cs
| |
| |
| |
| |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Net; |
| using System.Net.Sockets; |
| using System.Text; |
| using System.Threading; |
| using System.Threading.Tasks; |
| |
| namespace SocketServer |
| { |
| public class StateObject |
| { |
| public const int BufferSize = 1024; |
| public byte[] buffer = new byte[BufferSize]; |
| |
| public StringBuilder sb = new StringBuilder(); |
| |
| public Socket workSocket = null; |
| } |
| class AsynSocketServer |
| { |
| |
| public static ManualResetEvent allDone = new ManualResetEvent(false); |
| |
| public AsynSocketServer() |
| { |
| |
| } |
| public static void StartListening() |
| { |
| |
| |
| |
| IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
| IPAddress ipAddress = ipHostInfo.AddressList[0]; |
| IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); |
| |
| |
| Socket listener = new Socket(ipAddress.AddressFamily, |
| SocketType.Stream, ProtocolType.Tcp); |
| |
| |
| try |
| { |
| listener.Bind(localEndPoint); |
| listener.Listen(100); |
| |
| while (true) |
| { |
| |
| allDone.Reset(); |
| |
| |
| Console.WriteLine("Waiting for a connection..."); |
| listener.BeginAccept( |
| new AsyncCallback(AcceptCallback), |
| listener); |
| |
| allDone.WaitOne(); |
| } |
| |
| } |
| catch (Exception e) |
| { |
| Console.WriteLine(e.ToString()); |
| } |
| |
| Console.WriteLine("\nPress ENTER to continue..."); |
| Console.Read(); |
| } |
| public static void AcceptCallback(IAsyncResult asyncResult) |
| { |
| allDone.Set(); |
| Socket listener = (Socket)asyncResult.AsyncState; |
| Socket handler = listener.EndAccept(asyncResult); |
| |
| StateObject state = new StateObject(); |
| state.workSocket = handler; |
| handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReadCallback), state); |
| } |
| public static void ReadCallback(IAsyncResult asyncResult) |
| { |
| String content = String.Empty; |
| |
| |
| StateObject state = (StateObject)asyncResult.AsyncState; |
| Socket handler = state.workSocket; |
| |
| |
| int bytesRead = handler.EndReceive(asyncResult); |
| |
| if (bytesRead > 0) |
| { |
| |
| state.sb.Append(Encoding.ASCII.GetString( |
| state.buffer, 0, bytesRead)); |
| |
| |
| |
| content = state.sb.ToString(); |
| if (content.IndexOf("<EOF>") > -1) |
| { |
| |
| |
| Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", |
| content.Length, content); |
| |
| Send(handler, content); |
| } |
| else |
| { |
| |
| handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, |
| new AsyncCallback(ReadCallback), state); |
| } |
| } |
| } |
| private static void Send(Socket handler, String data) |
| { |
| |
| byte[] byteData = Encoding.ASCII.GetBytes(data); |
| |
| |
| handler.BeginSend(byteData, 0, byteData.Length, 0, |
| new AsyncCallback(SendCallback), handler); |
| } |
| |
| private static void SendCallback(IAsyncResult ar) |
| { |
| try |
| { |
| |
| Socket handler = (Socket)ar.AsyncState; |
| |
| |
| int bytesSent = handler.EndSend(ar); |
| Console.WriteLine("Sent {0} bytes to client.", bytesSent); |
| |
| handler.Shutdown(SocketShutdown.Both); |
| handler.Close(); |
| |
| } |
| catch (Exception e) |
| { |
| Console.WriteLine(e.ToString()); |
| } |
| } |
| |
| } |
| } |
Socket\SocketServer\Program.cs
| |
| using System; |
| |
| namespace SocketServer |
| { |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| AsynSocketServer.StartListening(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
Socket\SocketServer\SocketServer.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>Exe</OutputType> |
| <TargetFramework>net6.0</TargetFramework> |
| </PropertyGroup> |
| |
| </Project> |
| |
Socket\SocketServer\Properties\launchSettings.json
| |
| { |
| "profiles": { |
| "SocketServer": { |
| "commandName": "Project", |
| "launchBrowser": true, |
| "environmentVariables": { |
| "ASPNETCORE_ENVIRONMENT": "Development" |
| }, |
| "applicationUrl": "https://localhost:59526;http://localhost:59527" |
| } |
| } |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2022-10-15 autoit_passwordbook
2022-10-15 golang_class