You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
336 lines
8.7 KiB
336 lines
8.7 KiB
using System; |
|
using System.IO; |
|
using System.Net.Sockets; |
|
using System.Net; |
|
using System.Text; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
|
|
/// <summary> |
|
/// Summary description for Class1 |
|
/// </summary> |
|
namespace TCPSRVCLI |
|
{ |
|
public class TCPSrvCli |
|
{ |
|
private String _address; |
|
private int _port; |
|
private int _mode = -1; // -1 error, 0 server, 1 client |
|
private bool _enabled = false; |
|
private bool _connected = false; |
|
private StreamWriter sw = null; |
|
private StreamReader sr = null; |
|
private TcpClient client; |
|
Thread recv_thread = null; |
|
private TcpListener listner = null; |
|
|
|
public delegate void Recv_Mess_s(String s); |
|
public delegate void Recv_Mess_b(char[] arr); |
|
public static Recv_Mess_s onRecvS; |
|
public static Recv_Mess_b onRecvB; |
|
|
|
public bool Connected { get => _connected; } |
|
public int Mode { get => _mode; } |
|
|
|
public TCPSrvCli(String address = "127.0.0.1", int port = 12000) |
|
{ |
|
_address = address; |
|
_port = port; |
|
|
|
if (InitSrv()) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("I a'm server"); |
|
#endif |
|
_connected = true; |
|
return; |
|
} |
|
if (InitCli()) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("I a'm client"); |
|
#endif |
|
_connected = true; |
|
return; |
|
} |
|
_connected = false; |
|
} |
|
public bool InitCli() |
|
{ |
|
|
|
#if DEBUG |
|
Console.WriteLine("Start Client"); |
|
#endif |
|
int timeout = 100; |
|
do |
|
{ |
|
try |
|
{ |
|
client = new TcpClient(); |
|
client.Connect(new IPEndPoint(IPAddress.Parse(_address), _port)); |
|
} |
|
catch (Exception ee) |
|
{ |
|
#if DEBUG |
|
Console.Write("."); |
|
#endif |
|
Thread.Sleep(1000); |
|
} |
|
} while (!client.Connected && timeout-- > 0); |
|
|
|
if (timeout < 2) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("\r\nError connect Client " + timeout.ToString()); |
|
#endif |
|
return false; |
|
} |
|
Console.WriteLine(); |
|
_enabled = true; |
|
sw = new StreamWriter(client.GetStream()); |
|
sr = new StreamReader(client.GetStream()); |
|
sw.AutoFlush = true; |
|
_connected = true; |
|
|
|
if (client.Connected) |
|
{ |
|
recv_thread = new Thread(new ThreadStart(Recv)); |
|
recv_thread.Start(); |
|
} |
|
_mode = 1; |
|
return true; |
|
} |
|
public bool InitSrv() |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("Start Server"); |
|
#endif |
|
try |
|
{ |
|
listner = new TcpListener(new IPEndPoint(IPAddress.Parse(_address), _port)); |
|
listner.Start(); |
|
} |
|
catch (Exception ee) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("Error start Server"); |
|
#endif |
|
_mode = -1; |
|
return false; |
|
} |
|
_mode = 0; |
|
_enabled = true; |
|
_connected = true; |
|
|
|
client = listner.AcceptTcpClient(); |
|
sr = new StreamReader(client.GetStream()); |
|
sw = new StreamWriter(client.GetStream()); |
|
sw.AutoFlush = true; |
|
|
|
recv_thread = new Thread(new ThreadStart(Recv)); |
|
recv_thread.Start(); |
|
|
|
return true; |
|
} |
|
public void Stop() |
|
{ |
|
try |
|
{ |
|
_enabled = false; |
|
if (_mode == 0) |
|
{ |
|
client.Close(); |
|
listner.Stop(); |
|
} |
|
if (_mode == 1) |
|
{ |
|
client.Close(); |
|
} |
|
sr.Close(); |
|
sw.Close(); |
|
_connected = false; |
|
} |
|
catch (Exception ee) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine(ee); |
|
#endif |
|
} |
|
} |
|
private void Recv() |
|
{ |
|
while (_enabled) |
|
{ |
|
if (client.Available > 0) |
|
{ |
|
char[] s = new char[client.Available]; |
|
sr.Read(s, 0, s.Length); |
|
#if DEBUG |
|
Console.WriteLine(s.Length); |
|
#endif |
|
if (s[s.Length - 2] == '\r' && s[s.Length - 1] == '\n') |
|
onRecvS(new String(s)); |
|
else |
|
onRecvB(s); |
|
} |
|
} |
|
client.Close(); |
|
} |
|
public void Send(String s) |
|
{ |
|
try |
|
{ |
|
if (client.Connected) |
|
{ |
|
sw.WriteLine(s); |
|
} |
|
} |
|
catch (Exception) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("Send string error"); |
|
#endif |
|
Stop(); |
|
if (_mode == 0) |
|
{ |
|
InitSrv(); |
|
return; |
|
} |
|
if (_mode == 1) |
|
{ |
|
InitCli(); |
|
return; |
|
} |
|
} |
|
} |
|
public void Send(Byte[] b) |
|
{ |
|
try |
|
{ |
|
if (client.Connected) |
|
{ |
|
sw.Write(Encoding.UTF8.GetChars(b)); |
|
} |
|
} |
|
catch (Exception) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine("Send array error"); |
|
#endif |
|
Stop(); |
|
if (_mode == 0) |
|
{ |
|
InitSrv(); |
|
return; |
|
} |
|
if (_mode == 1) |
|
{ |
|
InitCli(); |
|
return; |
|
} |
|
} |
|
} |
|
} |
|
|
|
public class UDPSrvCli |
|
{ |
|
private String _address; |
|
private int _l_port; |
|
private int _r_port; |
|
private bool _enabled = false; |
|
private bool _connected = false; |
|
|
|
public delegate void Recv_Mess_s(String s); |
|
public delegate void Recv_Mess_b(Byte[] arr); |
|
public static Recv_Mess_s onRecvS; |
|
public static Recv_Mess_b onRecvB; |
|
|
|
public bool Connected { get => _connected; } |
|
public int LocalPort { get => _l_port; } |
|
public int RemotePort { get => _r_port; } |
|
|
|
public UDPSrvCli(String address = "127.0.0.1", int lport = 21000, int rport = 21001) |
|
{ |
|
_address = address; |
|
_l_port = lport; |
|
_r_port = rport; |
|
|
|
try |
|
{ |
|
using (UdpClient tmp = new UdpClient(_l_port)) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine($"Client {_l_port}: OK"); |
|
#endif |
|
} |
|
} |
|
catch (Exception ee) |
|
{ |
|
_l_port = 21001; |
|
_r_port = 21000; |
|
#if DEBUG |
|
Console.WriteLine($"Client {_l_port}: OK"); |
|
#endif |
|
} |
|
Task.Run(ReceiveMessageAsync); |
|
|
|
_connected = true; |
|
_enabled = true; |
|
return; |
|
} |
|
async Task ReceiveMessageAsync() |
|
{ |
|
using (UdpClient receiver = new UdpClient(_l_port)) |
|
{ |
|
while (_enabled) |
|
{ |
|
var result = await receiver.ReceiveAsync(); |
|
if (result.Buffer[result.Buffer.Length - 2] == '\r' && result.Buffer[result.Buffer.Length - 1] == '\n') |
|
onRecvS(Encoding.UTF8.GetString(result.Buffer)); |
|
else |
|
onRecvB(result.Buffer); |
|
} |
|
} |
|
} |
|
|
|
public async void Send(String s) |
|
{ |
|
try |
|
{ |
|
using (UdpClient sender = new UdpClient()) |
|
{ |
|
byte[] data = Encoding.UTF8.GetBytes($"{s}\r\n"); |
|
await sender.SendAsync(data, data.Length, new IPEndPoint(IPAddress.Parse(_address), _r_port)); |
|
} |
|
} |
|
catch (Exception ee) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine(ee); |
|
#endif |
|
} |
|
} |
|
public async void Send(Byte[] b) |
|
{ |
|
try |
|
{ |
|
using (UdpClient sender = new UdpClient()) |
|
{ |
|
await sender.SendAsync(b, b.Length, new IPEndPoint(IPAddress.Parse(_address), _r_port)); |
|
} |
|
} |
|
catch (Exception ee) |
|
{ |
|
#if DEBUG |
|
Console.WriteLine(ee); |
|
#endif |
|
} |
|
} |
|
|
|
public void Stop() |
|
{ |
|
_enabled = false; |
|
_connected = false; |
|
} |
|
} |
|
}
|
|
|