mirror of
https://gitlab1.ptb.de/waltem01/csharpsocketserver
synced 2024-02-04 23:19:43 +00:00
non-null event list & SOCKET_CONNECTED exception
This commit is contained in:
parent
f72ab420a5
commit
3a00f778bf
21
SocketLib.cs
21
SocketLib.cs
|
@ -7,21 +7,21 @@ using System;
|
|||
|
||||
namespace SocketLib;
|
||||
public static class Connection {
|
||||
public static int port = 3000;
|
||||
public static Socket? self;
|
||||
private static byte[] buffer = new byte[1024];
|
||||
public static string ipAddress = "127.0.0.1";
|
||||
public static int port = 3000;
|
||||
private static byte[] buffer = new byte[1024];
|
||||
public static Socket? self;
|
||||
private static int listeners = 0;
|
||||
public static Action<Socket>? disconnectMethod;
|
||||
public static Action<Socket>? connectMethod;
|
||||
public static Action<Socket>? disconnectMethod;
|
||||
public static Action<Socket, string>? messageMethod;
|
||||
public static List<MessageEvent>? messageEvents;
|
||||
public static List<MessageEvent> messageEvents = new List<MessageEvent>();
|
||||
private static Exception SOCKET_CONNECTED = new Exception("Socket already connected. Connection needs to be closed first.");
|
||||
|
||||
public static void Initialize(int? _port=null) {
|
||||
// Initialize global variables
|
||||
if (_port != null)
|
||||
port = (int)_port;
|
||||
messageEvents = new List<MessageEvent>();
|
||||
|
||||
// Get computers' ipv4 address
|
||||
try {
|
||||
|
@ -39,6 +39,9 @@ public static class Connection {
|
|||
}
|
||||
|
||||
public static void ServerStart() {
|
||||
if (self != null)
|
||||
throw SOCKET_CONNECTED;
|
||||
|
||||
// Create a new socket server and listen for connections
|
||||
self = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
self.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
|
||||
|
@ -57,6 +60,9 @@ public static class Connection {
|
|||
}
|
||||
|
||||
public static void ClientConnect(string ipAndPort) {
|
||||
if (self != null)
|
||||
throw SOCKET_CONNECTED;
|
||||
|
||||
// Split ipAndPort into the ipAddress and Port
|
||||
string ipAddr = ipAddress;
|
||||
int _port = port;
|
||||
|
@ -90,6 +96,9 @@ public static class Connection {
|
|||
}
|
||||
|
||||
public static void ClientListen() {
|
||||
if (self != null)
|
||||
throw SOCKET_CONNECTED;
|
||||
|
||||
// Create a new socket server and listen for connections
|
||||
Socket own = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
own.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
|
||||
|
|
Loading…
Reference in New Issue
Block a user