C# 从多个客户端向服务器发送视频文件

C# 从多个客户端向服务器发送视频文件,c#,video-streaming,client-server,C#,Video Streaming,Client Server,我正在使用我在网上找到的用于通信的客户机/服务器代码: 客户: public void Send(string name, string path) { try { IPAddress[] ipAddress = Dns.GetHostAddresses("address"); IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);

我正在使用我在网上找到的用于通信的客户机/服务器代码:

客户:

    public void Send(string name, string path)
    {
        try
        {
            IPAddress[] ipAddress = Dns.GetHostAddresses("address");
            IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
            Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

            string fileName = "somefile";
            string filePath = path;
            byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);

            byte[] fileData = File.ReadAllBytes(System.IO.Path.Combine(filePath, fileName)); 
            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            fileData.CopyTo(clientData, 4 + fileNameByte.Length);

            clientSock.Connect(ipEnd);
            clientSock.Send(clientData);
            MessageBox.Show("file has been send: " + fileName);

            clientSock.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("File Sending fail." + ex.Message);
        }
    }
服务器:

public void Receive()
    {
        try
        {
            lblInfo.Content = "That program can transfer small file. I've test up to 850kb file";
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            Socket clientSock = sock.Accept();
            // 1024 * 25.000 = 25mb max that can be received at once with this program.
            byte[] clientData = new byte[1024 * 25000];
            string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\";

            int receivedBytesLen = clientSock.Receive(clientData);

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            lblInfo.Content = "Client: connected & File started received.";

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            lblInfo.Content = "File: received & saved at path: " + receivedPath;

            bWrite.Close();
            clientSock.Close();
        }
        catch (Exception ex)
        {
            lblInfo.Content = "File Receiving fail." + ex.Message;
        }
    }
此代码适用于将文件传输到服务器的客户端。我想知道如何更改此代码,使其与可以向服务器发送文件的多个客户端通信

此外,服务器代码“挂起”在
Socket clientSock=sock.Accept()处并等待,直到它收到某个内容。如果有一个“监听器”监听新的输入文件,然后在代码中循环,而不是没完没了地排队等待,那就太好了


我对客户机/服务器编程不熟悉,对其他建议也耳熟能详。

这里有一些关于为新连接创建循环的解释:


嗨,我看过你给我的链接。我对客户机/服务器编程非常陌生。我在代码中的哪一行设置代码?提前感谢如果你想同时接受多个连接,你必须在线程中处理客户端消息。是的,这也是我在这个项目中需要的。同时处理几个连接。这些线程是什么样子的?创建一个新类,它将包含一个带有while循环的方法,您只需在启动线程之前传递套接字处理程序。然后,他们将处理客户端发送的数据。如果您想要创建线程的示例:但while循环只遍历一次。如果它再次循环,我会得到这个错误:索引和计数必须引用缓冲区中的一个位置。参数名称:字节。我刚才也犯了同样的错误。
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
listener.Bind(ipEnd);
listener.Listen(100);

Console.WriteLine("Waiting for a connection...");
Socket handler = listener.Accept();

while (true)
{
    // 1024 * 25.000 = 25mb max that can be received at once with this program.
    byte[] clientData = new byte[1024 * 25000];
    string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\";

    int bytesRec = handler.Receive(clientData);
    int fileNameLen = BitConverter.ToInt32(clientData, 0);
    string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

    BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
    bWrite.Write(clientData, 4 + fileNameLen, bytesRec - 4 - fileNameLen);
    bWrite.Close();
}

//dont forget to close handler at server shutdown
//handler.Shutdown(SocketShutdown.Both);
//handler.Close();