public void ServerData() { while (Main.Game.Instance.IsStarted && socket != null) { for (int i = 0; i < players.Count; ++i) { if (players[i].socket.Available > 0 && Main.Game.Instance.IsStarted) { StringBuilder builder = new StringBuilder(); int recivedCount = 0; byte[] recivedBytes = new byte[256]; while (players[i].socket.Available > 0 && Main.Game.Instance.IsStarted) { try { recivedCount = players[i].socket.Receive(recivedBytes); } catch { } builder.Append(Encoding.Unicode.GetString(recivedBytes, 0, recivedCount)); } Packet packet = Packet.GetPacket(builder.ToString()); Send(packet, players[i]); string[] splited = builder.ToString().Split(";"); for (int j = 0; j < splited.Length; ++j) { if (splited[j] != "" || splited[j] != " ") { HasReceived?.Invoke(players[i], new Server.DataReceivedArgs(Packet.GetPacket(splited[j].Replace(";", "")))); } } } if (players[i].socket == null || !players[i].socket.Connected) { players.RemoveAt(i); } } } }
public void ClientReceive() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), Server.PORT); socket.Connect(endPoint); if (socket.Connected) { Send(new PacketConnect(nickname)); } while (socket.Connected && Main.Game.Instance.IsStarted) { StringBuilder builder = new StringBuilder(); int receivedCount = 0; byte[] receivedBytes = new byte[256]; while (socket.Available > 0 && Main.Game.Instance.IsStarted) { try { receivedCount = socket.Receive(receivedBytes); } catch { break; } builder.Append(Encoding.Unicode.GetString(receivedBytes, 0, receivedCount)); } string[] splited = builder.ToString().Split(";"); for (int i = 0; i < splited.Length; ++i) { if (splited[i] != "" || splited[i] != " ") { HasReceived?.Invoke(null, new Server.DataReceivedArgs(Packet.GetPacket(splited[i].Replace(";", "")))); } } } }
public void ServerListing() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT); socket.Bind(endPoint); socket.Listen(5); while (Main.Game.Instance.IsStarted && socket != null) { Socket handler = null; try { handler = socket.Accept(); } catch { break; } StringBuilder builder = new StringBuilder(); int recivedCount = 0; byte[] recivedBytes = new byte[256]; do { recivedCount = handler.Receive(recivedBytes); builder.Append(Encoding.Unicode.GetString(recivedBytes, 0, recivedCount)); } while (handler.Available > 0 && Main.Game.Instance.IsStarted); Packet packetConnect = Packet.GetPacket(builder.ToString()); HasReceived?.Invoke(handler, new DataReceivedArgs(packetConnect)); for (int i = 0; i < players.Count; ++i) { SendTo(handler, new PacketConnect(players[i].nickname)); } SendTo(handler, new PacketConnect(nickname)); players.Add(new Player(handler, ((PacketConnect)packetConnect).Nickname)); } }