-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (59 loc) · 1.96 KB
/
Program.cs
File metadata and controls
60 lines (59 loc) · 1.96 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
using IOCPClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTestClient
{
class Program
{
static void Main(string[] args)
{
Test().Wait();
Console.ReadKey();
}
static async Task Test()
{
IPAddress addr = IPAddress.Parse(ConfigurationManager.AppSettings["remoteAddr"]);
int port = int.Parse(ConfigurationManager.AppSettings["remotePort"]);
Client client = new Client(new IPEndPoint(addr, port));
while(true)
{
try
{
var result = await client.ConnectAsync(10000);
if (result.SocketError == SocketError.Success)
break;
Console.WriteLine("无法连接,错误:" + result.SocketError);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
await Task.Delay(1000);
}
}
while(true)
{
try
{
var result = await client.ReceiveAsync(client.ConnectSocket, 10000);
if(!result.ReceiveSuccess)
{
Console.WriteLine("没有接收到数据");
break;
}
Console.WriteLine("接收到的数据为:" + client.Encoding.GetString(result.Data));
await client.SendAsync(client.ConnectSocket, client.Encoding.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
}
catch(Exception ex)
{
Console.WriteLine("接收数据发生错误:" + ex.Message);
}
}
}
}
}