代码之家  ›  专栏  ›  技术社区  ›  Metalstorm

如何在c sharp中同时通过tcp发送多个文件?

  •  1
  • Metalstorm  · 技术社区  · 14 年前

    我有一个tcp客户机-服务器实现运行在同一个程序中,在不同的后台工作线程上。在多台计算机上会有此程序的实例,以便它们可以在彼此之间发送和接收文件。我可以使用网络流在计算机之间按顺序发送文件,但如何同时从计算机A发送多个文件到计算机B。

    通过一个连接(socket)发送多个文件是可以的,但是如果有多个网络流将数据发送到客户机,客户机不知道哪个数据块与哪个文件分开?

    客户机是否有可能两次连接到服务器(在不同的端口上,因为一个“随机”端口/未使用的端口被分配给连接),然后每个连接都有自己的流,允许同时发送两个文件?

    谢谢你的时间和努力。

    5 回复  |  直到 14 年前
        1
  •  1
  •   casperOne    14 年前

    你可以那样做,但我看不到好处。除非每一个连接都被限制在某个地方,否则实际上会导致两倍于I/O操作的开销。

    这和把文件写到磁盘上是一样的,仅仅因为你把它分成两个线程并不意味着它会更快,因为磁盘只能一次写入。你可能真的看到 更慢的 响应时间。

        2
  •  5
  •   Jon Skeet    14 年前

    客户端当然可以多次连接到服务器,而且可能应该连接。

    但是,您可以指定相同的服务器端口-不同的 地方的 端口将在服务器端分配,但客户端不需要知道这一点。(想想web服务器- 太多了 所有客户端都将同时连接到端口80。)

    当然,也会自动为您分配独立的客户端端口-基本上,连接不应该相互干扰。

        3
  •  3
  •   user416134    13 年前

    您需要使用异步客户机和服务器套接字。基本上不用Recieve和Send,而是使用BeginRecieve、BeginSend、BeginConnect和beginacept。这样线程就为您完成了。在工作线程中拥有每个连接不是一个好主意。这样,每个新的请求都会同时处理(异步)。您还可以使用发送文件的前几个字节来存储有关该文件的数据。下面的例子。当然,下面的数字(1,2,3..)是一个字符串文件名,您在上面调用了Bitconverter.GetBytes(string var)。希望这有帮助。maxpfc@gmail.com(skype)

    byte[] completefile = [1,2,3,4,5,6,7,8,9];
    byte[] filename;
    filename = split(lcomplefile, 3);  
    

    下面是一个可编译的Asyc套接字示例。

    using System;
    using System.Drawing;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;
    class AsyncTcpClient Form:
    {
    private TextBox newText;
    private TextBox conStatus;
    private ListBox results;
    private Socket client;
    private byte[] data = new byte[1024];
    private int size = 1024;
    public AsyncTcpClient()
    {
    Text = "Asynchronous TCP Client";
    Size = new Size(400, 380);
    Label label1 = new Label();
    label1.Parent = this;
    label1.Text = "Enter text string:";
    label1.AutoSize = true;
    label1.Location = new Point(10, 30);
    newText = new TextBox();
    newText.Parent = this;
    newText.Size = new Size(200, 2 * Font.Height);
    newText.Location = new Point(10, 55);
    results = new ListBox();
    results.Parent = this;
    results.Location = new Point(10, 85);
    results.Size = new Size(360, 18 * Font.Height);
    Label label2 = new Label();
    label2.Parent = this;
    label2.Text = "Connection Status:";
    label2.AutoSize = true;
    label2.Location = new Point(10, 330);
    conStatus = new TextBox();
    conStatus.Parent = this;
    conStatus.Text = "Disconnected";
    conStatus.Size = new Size(200, 2 * Font.Height);
    conStatus.Location = new Point(110, 325);
    This document is created with the unregistered version of CHM2PDF Pilot
    Button sendit = new Button();
    sendit.Parent = this;
    sendit.Text = "Send";
    sendit.Location = new Point(220,52);
    sendit.Size = new Size(5 * Font.Height, 2 * Font.Height);
    sendit.Click += new EventHandler(ButtonSendOnClick);
    Button connect = new Button();
    connect.Parent = this;
    connect.Text = "Connect";
    connect.Location = new Point(295, 20);
    connect.Size = new Size(6 * Font.Height, 2 * Font.Height);
    connect.Click += new EventHandler(ButtonConnectOnClick);
    Button discon = new Button();
    discon.Parent = this;
    discon.Text = "Disconnect";
    discon.Location = new Point(295,52);
    discon.Size = new Size(6 * Font.Height, 2 * Font.Height);
    discon.Click += new EventHandler(ButtonDisconOnClick);
    }
    void ButtonConnectOnClick(object obj, EventArgs ea)
    {
    conStatus.Text = "Connecting...";
    Socket newsock = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);
    IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
    newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
    }
    void ButtonSendOnClick(object obj, EventArgs ea)
    {
    byte[] message = Encoding.ASCII.GetBytes(newText.Text);
    newText.Clear();
    client.BeginSend(message, 0, message.Length, SocketFlags.None,
    new AsyncCallback(SendData), client);
    }
    void ButtonDisconOnClick(object obj, EventArgs ea)
    {
    client.Close();
    conStatus.Text = "Disconnected";
    }
    void Connected(IAsyncResult iar)
    {
    client = (Socket)iar.AsyncState;
    try
    {
    client.EndConnect(iar);
    conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
    client.BeginReceive(data, 0, size, SocketFlags.None,
    new AsyncCallback(ReceiveData), client);
    } catch (SocketException)
    {
    conStatus.Text = "Error connecting";
    }
    }
    void ReceiveData(IAsyncResult iar)
    {
    Socket remote = (Socket)iar.AsyncState;
    int recv = remote.EndReceive(iar);
    string stringData = Encoding.ASCII.GetString(data, 0, recv);
    results.Items.Add(stringData);
    }
    void SendData(IAsyncResult iar)
    {
    Socket remote = (Socket)iar.AsyncState;
    int sent = remote.EndSend(iar);
    remote.BeginReceive(data, 0, size, SocketFlags.None,
    This document is created with the unregistered version of CHM2PDF Pilot
    new AsyncCallback(ReceiveData), remote);
    }
    public static void Main()
    {
    Application.Run(new AsyncTcpClient());
    }
    }
    
        4
  •  1
  •   Tim Robinson    14 年前

    客户机是否有可能两次连接到服务器(在不同的端口上,因为一个“随机”端口/未使用的端口被分配给连接),然后每个连接都有自己的流,允许同时发送两个文件?

    是的;这就是网络协议通常的工作方式。您不需要在服务器端选择新的端口号:即使您监听固定的端口号,到该端口的连接也保持独立。

    例如,www.stackoverflow.com上的web服务器总是监听端口80,但是您和我可以从我们的web浏览器进行连接,而且我们的连接不会混淆。

        5
  •  0
  •   Steve Townsend    14 年前

    您要么必须在单个套接字上叠加一个协议来标识哪些数据是哪个文件的一部分,要么使用多个套接字来知道每个文件的开始和结束位置。

    即使有多个套接字,如果要在没有套接字打开/关闭管理的情况下对新文件重用同一个套接字,也必须有开始/结束标记。

    为什么你不能用FTP?

    推荐文章