来自3台服务器的文件流

时间:2019-05-30 10:16:51

标签: c# filestream tcpclient tcpserver

我正在创建管理员gui,以从服务器中选择音乐或电影文件。所选文件将从此服务器下载到我的客户端文件夹。我该如何让3台服务器协同工作并共享自己的数据块?

我正在Visual Studio上使用C#Winforms进行此操作。我已经用1台服务器实现了它,并且正在运行。但是,当我尝试在不同端口上连接第二台或第三台服务器时,客户端程序冻结,并且没有字节传输。我曾尝试过多线程,但无法再次连接多台服务器。

客户

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CiDoMa
{
    public partial class Form1 : Form
    {
        string fileName, fileInitName;

        public Form1()
        {
            InitializeComponent();
        }

        private void Music_button_Click(object sender, EventArgs e)
        {

            fileInitName = fileName_textbox.Text;

            if (fileInitName.Contains(".mp3"))
            {
                fileName = "Music/" + fileInitName;
            }

            else if (fileInitName.Contains(".mp4"))
            {
                fileName = "Movie/" + fileInitName;
            }
            progressBar1.Value = 0;
            IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
            IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9060);
            IPEndPoint ipep3 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9070);

            Socket server0 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            server0.Connect(ipep1);
            byte[] data = new byte[1024];
            data = Encoding.ASCII.GetBytes(fileName);
            server0.Send(data, data.Length, SocketFlags.None);

            using (FileStream to = new FileStream("../../" + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    byte[] buffer = new byte[1024];
                    int rcv = 1;
                    while (rcv!=0)
                    {
                        rcv = server0.Receive(buffer, 1024, SocketFlags.None);
                        to.Write(buffer, 0, buffer.Length);
                    }
                    progressBar1.Value += 100;
                }

            server0.Shutdown(SocketShutdown.Both);
            server0.Close();
        }
    }
}

服务器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;


namespace CiDoMaServer
{
    public partial class Form1 : Form
    {
        string fileName = "";
        IPEndPoint ipep, ipep2, ipep3;
        int rcv;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Server_init_Click(object sender, EventArgs e)
        {
            ipep = new IPEndPoint(IPAddress.Any,
                9050);
            ipep2 = new IPEndPoint(IPAddress.Any,
                9060);
            ipep3 = new IPEndPoint(IPAddress.Any,
                9070);
            IPEndPoint[] ipPoints = new IPEndPoint[3] { ipep, ipep2, ipep3 };

            Socket[] scon = new Socket[3];
            Socket[] server = new Socket[3];

            for (int i = 0; i < scon.Length; i++)
            {
                scon[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                scon[i].Bind(ipPoints[i]);
                //scon[i].Listen(10);
                //server[i] = scon[i].Accept();
            }
            scon[0].Listen(10);
            server[0] = scon[0].Accept();
            byte[] data = new byte[1024];
            rcv = server[0].Receive(data, data.Length, SocketFlags.None);
            fileName = Encoding.ASCII.GetString(data);

            while (rcv != 0) {
            using (FileStream from = new FileStream("../../" + fileName, FileMode.Open, FileAccess.Read))
            {
                int readCount;
                byte[] buffer = new byte[1024];
                while ((readCount = from.Read(buffer, 0, 1024)) != 0)
                {
                    server[0].Send(buffer, 1024, SocketFlags.None);
                }
                }
            }
            server[0].Close();
            scon[0].Close();

            /*for(int i=0; i<scon.Length; i++) { 
                server[i].Close();
                scon[i].Close();
            }*/
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果要创建同时服务于多客户端的服务器,则服务器应支持异步和多线程。而且处理多线程并不容易,因此我认为这是您在多线程操作中失败的原因,但是您可以尝试使其变得更好

关于示例,您可以看一下此example source code

asynchronous socket

希望对您有帮助