建立连接后立即收到客户端服务器文件传输,FIN ACK

时间:2017-07-11 11:01:17

标签: c# web-services sockets azure tcp

我很难搞清楚这一点,所以任何帮助都会非常感激。 我试图在Web服务器(MS Azure)和Windows Embedded Standard上运行的某些嵌入式系统之间实现客户端/服务器文件传输软件套件。在Web服务器端,我有客户端应用程序(Web APP + ASMX WebService),在每个嵌入式系统上,我都有一个服务器应用程序(Winforms app)监听端口8020,如下图所示。

system architecture diagram

我的本​​地测试环境包括我的dev pc(Windows 10,VS2015,192.168.1.2,托管Web APP和WebService)以及两个连接到本地LAN的机器人(Robot Client 1:192.168.1.80,Robot Client) 2:192.168.1.132)

在当地,一切都很完美。文件传输成功完成。但是,一旦我将Web应用程序和Web服务部署到MS Azure Web服务器,情况就不是这样了。我使用wireshark监控流量,看看究竟发生了什么。从附图中可以看出。安装在服务器上的客户端应用程序在建立连接后立即发送[FIN,ACK]?!

Wireshark capture

我在虚拟机上添加了相应的端点。我在本地路由器上也打开了端口8020,并且我在两个机器人的每个防火墙的同一端口上创建了一个入站规则。

关于代码, WebApplication:

private void TrasnferMAJ(string RobotsRef, string SelectedTime, string robotName, string Vmaj)
    {
        WebserviceRobots ws = new WebserviceRobots();
        ws.SendMAJCompleted += new SendMAJCompletedEventHandler(SendMAJCompleted);
        ws.SendMAJAsync(RobotsRef, "", robotName, Vmaj);   
        ws.SendMAJ(RobotsRef, "", robotName, Vmaj);

    }

     private void SendMAJCompleted(object sender, SendMAJCompletedEventArgs e)
    {

        string UpdateStatus = "";
        if (e != null)
            UpdateStatus = e.Result;
    }

网络服务:

 [WebMethod]
    public string SendMAJ(string RobotsRef, string SelectedTime, string robotName, string Vmaj)
    {
        int port = 8020;
        string message = "Envoi terminé avec succès"; // Transfer completed successfully 
        byte[] SendingBuffer = null;
        TcpClient client = null;
        NetworkStream netstream = null;
        try
        {

           client = new TcpClient(robotName, port); // initiate comm
           netstream = client.GetStream(); // Network stream to be used
            FileStream Fs = new FileStream(GetMajPath(Vmaj), FileMode.Open, FileAccess.Read);
            int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize))); // the number of packets to be sent 

            int TotalLength = (int)Fs.Length, CurrentPacketLength = 0; // initializing TotalLength to stream length (nbr of bytes)
            for (int i = 0; i < NoOfPackets; i++)
            {
                if (TotalLength > BufferSize) // if we still have more than one Kbytes
                {
                    CurrentPacketLength = BufferSize;
                    TotalLength = TotalLength - CurrentPacketLength; // totalLength : the amount of data that has not been sent yet
                }
                else
                CurrentPacketLength = TotalLength; // this here could be less than a byte. 
                SendingBuffer = new byte[CurrentPacketLength]; 
                Fs.Read(SendingBuffer, 0, CurrentPacketLength); // reading the data from the filestream, & writing it to the SendingBuffer
                netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length); // Writing the SendingBuffer Buffer to the network stream
            }
            Fs.Close();

        }
        catch (Exception ex)
        {
            message = ex.Message;
        }
        finally
        {
            netstream.Close();
            client.Close();
        }
        // if it all goes well, the message should be ="Envoi terminé avec succès",else it would be assigned the exception message.
        return message;
    }

服务器应用程序(WinForms):

public partial class ReceiveMAJs : Form
{
    private const int BufferSize = 1024;
    public string Status = string.Empty;
    public Thread T = null;
    public ReceiveMAJs()
    {
        InitializeComponent();
    }

    private void ReceiveMAJs_Load(object sender, EventArgs e)
    {
        label1.Text = "Serveur en cours d'exécution...";
        // Creating a seperate thread for recieving the MAJ file
        ThreadStart Ts = new ThreadStart(StartReceiving);
        T = new Thread(Ts);
        T.Start();

    }

    public void StartReceiving()
    {
        ReceiveTCP(8020);
    }
    public void ReceiveTCP(int portN)
    {
        TcpListener Listener = null;
        try
        {
            Listener = new TcpListener(IPAddress.Any, portN);
            Listener.Start();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        byte[] RecData = new byte[BufferSize];
        int RecBytes;

        for (;;)
        {
            TcpClient client = null;
            NetworkStream netstream = null;
            Status = string.Empty;
            try
            {
                string message = "Mettre à jour ";
                string caption = "Connexion entrante";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;


                if (Listener.Pending()) // true if there are any pending connections
                {
                    client = Listener.AcceptTcpClient();
                    netstream = client.GetStream();
                    Status = "Connecter au serveur des MAJs\n";
                    result = MessageBox.Show(message, caption, buttons);

                    if (result == DialogResult.Yes)
                    {

                         string SaveFileName = string.Empty;
                         SaveFileDialog DialogSave = new SaveFileDialog();
                         DialogSave.Filter = "Tous les fichiers (*.*)|*.*";
                         DialogSave.RestoreDirectory = true; 
                         DialogSave.Title = "L'emplacement de sauvegarde?";
                         DialogSave.InitialDirectory = @"C:/";

                        Invoke((Action)(() => {
                            if (DialogSave.ShowDialog() == DialogResult.OK)
                                SaveFileName = DialogSave.FileName;
                        }));

                        if (SaveFileName != string.Empty)
                        {
                        int totalrecbytes = 0;
                        FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
                        while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 0)
                        {
                            Fs.Write(RecData, 0, RecBytes);
                            totalrecbytes += RecBytes;
                        }
                        Fs.Close();
                        }
                        netstream.Close();
                        client.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Thread.Sleep(5); // avoiding overworking the processor
        }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        T.Abort();
        this.Close();
    }


}

感谢您的回答。

0 个答案:

没有答案