-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocketHelper.cs
47 lines (43 loc) · 1.22 KB
/
SocketHelper.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace BattleBotServer
{
public class SocketHelper
{
private IPEndPoint ipendpoint;
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public SocketHelper(IPEndPoint clientIP)
{
connectToSocket(clientIP);
}
private void connectToSocket(IPEndPoint IP)
{
try
{
socket.Connect(IP);
}
catch (Exception e)
{
throw new ArgumentException(e.Message);
}
ipendpoint = IP;
}
public void SendToServer(string dataToSend)
{
var data = Encoding.ASCII.GetBytes(dataToSend);
try
{
socket.Send(data);
Console.WriteLine("Data send! Data: \"" + dataToSend + "\"");
}
catch (Exception)
{
Console.WriteLine("Couldn't send the command.\nCheck if the client didn't crash");
}
Thread.Sleep(50);
}
}
}