mirror of
https://gitlab1.ptb.de/waltem01/csharpsocketserver
synced 2024-02-04 23:19:43 +00:00
client to client functionality
This commit is contained in:
parent
2a2b3fa773
commit
2d3378fb6b
|
@ -10,12 +10,13 @@ public static class Connection {
|
|||
public static Socket self;
|
||||
private static byte[] buffer;
|
||||
private static string ipAddress;
|
||||
private static int listeners = 0;
|
||||
private static int listeners = 0, port;
|
||||
public static Action<Socket> disconnectMethod;
|
||||
public static List<MessageEvent> messageEvents;
|
||||
|
||||
public static void Initialize() {
|
||||
public static void Initialize(int _port=3000) {
|
||||
// Initialize global variables
|
||||
port = _port;
|
||||
buffer = new byte[1024];
|
||||
messageEvents = new List<MessageEvent>();
|
||||
disconnectMethod = new Action<Socket>((other)=>{});
|
||||
|
@ -33,7 +34,7 @@ public static class Connection {
|
|||
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));
|
||||
self.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
|
||||
self.Listen(10);
|
||||
|
||||
// Accept any client asynchronously
|
||||
|
@ -51,20 +52,29 @@ public static class Connection {
|
|||
public static void ClientConnect(string ipAndPort) {
|
||||
// Split ipAndPort into the ipAddress and Port
|
||||
string ipAddr = ipAddress;
|
||||
int port = 3000;
|
||||
int _port = port;
|
||||
if (ipAndPort != "") {
|
||||
string[] splits = ipAndPort.Split(':');
|
||||
ipAddr = splits[0];
|
||||
port = int.Parse(splits[1]);
|
||||
_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.Connect(new IPEndPoint(IPAddress.Parse(ipAddr), _port));
|
||||
|
||||
self.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), self);
|
||||
}
|
||||
|
||||
public static void ClientListen() {
|
||||
// Create a new socket server and listen for connections
|
||||
Socket self = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
self.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
|
||||
self.Listen(1);
|
||||
// Accept any client, stop listening after first client is connected and return client
|
||||
self.BeginAccept(new AsyncCallback(AcceptCallback), self);
|
||||
}
|
||||
|
||||
// End off the message send-callback
|
||||
private static void SendCallback(IAsyncResult AR) {
|
||||
Socket other = AR.AsyncState as Socket;
|
||||
|
|
Loading…
Reference in New Issue
Block a user