-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpClient.cs
More file actions
46 lines (40 loc) · 1.04 KB
/
TcpClient.cs
File metadata and controls
46 lines (40 loc) · 1.04 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
using System;
using System.Net;
using System.Text;
namespace BlockchainExample.Server
{
class TcpClient : IDisposable
{
private readonly string _ipAddress;
private readonly int _port;
private readonly System.Net.Sockets.TcpClient _client;
public TcpClient(string ipAddress, int port)
{
_ipAddress = ipAddress;
_port = port;
_client = new System.Net.Sockets.TcpClient();
}
public void Connect()
{
var ipAddress = IPAddress.Parse(_ipAddress);
_client.Connect(ipAddress, _port);
}
public void Send(string message)
{
var data = Encoding.ASCII.GetBytes(message);
using (var stream = _client.GetStream())
{
stream.Write(data, 0, data.Length);
}
}
public void Close()
{
_client.Close();
}
public void Dispose()
{
Close();
_client.Dispose();
}
}
}