added client connection

This commit is contained in:
waltem01 2022-06-28 10:23:18 +02:00
parent 5009197a70
commit 362b3005fe

View File

@ -6,7 +6,7 @@ using System.Threading;
using System.Collections;
namespace SocketLib;
public static class Server {
public static class Connection {
public static Socket self;
private static byte[] buffer;
private static string ipAddress;
@ -30,7 +30,7 @@ public static class Server {
} catch (Exception) {}
}
public static void Start() {
public static void ServerStart() {
// 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), 3000));
@ -48,6 +48,23 @@ public static class Server {
}
}
public static void ClientConnect(string ipAndPort) {
// Split ipAndPort into the ipAddress and Port
string ipAddr = ipAddress;
int port = 3000;
if (ipAndPort != "") {
string[] splits = ipAndPort.Split(':');
ipAddr = splits[0];
port = int.Parse(splits[1]);
}
// 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.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), self);
}
// End off the message send-callback
private static void SendCallback(IAsyncResult AR) {
Socket other = AR.AsyncState as Socket;