diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b33fd98 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,42 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (Server)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/MultiThreadTCPServer/bin/Debug/net6.0/MultiThreadTCPServer.dll", + "args": [], + "cwd": "${workspaceFolder}/MultiThreadTCPServer", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (Client)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-client", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/MultiThreadTCPClient/bin/Debug/net6.0/MultiThreadTCPServer.dll", + "args": [], + "cwd": "${workspaceFolder}/MultiThreadTCPClient", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..7ff81b3 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,53 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/MultiThreadTCPServer/MultiThreadTCPServer.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/MultiThreadTCPServer/MultiThreadTCPServer.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/MultiThreadTCPServer/MultiThreadTCPServer.csproj" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-client", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/MultiThreadTCPClient/MultiThreadTCPClient.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + ] +} \ No newline at end of file diff --git a/MachineServer/MachineServer.csproj b/MachineServer/MachineServer.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/MachineServer/MachineServer.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/MachineServer/Program.cs b/MachineServer/Program.cs new file mode 100644 index 0000000..1d6f336 --- /dev/null +++ b/MachineServer/Program.cs @@ -0,0 +1,15 @@ +using MachineServer; + +class Program +{ + static void Main(string[] args) + { + Thread t = new Thread(() => + { + Server myserver = new Server("127.0.0.1", 7468); + }); + + t.Start(); + Console.WriteLine("Server Started...!"); + } +} \ No newline at end of file diff --git a/MachineServer/Server.cs b/MachineServer/Server.cs new file mode 100644 index 0000000..bc4127e --- /dev/null +++ b/MachineServer/Server.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MachineServer +{ + internal class Server + { + TcpListener _server = null; + + public Server(string ip, int port) + { + IPAddress localAddr = IPAddress.Parse(ip); + _server = new TcpListener(localAddr, port); + _server.Start(); + StartListener(); + } + + private void StartListener() + { + try + { + while (true) + { + Console.WriteLine("Waiting for a connection..."); + TcpClient client = _server.AcceptTcpClient(); + Console.WriteLine("Connected!"); + + Thread t = new Thread(new ParameterizedThreadStart(HandleClient)); + t.Start(client); + } + } + catch (SocketException e) + { + Console.WriteLine("SocketException: {0}", e); + _server.Stop(); + } + } + + public void HandleClient(object obj) + { + TcpClient client = (TcpClient)obj; + var stream = client.GetStream(); + + string imei = String.Empty; + + string data = String.Empty; + + Byte[] bytes = new Byte[256]; + int i; + + try + { + while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) + { + string hex = BitConverter.ToString(bytes); + // + int msgType = BitConverter.ToInt32(bytes, 0); + int msgLength = BitConverter.ToInt32(bytes, sizeof(int)); + string msg = Encoding.ASCII.GetString(bytes, 2*sizeof(int), msgLength); + data = Encoding.ASCII.GetString(bytes, 0, i); + Console.WriteLine($"{data}: Received: {Thread.CurrentThread.ManagedThreadId}"); + + string str = "Hey Client!"; + Byte[] reply = System.Text.Encoding.ASCII.GetBytes(str); + stream.Write(reply, 0, reply.Length); + Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Sent: {str}"); + } + + } + catch (Exception e) + { + Console.WriteLine("Exception: {0}", e.ToString()); + client.Close(); + } + } + } +} diff --git a/MultiThreadTCP.sln b/MultiThreadTCP.sln index fef2c53..a7c981b 100644 --- a/MultiThreadTCP.sln +++ b/MultiThreadTCP.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33103.201 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiThreadTCPServer", "MultiThreadTCPServer\MultiThreadTCPServer.csproj", "{7C366206-F583-4E04-9715-AA36E8FFB3A8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiThreadTCPServer", "MultiThreadTCPServer\MultiThreadTCPServer.csproj", "{7C366206-F583-4E04-9715-AA36E8FFB3A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiThreadTCPClient", "MultiThreadTCPClient\MultiThreadTCPClient.csproj", "{E7C9CEE3-6088-485D-825D-4463E5CE5C70}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiThreadTCPClient", "MultiThreadTCPClient\MultiThreadTCPClient.csproj", "{E7C9CEE3-6088-485D-825D-4463E5CE5C70}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MachineServer", "MachineServer\MachineServer.csproj", "{608C7096-A4A6-4465-A356-73886C92CFFD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {E7C9CEE3-6088-485D-825D-4463E5CE5C70}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7C9CEE3-6088-485D-825D-4463E5CE5C70}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7C9CEE3-6088-485D-825D-4463E5CE5C70}.Release|Any CPU.Build.0 = Release|Any CPU + {608C7096-A4A6-4465-A356-73886C92CFFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {608C7096-A4A6-4465-A356-73886C92CFFD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {608C7096-A4A6-4465-A356-73886C92CFFD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {608C7096-A4A6-4465-A356-73886C92CFFD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE