读取阵列中多个客户端发送的数据并更新值C# [英] Read data sent by multiple Clients in an array and update values C#

查看:37
本文介绍了读取阵列中多个客户端发送的数据并更新值C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台客户端计算机将Kinect联合数据发送到然而,服务器计算机只有一个变量( theString
从两个客户端读取数据而不必复制方法有没有办法设置两个不同的变量从两个不同的客户端计算机读取数据。此外,
结果   ;变量
将数组放入数组形式,但我希望它以[time,joint.x,joint,y,joint.z ...... joint25.z]的形式存在(那里)是25个关节),然后更新其值。目前,字符串和数组只保留打印值而不是更新它。所以我
想要两个变量来区分来自两台计算机的数据并显示和更新数组值。


 public class AsynchIOServer 
{
static void监听器(对象状态)
{
TcpListener listener = state as TcpListener;
using(Socket socketForClient = listener.AcceptSocket())
{
if(socketForClient.Connected)
{
Console.WriteLine(" Client:" + socketForClient.RemoteEndPoint +"现在连接到服务器。";
使用(NetworkStream networkStream = new NetworkStream(socketForClient))
using(System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream))
using(System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream))
{
try
{
while(true)
{
string theString = streamReader.ReadLine( );
if(string.IsNullOrEmpty(theString)== false)
{
Console.WriteLine("" + theString)收到的消息;
var result = theString.Split(',');
}
}

}
catch(exception ex)
{
Console.WriteLine(" Error:" + ex。信息);
}
}
}
}
Console.WriteLine("按任意键退出服务器程序");
Console.ReadKey();
}

public static void Main()
{

TcpListener [] listeners =
{
new TcpListener(15 ),new TcpListener(10)
};


Console.WriteLine(" ***********这是服务器程序***********");
Console.WriteLine("有多少客户端将连接到此服务器?:";);
int numberOfClientsYouNeedToConnect = int.Parse(Console.ReadLine());


foreach(侦听器中的TcpListener侦听器)
{
listener.Start();
for(int i = 0; i< numberOfClientsYouNeedToConnect; i ++)
{
Thread newThread = new Thread(new ParameterizedThreadStart(Listeners));
newThread.Start(listener);
}
}

}
}

解决方案

您好,



 我建议您检查一下数据字符串并由JointXX


解析为容器类Dictionary< JointXX,Vector3D>。然后,当读取新数据


时,您只能再次重新分析。但这次只更新由JointXX索引的Vector3D的值


。 您可以使用自定义3D点类,但


如果您稍后决定在图形中使用JointXX 3D空间XYZ,那么最好


存储为Vector3D从头开始。


 


 如果您提供客户输出的样本,那么我们可以提供


<更多细节可以帮助您的项目进展。


 


 希望这会有所帮助:)


I have two Client Computers sending Kinect joint data to the Server computer however only one variable (theString) is reading the data from both Clients without having to duplicate the method is there any way to set up two different variables reading data from two different client computers. Also,results variable put the array in an array form but I want it to put it in the form of [time, joint.x, joint,y, joint.z ...... joint25.z] (there are 25 joints) and then update its value. Currently, the string and array just keep printing values instead of updating it. So I want two variables to distinguish the data incoming from two computers and display and update the array value.

public class AsynchIOServer
{
    static void Listeners(object state)
    {
        TcpListener listener = state as TcpListener;
        using (Socket socketForClient = listener.AcceptSocket())
        {
            if (socketForClient.Connected)
            {
                Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
                using (NetworkStream networkStream = new NetworkStream(socketForClient))
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream))
                using (System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream))
                {
                    try
                    {
                        while (true)
                        {
                            string theString = streamReader.ReadLine();
                            if (string.IsNullOrEmpty(theString) == false)
                            {
                                Console.WriteLine("Message recieved by  " + theString);
                                var result = theString.Split(',');
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                    }
                }
            }
        }
        Console.WriteLine("Press any key to exit from server program");
        Console.ReadKey();
    }

    public static void Main()
    {

        TcpListener[] listeners =
        {
            new TcpListener(15), new TcpListener(10)
        };


        Console.WriteLine("***********This is Server program***********");
        Console.WriteLine("How many clients are going to connect to this server?:");
        int numberOfClientsYouNeedToConnect = int.Parse(Console.ReadLine());


        foreach (TcpListener listener in listeners)
        {
            listener.Start();
            for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
            {
                Thread newThread = new Thread(new ParameterizedThreadStart(Listeners));
                newThread.Start(listener);
            }
        }

    }
}

解决方案

Hello,

 I suggest you examine the format of the data string and parse by JointXX

into a container class Dictionary<JointXX, Vector3D>. Then when new data

is read, you would only reparse again. But this time only update the values

of the Vector3D indexed by JointXX.  You can use a custom 3D point class but

if you decide later to use the JointXX 3D space XYZ in graphics then better to

store as Vector3D from the start.

 

 If you provide a sample of the output from the clients then we can provide

some more details that could help your project progression.

 

 Hope this helps :)


这篇关于读取阵列中多个客户端发送的数据并更新值C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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