-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWinSock.cs
198 lines (148 loc) · 4.85 KB
/
CWinSock.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Ground_Floor
{
class CWinSock
{
public bool bDisconnected;
public CWinSock(string sServer, int iPort)
{
byte[] iPAddr = new byte[4];
string[] tabstring = sServer.Split('.');
for (int iCpt = 0; iCpt <= 3; iCpt++)
iPAddr[iCpt] = Convert.ToByte(tabstring[iCpt]);
_myIp = new IPAddress(iPAddr);
_iPort = iPort;
_ipEnd = new IPEndPoint(_myIp, _iPort);
bDisconnected = false;
}
public void setPort(int iPort)
{
_iPort = iPort;
_ipEnd = new IPEndPoint(_myIp, _iPort);
}
public void Disconnect()
{
_myServer.Close();
}
public void DisConnect()
{
IAsyncResult res = null;
_myServer.EndConnect(res);
_myServer = null;
}
public bool Connect()
{
bool bReturn = false;
try
{
_myServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_myServer.Connect(_ipEnd);
Console.WriteLine("Connect to socket IpEnd to address " + _ipEnd.Address.ToString() + " port " + _ipEnd.Port.ToString() + ".");
bReturn = true;
bDisconnected = false;
}
catch
{
Console.WriteLine("Failed to Connect to socket IpEnd to address " + _ipEnd.Address.ToString() + " port " + _ipEnd.Port.ToString() + ".");
}
return bReturn;
}
public void WaitConnection()
{
try
{
_myServer.Listen(10);
Console.WriteLine("Server waiting for connection on port " + _iPort);
_myClient = _myServer.Accept();
_myClient.Blocking = true;
_bIsConnected = true;
Console.WriteLine("Client Connected on port " + _iPort);
}
catch
{
Console.WriteLine("Exception caught while waiting for client connection.");
}
}
public byte[] ReceiveByte()
{
int iReceived;
byte[] bBuffer = new byte[124];
byte[] returnBuffer = null;
try
{
iReceived = _myServer.Receive(bBuffer);
if (iReceived > 0)
{
returnBuffer = new byte[iReceived];
Array.Copy(bBuffer, returnBuffer, iReceived);
Console.WriteLine(iReceived.ToString() + " Byte received from client on port " + _iPort.ToString());
}
else
{
_bIsConnected = false;
}
Thread.Sleep(50);
}
catch
{
Console.WriteLine("Exception caught while waiting to receive data. Client might be disconnected.");
}
return returnBuffer;
}
public void SendAnswer(string sAnswer)
{
byte[] bBuffer;
char[] aChar;
int iCptChar;
aChar = new char[sAnswer.Length];
bBuffer = new byte[sAnswer.Length];
aChar = sAnswer.ToCharArray();
for (iCptChar = 0; iCptChar < sAnswer.Length; iCptChar++)
bBuffer[iCptChar] = (byte)aChar[iCptChar];
try
{
_myClient.Send(bBuffer);
}
catch
{
Console.WriteLine("Exception caught while sending data. Client might be disconnected.");
}
}
public void SendByte(byte[] cAnswer)
{
try
{
_myServer.Send(cAnswer);
}
catch
{
Console.WriteLine("Exception caught while sending data buffer. Client might be disconnected. Attempt to reconnect.");
bDisconnected = true;
Connect();
}
}
public bool IsClientConnected()
{
if (_myClient == null)
{
return false;
}
else
{
//return _myClient.Connected;
return _bIsConnected;
}
}
private IPAddress _myIp;
private IPEndPoint _ipEnd;
private Socket _myServer;
private Socket _myClient;
private int _iPort;
private bool _bIsConnected;
}
}