【问题标题】:send video files to server from multiple clients从多个客户端向服务器发送视频文件
【发布时间】:2011-12-07 09:25:04
【问题描述】:

我正在使用我在网上找到的这个客户端/服务器代码进行通信:

客户:

    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;
        }
    }

此代码适用于将文件传输到服务器的 1 个客户端。我想知道如何更改此代码以使其与可以将文件发送到服务器的多个客户端进行通信?

另外,服务器代码“挂起”在Socket clientSock = sock.Accept(); 并等待直到它收到一些东西。如果有一个“侦听器”来侦听新的传入文件然后循环通过代码,而不是在那一行无休止地等待,那就太好了。

我是客户端/服务器编程的新手,并且非常愿意听取其他建议。

【问题讨论】:

    标签: c# video-streaming client-server


    【解决方案1】:

    这里有关于为新连接创建循环的说明:

    http://msdn.microsoft.com/en-us/library/dz10xcwh.aspx

    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();
    

    【讨论】:

    • 嗨,我已经阅读了你给我的那个链接。我对客户端/服务器编程真的很陌生。我在哪里设置我的代码中的那些代码行?提前致谢
    • 那么如果你想同时接受多个连接,你将不得不在线程中处理客户端消息。
    • 是的,我也需要这个项目。同时处理多个连接。这些线程会是什么样子?
    • 创建一个新类,该类将包含一个带有 while 循环的方法,您只需在启动线程之前传递套接字处理程序。然后他们将处理客户端发送的数据。如果您想创建线程示例:msdn.microsoft.com/en-us/library/7a2f3ay4%28v=vs.80%29.aspx
    • 但是 while 循环只会遍历一次。如果它再次循环通过它,我会得到这个错误:索引和计数必须引用缓冲区中的一个位置。参数名称:字节。我刚刚做了那个线程,同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2011-05-29
    • 2021-12-04
    相关资源
    最近更新 更多