通过TCP / IP将字符串传输到多台计算机

时间:2018-07-31 19:41:08

标签: c# string tcp kinect tcplistener

该代码当前采用kinect身体数据并将其写入Excel工作表中。我还有另一个代码,可以在两台计算机之间创建TCP / IP连接。我如何最好地结合这两个想法?

我要求此“联合字符串数据”通过网络发送,并由另一台服务器计算机实时收集,而不是在excel文件中。你有什么建议吗 ?

换句话说,我有一些服务器代码,但是我希望服务器侦听客户端代码(另一台计算机)正在生成的Kinect字符串数据,当前正在将其写入本地excel文件。

客户端

...
  _recordBody = !_recordBody;
                if (_recordBody == false)
                {
                    recordButton.Content = "Record";
                    return;
                }
                recordButton.Content = "Stop Recording";
                currentTrackingID = 0;
                // create a csv file and write file header
                string currPath = System.IO.Directory.GetCurrentDirectory();
                string folder = "recordings";
                string recordPath = System.IO.Path.Combine(currPath, folder);
                if (!Directory.Exists(recordPath))
                {
                    Directory.CreateDirectory(recordPath);
                }
                timeRecordingStarted = DateTime.Now;
                string filename = timeRecordingStarted.ToString("yyyy-MM-dd HH-mm-ss");
                filename = filename + ".csv";
                filePath = System.IO.Path.Combine(recordPath, filename);
                string[] writtentext = {"time," + "headX," + 
                                        "headY," +
                                        "headZ," +
                                        "headS," +
                                        "neckX," + 
                                        "neckY," +
                                        "neckZ," +
                                        "neckS," +
                                        "spineShoulderX," + 
                                        "spineShoulderY," +
                                        "spineShoulderZ," +
                                        ...
                                        "handLeftY," +
                                        "handLeftZ," +
                                        "handLeftS," +
                                        "handTipRightX," + 
                                        "handTipRightY," +
                                        "handTipRightZ," +
                                        "handTipRightS," +
                                        "handTipLeftX," + 
                                        "handTipLeftY," +
                                        "handTipLeftZ," +
                                        "handTipLeftS"
                                       };
                File.WriteAllLines(filePath, writtentext);
         ...

服务器代码

    static TcpListener tcpListener = new TcpListener(10);
    static void Listeners()
    {

        Socket socketForClient = tcpListener.AcceptSocket();
        if (socketForClient.Connected)
        {
            Console.WriteLine("Client:"+socketForClient.RemoteEndPoint+" now connected to server.");
            NetworkStream networkStream = new NetworkStream(socketForClient);
            System.IO.StreamWriter streamWriter =
            new System.IO.StreamWriter(networkStream);
            System.IO.StreamReader streamReader =
            new System.IO.StreamReader(networkStream);


            {
                string theString = streamReader.ReadLine();
                Console.WriteLine("Message recieved by client:" + theString);
                if (theString == "exit")
                    break;
            }
            streamReader.Close();
            networkStream.Close();
            streamWriter.Close();
            //}

        }
        socketForClient.Close();
        Console.WriteLine("Press any key to exit from server program");
        Console.ReadKey();
    }

    public static void Main()
    {
        //TcpListener tcpListener = new TcpListener(10);
        tcpListener.Start();
        Console.WriteLine("************This is Server program************");
        Console.WriteLine("How many clients are going to connect to this server?:");
        int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());
        for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
        {
            Thread newThread = new Thread(new ThreadStart(Listeners));
            newThread.Start();
        }


    }
}

0 个答案:

没有答案