项目建议+不寻常的tcpclient问题 [英] Project advice + unusual tcpclient issue

查看:60
本文介绍了项目建议+不寻常的tcpclient问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我正在为游戏创建服务器我将开始使用虚幻引擎进行开发,但我希望服务器是一个WinForms应用程序。我的方法是创建一个C#WinForms应用程序,它能够接受TCP连接并存储它们,从任何接收数据包并在必要时进行广播。一个例子是PacketPlayerDisconnect,当播放器与服务器断开连接时,该数据包将被发送到所有其他客户端并包含playerID,以便在客户端上删除播放器模型。



首先,我想知道这是否是最佳方法,如果您有任何建议。



编辑:以下在我的解决方案中解决了



我的代码实现数据包系统的方式是通过以下格式发送字节数组:



0 | 1 | 2 ...

数据长度| packetID |数据包内容...



字节1和2允许我构造正确的数据包并在收到时填充其内容。



我发现一个奇怪的问题,服务器向客户端发送一个数据包,并说它收到7.这是代码:



服务器:

Hello

I'm working on creating a server for a game I'm going to begin development on using Unreal Engine, however I want the server to be a WinForms application. My approach was to create a C# WinForms application which is able to accept TCP connections and store them, receive packets from any and broadcast if necessary. An example would be PacketPlayerDisconnect, when a player disconnects from the server, this packet is sent to all of the other clients and contains the playerID, so that player model is removed on the clients.

First of all I'd like to know whether this is the best approach or not, and if you have any advice.

The following is solved in my solution

The way my code implements a packet system is by sending a byte array in the following format:

0 | 1 | 2...
data length | packetID | packet contents...

Bytes 1 and 2 allow me to construct the correct packet and populate its contents when it's received.

I'm seeing a strange issue where the server sends a single packet to the client and it says it receives 7. Here's the code:

Server:

public class GameServer
    {
        private Main main;
        private List<GameClient> clients = new List<GameClient>();

        #region Config
        private int port = 1123;
        #endregion

        #region Connection
        private TcpListener listener;
        private bool listening = false;
        private Thread listenThread;
        #endregion

        public GameServer(Main main)
        {
            this.main = main;
        }

        public void start()
        {
            listener = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
            listener.Start();

            listenThread = new Thread(listen);
            listenThread.Start();
        }

        public void stop()
        {
            listening = false;
            listener.Server.Close();
            listener.Stop();

            clients.ForEach((client) => client.disconnect());
        }

        private void listen()
        {
            listening = true;

            while(listening)
            {
                TcpClient client = listener.AcceptTcpClient();
                GameClient game_client = new GameClient(main, client);
                clients.Add(game_client);
                game_client.sendPacket(new PacketRawData("Hello, Client"));
            }
        }



客户:


Client:

public class GameClient
    {
        private Main main;
        private TcpClient client;
        private NetworkStream client_stream;
        private Thread listenThread;
        private bool listening;

        public GameClient(Main main, String ipAddress, int portNum)
        {
            this.main = main;
            client = new TcpClient();
            
            listenThread = new Thread(receive);
            listenThread.Start();

            client.Connect(ipAddress, portNum);
            client_stream = client.GetStream();
        }

        public void send(string message)
        {
            byte[] message_data = Encoding.UTF8.GetBytes(message);
            byte[] data = new byte[1 + message_data.Length];

            int message_length = message_data.Length;
            data[0] = (byte)message_length;

            for(int i = 0; i < message_length; i++) // place message bytes after length byte
            {
                data[i + 1] = message_data[1];
            }

            NetworkStream client_stream = client.GetStream();
            client_stream.Write(data, 0, data.Length);
            client_stream.Flush();

            main.logPacket(PacketDirection.SEND, message);
        }

        private void receive()
        {
            listening = true;

            while(listening)
            {
                if(client_stream.DataAvailable)
                {
                    byte[] length_byte = new byte[1];
                    client_stream.Read(length_byte, 0, length_byte.Length); // read first byte (length)

                    int data_length = length_byte[0];
                    byte[] data_bytes = new byte[data_length]; // read rest of data
                    client_stream.Read(length_byte, 0, length_byte.Length);

                    String data = Encoding.UTF8.GetString(data_bytes);

                    main.logPacket(PacketDirection.RECEIVE, data);
                }
            }            
        }
    }





我尝试了什么:



使用Wireshark监控数据包,但它没有检测到我的接口



使用多种方法发送/接收。



What I have tried:

Using Wireshark to monitor packets, but it don't detect my interfaces

Using several methods to send/receive.

推荐答案

解决方案:这段代码是问题所在:



Solution: This piece of code was the problem:

for(int i = 0; i < message_length; i++) // place message bytes after length byte
{
        data[i + 1] = message_data[1];
}

有一个错字;

message_data[1];

应该是

message_data[i];



这导致了很多a要发送而不是正确的消息。


This caused lots of "a"s to be sent instead of the correct message.


这篇关于项目建议+不寻常的tcpclient问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆