forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (78 loc) · 2.69 KB
/
Program.cs
File metadata and controls
91 lines (78 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using NSmartProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TCPTester.Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** Tcp Sever ***");
//int port = int.Parse(args[0]);
int port = 12346;
Console.WriteLine("start listen " + port.ToString());
TcpListener tcpListener = TcpListener.Create(port);
tcpListener.Start();
TcpClient client = null;
CancellationTokenSource cts = null;
// var thread = new Thread(() =>
// {
int count = 5;
//while (count > 0)
//{
client = tcpListener.AcceptTcpClient();
//client.SetKeepAlive(out _);
//Task.Run(() =>
//{
var stream = client.GetStream();
while (count > 0)
{
byte[] bytes = new byte[4096];
try
{
cts = new CancellationTokenSource();
Task<int> result = stream.ReadAsync(bytes, 0, bytes.Length, cts.Token);
result.Wait(cts.Token);
var length = result.Result;
Console.WriteLine("continue..." + length);
Console.WriteLine(ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim());
string retMessage = "received++" + ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim() + "+++";
byte[] retMessageBytes = ASCIIEncoding.ASCII.GetBytes(retMessage);
stream.Write(retMessageBytes, 0, retMessageBytes.Length);
if (length == 0) { stream.Close(); break; }
}
catch
{
break;
}
count--;
}
//});
//}
Environment.Exit(0);
//});
//thread.Start();
//while (true)
//{
// var line = Console.ReadLine();
// if (line == "stop")
// {
// //client.Close();
// //cts.Cancel(true);
// TcpClient tc = new TcpClient();
// tc.Connect("127.0.0.1", 12306);
// tc.Client.Send(new byte[] { 0 });
// }
//}
}
public static void test111()
{
}
}
}