larger buffer size and easy ip/port parsing

This commit is contained in:
waltem01 2022-09-20 12:30:49 +02:00
parent 3a00f778bf
commit ca763f2b4d

View File

@ -9,7 +9,7 @@ namespace SocketLib;
public static class Connection {
public static string ipAddress = "127.0.0.1";
public static int port = 3000;
private static byte[] buffer = new byte[1024];
private static byte[] buffer = new byte[4096];
public static Socket? self;
private static int listeners = 0;
public static Action<Socket>? connectMethod;
@ -18,24 +18,51 @@ public static class Connection {
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) {
public static void Initialize(string? _ipAddress=null, int? _port=null) {
// Initialize global variables
if (_port != null)
port = (int)_port;
// Get computers' ipv4 address
try {
string hostName = Dns.GetHostName();
IPHostEntry entryList = Dns.GetHostEntry(hostName);
IPAddress? ipv4Entry = null;
foreach (IPAddress entry in entryList.AddressList)
if (entry.AddressFamily == AddressFamily.InterNetwork) {
ipv4Entry = entry;
break;
}
if (ipv4Entry != null)
ipAddress = ipv4Entry.ToString();
} catch (Exception) {}
if (ipAddress == null) {
try {
string hostName = Dns.GetHostName();
IPHostEntry entryList = Dns.GetHostEntry(hostName);
IPAddress? ipv4Entry = null;
foreach (IPAddress entry in entryList.AddressList)
if (entry.AddressFamily == AddressFamily.InterNetwork) {
ipv4Entry = entry;
break;
}
if (ipv4Entry != null)
ipAddress = ipv4Entry.ToString();
} catch (Exception) {}
}
}
public static void ParseIPandPort(string ipAndPort) {
string ipAddr = ipAddress;
int _port = port;
if (ipAndPort != "") {
string[] splits = ipAndPort.Split(':');
ipAddr = splits[0];
try {
IPAddress.Parse(ipAddr);
} catch (Exception) {
IPHostEntry entryList = Dns.GetHostEntry(ipAddr);
IPAddress? ipv4Entry = null;
foreach (IPAddress entry in entryList.AddressList)
if (entry.AddressFamily == AddressFamily.InterNetwork) {
ipv4Entry = entry;
break;
}
if (ipv4Entry != null)
ipAddr = ipv4Entry.ToString();
}
_port = int.Parse(splits[1]);
}
ipAddress = ipAddr;
port = _port;
}
public static void ServerStart() {
@ -64,30 +91,11 @@ public static class Connection {
throw SOCKET_CONNECTED;
// Split ipAndPort into the ipAddress and Port
string ipAddr = ipAddress;
int _port = port;
if (ipAndPort != "") {
string[] splits = ipAndPort.Split(':');
ipAddr = splits[0];
try {
IPAddress.Parse(ipAddr);
} catch (Exception) {
IPHostEntry entryList = Dns.GetHostEntry(ipAddr);
IPAddress? ipv4Entry = null;
foreach (IPAddress entry in entryList.AddressList)
if (entry.AddressFamily == AddressFamily.InterNetwork) {
ipv4Entry = entry;
break;
}
if (ipv4Entry != null)
ipAddr = ipv4Entry.ToString();
}
_port = int.Parse(splits[1]);
}
ParseIPandPort(ipAndPort);
// Create a new socket client and connect to a server
self = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
self.Connect(new IPEndPoint(IPAddress.Parse(ipAddr), _port));
self.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), port));
self.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), self);