Skip to content

Commit d7b0ae8

Browse files
committed
Tcp server
1 parent 8bfe512 commit d7b0ae8

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

c#/base/TCPServer.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Sockets;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace TCPServer
12+
{
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9000);
18+
tcpListener.Start();
19+
while (true)
20+
{
21+
if (tcpListener.Pending())
22+
{
23+
TcpClient client = tcpListener.AcceptTcpClient();
24+
Task.Run(() =>
25+
{
26+
NetworkStream stream = client.GetStream();
27+
var remote = client.Client.RemoteEndPoint;
28+
while (true)
29+
{
30+
if (stream.DataAvailable)
31+
{
32+
byte[] data = new byte[1024];
33+
int len = stream.Read(data, 0, 1024);
34+
string Name = Encoding.UTF8.GetString(data,0,len);
35+
var senddata = Encoding.UTF8.GetBytes("Hello:" + Name);
36+
stream.Write(senddata, 0, senddata.Length);
37+
}
38+
39+
if (!client.IsOnline())
40+
{
41+
Console.WriteLine("Connect Closed.");
42+
break;
43+
}
44+
Thread.Sleep(1);
45+
}
46+
});
47+
}
48+
Thread.Sleep(1);
49+
}
50+
}
51+
}
52+
53+
public static class TcpClientEx
54+
{
55+
public static bool IsOnline(this TcpClient client)
56+
{
57+
return !((client.Client.Poll(15000, SelectMode.SelectRead) && (client.Client.Available == 0)) || !client.Client.Connected);
58+
}
59+
}
60+
}

c#/base/TcpClient.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net.Sockets;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace TCPClient
12+
{
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
ThreadPool.SetMinThreads(100, 100);
18+
ThreadPool.SetMaxThreads(200, 200);
19+
20+
Parallel.For(1, 10, x =>
21+
{
22+
SendData("Tom");
23+
});
24+
25+
Console.WriteLine("All Completed!");
26+
Console.ReadKey();
27+
}
28+
29+
private static void SendData(string Name)
30+
{
31+
Task.Run(() =>
32+
{
33+
Console.WriteLine("Start");
34+
TcpClient tcpClient = new TcpClient();
35+
tcpClient.Connect("127.0.0.1", 9000);
36+
Console.WriteLine("Connected");
37+
NetworkStream netStream = tcpClient.GetStream();
38+
39+
Task.Run(() =>
40+
{
41+
Thread.Sleep(100);
42+
while (true)
43+
{
44+
if (!tcpClient.Client.Connected)
45+
{
46+
break;
47+
}
48+
49+
if (netStream == null)
50+
{
51+
break;
52+
}
53+
54+
try
55+
{
56+
if (netStream.DataAvailable)
57+
{
58+
byte[] data = new byte[1024];
59+
int len = netStream.Read(data, 0, 1024);
60+
var message = Encoding.UTF8.GetString(data, 0, len);
61+
Console.WriteLine(message);
62+
}
63+
}
64+
catch
65+
{
66+
break;
67+
}
68+
69+
Thread.Sleep(10);
70+
}
71+
});
72+
73+
for (int i = 0; i < 100; i++)
74+
{
75+
byte[] datas = Encoding.UTF8.GetBytes(Name);
76+
int Len = datas.Length;
77+
netStream.Write(datas, 0, Len);
78+
Thread.Sleep(1000);
79+
}
80+
81+
netStream.Close();
82+
netStream = null;
83+
tcpClient.Close();
84+
85+
Console.WriteLine("Completed");
86+
});
87+
}
88+
}
89+
}

c#/base/build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@echo off
2-
set cmd_srcs=MyThread.cs
2+
set cmd_srcs=MyThread.cs TcpServer.cs TcpClient.cs
33
set app_srcs=MyApp.cs
44
set dll_srcs=MyDll.cs
55

0 commit comments

Comments
 (0)