-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
62 lines (57 loc) · 1.47 KB
/
Client.cs
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
using System;
using System.Net;
using System.Net.Sockets;
namespace DataPacket_CSharp
{
public class Client
{
public Socket s = null;
public bool isConnected
{
get
{
if (s != null)
return s.Connected;
return false;
}
set
{
}
}
public string IP;
public int PORT;
private IPAddress ipAddress;
private IPEndPoint remoteEP;
public Client() { }
public Client(string _IP="127.0.0.1", int _PORT=2000)
{
ipAddress = IPAddress.Parse(_IP);
remoteEP = new IPEndPoint(ipAddress, _PORT);
TryConnect();
}
public bool TryConnect()
{
try
{
s = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(remoteEP);
return true;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
public bool CheckIsConntected()
{
try
{
this.isConnected = s.Connected;
//this.isConnected = !(s.Poll(1, SelectMode.SelectRead) && s.Available == 0);
}
catch (SocketException) { }
return this.isConnected;
}
}
}