private static void ServerConnection_OnAccept(object self) { Energy.Core.Network.SocketClient clientConnection = self as Energy.Core.Network.SocketClient; string text = string.Format(Global.HelloText, Global.ClientCounter.Increment()); clientConnection.Send(System.Text.Encoding.UTF8.GetBytes(text)); }
private static void ServerConnection_OnReceive(object self, byte[] data) { Energy.Core.Network.SocketClient clientConnection = self as Energy.Core.Network.SocketClient; string text = Encoding.UTF8.GetString(data); Console.WriteLine("Client message from " + clientConnection.ToString() + ": " + text); clientConnection.Send(System.Text.Encoding.UTF8.GetBytes(text)); }
static void Main(string[] args) { //(new SimpleClient()).Start(); //host = "localhost"; //host = "211.1.1.1"; //host = "::1"; string host = Energy.Base.Text.GetElementOrEmpty(args, 0); int port = Energy.Base.Cast.StringToInteger(Energy.Base.Text.GetElementOrEmpty(args, 1)); if (string.IsNullOrEmpty(host)) { host = "127.0.0.1"; } if (port == 0) { port = 9000; } string value; int integer; value = Energy.Core.Tilde.Ask("Host", host); if (!string.IsNullOrEmpty(value)) { host = value; } value = Energy.Core.Tilde.Ask("Port", port.ToString()); if (0 < (integer = Energy.Base.Cast.StringToInteger(value))) { port = integer; } string address = Energy.Core.Network.GetHostAddress(host, AddressFamily.InterNetwork); Energy.Core.Tilde.WriteLine($"Trying to connect to ~w~{address}~0~:~y~{port}~0~ and start conversation..."); IPAddress ipAddress = IPAddress.Parse(address); //IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); Energy.Core.Network.SocketClient socketConnection = new Energy.Core.Network.SocketClient(); socketConnection.Host = host; socketConnection.Port = port; socketConnection.OnConnect += SocketConnection_OnConnect; socketConnection.OnReceive += SocketConnection_OnReceive; socketConnection.OnSend += SocketConnection_OnSend; socketConnection.Connect(); Energy.Core.Tilde.Pause(); }
private static void Connection_OnConnect(object self) { Energy.Core.Network.SocketClient connection = self as Energy.Core.Network.SocketClient; if (connection == null) { string tilde = "~r~ERROR~0~ ~lm~Object is null in ~y~Connection_OnConnect"; Energy.Core.Tilde.WriteLine(tilde); return; } int size = 10000; if (!connection.Send(new string('A', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('B', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('C', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('D', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Receive()) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Receive error in ~y~Connection_OnConnect"); } else { Energy.Core.Tilde.WriteLine("~r~OK~0~ ~lc~Start receiving in ~y~Connection_OnConnect"); } if (!connection.Send(new string('A', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('B', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('C', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } if (!connection.Send(new string('D', size) + "\r\n")) { Energy.Core.Tilde.WriteLine("~r~ERROR~0~ ~lm~Send error in ~y~Connection_OnConnect"); } }
static void Main(string[] args) { Energy.Core.Tilde.WriteLine("Welcome to ~lm~Network ~lc~Client~0~ using ~y~Energy.Core.Network.~lc~SocketClient"); Energy.Core.Network.SocketClient connection = new Energy.Core.Network.SocketClient(); connection.Host = "localhost"; connection.Port = 9000; connection.OnReceive += Connection_OnReceive; connection.OnSend += Connection_OnSend; connection.OnConnect += Connection_OnConnect; connection.OnException += Connection_OnException; connection.Timeout = 5000; connection.Retry = 1; connection.Connect(); Energy.Core.Tilde.Pause(); }