-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerSocket.cs
35 lines (33 loc) · 1.06 KB
/
ServerSocket.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace Sockets {
class ServerSocket {
private Socket serverSocket;
/// <summary>
/// Create a new ServerSocket object
/// </summary>
/// <param name="port">The port to bind</param>
public ServerSocket(int port) {
this.serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
try {
this.serverSocket.Bind(new IPEndPoint(0, port));
this.serverSocket.Listen(10);
} catch (Exception e) {
Debug.WriteLine(e.Message);
this.serverSocket.Close();
return;
}
}
/// <summary>
/// Wait and accept any connection
/// </summary>
/// <returns>A Socket to connect to</returns>
public Socket Accept() => this.serverSocket.Accept();
/// <summary>
/// Close the socket
/// </summary>
public void Close() => this.serverSocket.Close();
}
}