public void Listen(int port) { TcpServerSocket tcpServerSocket = new TcpServerSocket(); this.socket = tcpServerSocket; tcpServerSocket.OnClientConnected += delegate(TcpServerSocket sender, Socket client) { this.onConnected(); }; tcpServerSocket.Listen(port); }
static void initServer(string[] args) { int port = 0; try { port = int.Parse(args[1]); var server = new TcpServerSocket(); server.OnDisconnect += onDisconnect; server.Listen(port); _socket = server; start(); } catch (Exception) { Console.WriteLine("Invalid port"); } }
public override void Run(string[] args) { var port = 0; try { port = int.Parse(args[1]); } catch (Exception) { port = 3333; } var server = new TcpServerSocket(); _socket = server; server.OnDisconnect += onDisconnect; server.Listen(port); server.OnReceive += onReceive; Console.CancelKeyPress += onCancel; while (true) { server.Send(Encoding.UTF8.GetBytes(Console.ReadLine())); } }
public void Listen(int port) { if (m_Port != port && m_serverSocket != null) { DebugLog.Warning(""); Disconnect(); } m_Port = port; m_serverSocket = new TcpServerSocket(); m_serverSocket.OnClientConnect += OnClientConnected; m_serverSocket.OnClientDisconnect += OnClientDisconnected; m_serverSocket.OnReceive += OnReceived; m_serverSocket.OnDisconnect += OnDisconnected; m_serverSocket.Listen(m_Port); UpdateBehaviour.AddUpdate(this); }
void when_created() { TcpServerSocket server = null; before = () => { server = new TcpServerSocket(); }; it["has no connected clients"] = () => server.connectedClients.should_be(0); it["is not listening"] = () => server.isConnected.should_be_false(); it["can disconnect without triggering event"] = () => { server.OnDisconnect += (sender, e) => fail(); server.Disconnect(); }; it["can listen"] = () => { server.Listen(Port); server.isConnected.should_be_true(); // Cleanup server.Disconnect(); }; it["can not listen when address is used"] = () => { var blockingServer = new TcpServerSocket(); blockingServer.Listen(Port); server.Listen(Port); server.isConnected.should_be_false(); // Cleanup blockingServer.Disconnect(); }; it["can not send"] = () => server.Send(new byte[] { 1, 2 }); context["when listening"] = () => { before = () => { server.Listen(Port); }; after = () => { try { server.Disconnect(); } catch (Exception) { } }; it["can disconnect"] = () => { var didDisconnect = false; server.OnDisconnect += (sender, e) => didDisconnect = true; server.Disconnect(); didDisconnect.should_be_true(); server.isConnected.should_be_false(); }; it["accepts connections"] = () => { var clientConnected = false; server.OnClientConnect += (sender, e) => clientConnected = true; createAndConnectClient(Port); server.connectedClients.should_be(1); clientConnected.should_be_true(); }; it["accepts multiple connections"] = () => { createAndConnectClient(Port); createAndConnectClient(Port); createAndConnectClient(Port); server.connectedClients.should_be(3); }; context["when connection accepted"] = () => { Socket client1 = null; Socket client2 = null; before = () => { client1 = createAndConnectClient(Port); client2 = createAndConnectClient(Port); }; it["can disconnect"] = () => { server.Disconnect(); wait(); server.connectedClients.should_be(0); }; it["receives client disconnect"] = () => { var clientDidDisconntect = false; server.OnReceive += (sender, e) => fail(); server.OnClientDisconnect += (sender, e) => clientDidDisconntect = true; client1.Disconnect(false); client1.Close(); wait(); server.connectedClients.should_be(1); clientDidDisconntect.should_be_true(); }; it["receives message"] = () => { var message = "Hello"; ReceiveEventArgs receiveEventArgs = null; server.OnReceive += (sender, e) => receiveEventArgs = e; client1.Send(Encoding.UTF8.GetBytes(message)); wait(); message.should_be(Encoding.UTF8.GetString(receiveEventArgs.bytes)); receiveEventArgs.client.should_not_be_null(); }; it["receives multiple messages"] = () => { var message1 = "Hello1"; var message2 = "Hello2"; ReceiveEventArgs receiveEventArgs = null; server.OnReceive += (sender, e) => receiveEventArgs = e; client1.Send(Encoding.UTF8.GetBytes(message1)); wait(); message1.should_be(Encoding.UTF8.GetString(receiveEventArgs.bytes)); client1.Send(Encoding.UTF8.GetBytes(message2)); wait(); message2.should_be(Encoding.UTF8.GetString(receiveEventArgs.bytes)); }; it["can respond to client"] = () => { var clientMessage = "Hello"; var serverMessage = "Hi"; var receivedMessage = string.Empty; ReceiveEventArgs receiveEventArgs = null; server.OnReceive += (sender, e) => receiveEventArgs = e; client1.Send(Encoding.UTF8.GetBytes(clientMessage)); wait(); prepareForReceive(client1, msg => receivedMessage = msg); server.SendWith(receiveEventArgs.client, Encoding.UTF8.GetBytes(serverMessage)); wait(); receivedMessage.should_be(serverMessage); }; it["can send to all connected clients"] = () => { var message = "Hello"; var client1ReceivedMessage = string.Empty; var client2ReceivedMessage = string.Empty; prepareForReceive(client1, msg => client1ReceivedMessage = msg); prepareForReceive(client2, msg => client2ReceivedMessage = msg); server.Send(Encoding.UTF8.GetBytes(message)); wait(); client1ReceivedMessage.should_be(message); client2ReceivedMessage.should_be(message); }; }; }; }
public static void Main(string[] args) { DataTable dt = new DataTable(); var dddd = dt.SerializeToByte(); int len = 1234445566; byte[] d = new byte[4]; d[0] = (Byte)((len & 0x7f000000) >> 24); d[1] = (Byte)((len & 0xff0000) >> 16); d[2] = (Byte)((len & 0xff00) >> 8); d[3] = (Byte)(len & 0xff); int reciveObjLength = d[3]; reciveObjLength += (d[2] << 8); reciveObjLength += d[1] << 16; reciveObjLength += (d[0] & 0x7f) << 24; Message msg = new Message(); var server = new TcpServerSocket(); //server.eShowMsg += Server_eShowMsg; //server.HandleMessage += Server_HandleMessage; server.OnError += Server_OnError; if (server.Bind(23456)) { server.Listen(); } int lastSended = 0, lastRec = 0; new Action <TcpServerSocket>((s) => { while (true) { // while (sendCount < recCount) { //lock (lck2) //sendCount += s.SendMessageAsync(msg); // Thread.Sleep(1); } Thread.Sleep(10); } }).BeginInvoke(server, null, null); while (true) { // if (rp != null) { // server.SendMessageAsync(msg); // Thread.Sleep(1); } // else { Thread.Sleep(1000); Console.WriteLine($"收到:{recCount} 发出:{sendCount} 客户端:{server.ClientCount}"); Console.Title = $"接收:{recCount - lastRec} 发出:{sendCount - lastSended} 客户端:{server.ClientCount}"; lastRec = recCount; lastSended = sendCount; } //reCount = 0; } }