-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.cs
More file actions
55 lines (46 loc) · 1.5 KB
/
TCPClient.cs
File metadata and controls
55 lines (46 loc) · 1.5 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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace CoreliumSocketAPI
{
public class TCPClient : ISocketService
{
public event Action Connected;
private readonly string name;
private readonly string ip;
private readonly int port;
private readonly CoreliumSocket server;
private bool isConnected = false;
public TCPClient(string name, int bufferSize, string ip, int port)
{
this.name = name;
this.ip = ip;
this.port = port;
server = new CoreliumSocket(bufferSize, SType.TCP, ip, port);
}
public ISocketService Start()
{
var point = new IPEndPoint(IPAddress.Parse(ip), port);
server.socket.BeginConnect(point, (a) =>
{
server.socket.EndConnect(a);
server.StartReceive();
server.Send("cname" + name);
Connected?.Invoke();
isConnected = true;
}, null);
return this;
}
public void Send(byte[] buffer) =>
server.Send(buffer);
public void Send(string message) =>
server.Send(message);
public void Stop() =>
server.socket.BeginDisconnect(false, (a) => server.socket.EndDisconnect(a), null);
public bool IsConnected() =>
isConnected;
public CoreliumSocket GetServer() => server;
}
}