add client code

main
Yik Teng Hie 3 years ago
parent 457c742274
commit 6cd45fa595

@ -5,6 +5,8 @@ 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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiThreadTCPClient", "MultiThreadTCPClient\MultiThreadTCPClient.csproj", "{E7C9CEE3-6088-485D-825D-4463E5CE5C70}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{7C366206-F583-4E04-9715-AA36E8FFB3A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C366206-F583-4E04-9715-AA36E8FFB3A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C366206-F583-4E04-9715-AA36E8FFB3A8}.Release|Any CPU.Build.0 = Release|Any CPU
{E7C9CEE3-6088-485D-825D-4463E5CE5C70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,59 @@
using System.Net.Sockets;
class Program
{
static void Main(string[] args)
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
Connect("127.0.0.1", "Hello I'm Client 1...");
}).Start();
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
Connect("127.0.0.1", "Hello I'm Client 2...");
}).Start();
Console.ReadLine();
}
private static void Connect(string server, string message)
{
try
{
Int32 port = 3000;
TcpClient client = new TcpClient(server, port);
NetworkStream stream = client.GetStream();
int count = 0;
while (count++ < 3)
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
Console.WriteLine($"Sent: {message}");
data = new Byte[256];
string response = string.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
response = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine($"Received: {response}");
Thread.Sleep(2000);
}
stream.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e}");
}
Console.Read();
}
}

@ -1,2 +1,15 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using MultiThreadTCPServer;
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(() =>
{
Server myserver = new Server("127.0.0.1", 3000);
});
t.Start();
Console.WriteLine("Server Started...!");
}
}
Loading…
Cancel
Save