简体   繁体   English

Unity TCP服务器/客户端

[英]Unity TCP server/client

I'm sorry but I'm kinda having a confusing problem here... 抱歉,我在这里遇到一个令人困惑的问题...

I was using Unity recently and try to make a simple TCP server that can broadcast through all its clients, also to himself where the server itself is one of the client, like chat room or something. 我最近在使用Unity,并尝试制作一个可以通过其所有客户端广播的简单TCP服务器,也可以向自己广播该服务器本身是客户端之一的服务器,例如聊天室或其他内容。

so I did successfully done so, but the problem is, all the data that received by the client is actually twice of the initially data send from client, below is the example of my code 所以我确实做到了,但是问题是,客户端接收的所有数据实际上是客户端发送的初始数据的两倍,以下是我的代码示例

        public void broadcast(string data)
        {
            print("Broadcast");
            byte[] dataByte = Encoding.ASCII.GetBytes(data);
            print(ClientList.Count);
            for(int x = 0; x<ClientList.Count;x++)
            {
                ClientList[x].Send(dataByte);
                print ("something" +ClientList[x].Send(dataByte));
                print ("loop");
            }
        }

and this is how the client will receive it 这就是客户将收到它的方式

    private void ReceiveData(IAsyncResult ar)
    {
        print ("ReceiveData Client");
        Socket handler = (Socket)ar.AsyncState;
        try
        {
            int x = handler.EndReceive(ar);
            print("Receiving Data...");
            print (x);
            if (x > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(Encoding.ASCII.GetString(buffer, 0, x));
                string content = sb.ToString();
                print("Data Received: " + content);
                //string[] data = content.Split(';');
                //for (int i = 0; i < data.Length; i++)
                //{
                    this.ProcessData(x, content, handler);
                //}
                sb.Remove(0, sb.Length);
                handler.BeginReceive(buffer, 0, 1024, 0, new AsyncCallback(ReceiveData), handler);
            }
            else
            {
                print("Connection Closed...");
            }
        }
        catch (Exception e)
        {
            print(e.Message);
        }
    }

I try to send test;this is client and the result is below 我尝试发送test;this is client ,结果如下

Broadcast
1
something20
ReceiveData Client
loop
Receiving data...
40
Data Received: test;this is client test; this is client"

I don't know what or how this went wrong, any help will be appreciated. 我不知道这是怎么回事或怎么回事,我们将不胜感激。 Thanks b4 谢谢b4

The problem is within these two lines: 问题在以下两行中:

ClientList[x].Send(dataByte);
print ("something" +ClientList[x].Send(dataByte));

Notice how you've executed ClientList[x].Send(dataByte) twice? 请注意,您如何执行ClientList[x].Send(dataByte)两次? That's why you're getting it 2 times on the receiving end. 这就是为什么您在接收端获得2次它的原因。 Just remove one of the two lines and you should be fine. 只需删除两条线之一,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM