听力端口永久。如果文件在我的流上,请获取文件。如何?

时间:2010-05-08 14:13:07

标签: c# .net visual-studio network-programming sockets

我写了2个客户端和服务器程序。客户端发送文件也是服务器监听端口而不是获取文件。但是我需要我的服务器应用必须永久监听51124端口。如果我的流上有任何文件,请显示一个消息框“你的流上有一个文件”,然后显示我的savefile对话框。但我的服务器应用程序在“无限循环”。

1)每次听51124端口 2)我的流上有一个文件,给我看一个消息框。



   private void Form1_Load(object sender, EventArgs e)
        {

            TcpListener Dinle = new TcpListener(51124);
            try
            {

                Dinle.Start();

                Socket Baglanti = Dinle.AcceptSocket();
                if (!Baglanti.Connected)
                {
                    MessageBox.Show("No Connection!");
                }

                else
                {
                    while (true)
                    {
                        byte[] Dizi = new byte[250000];
                        Baglanti.Receive(Dizi, Dizi.Length, 0);

                        string Yol;

                        saveFileDialog1.Title = "Save File";
                        saveFileDialog1.ShowDialog();
                        Yol = saveFileDialog1.FileName;
                        FileStream Dosya = new FileStream(Yol, FileMode.Create);
                        Dosya.Write(Dizi, 0, Dizi.Length - 20);
                        Dosya.Close();
                        listBox1.Items.Add("dosya indirildi");
                        listBox1.Items.Add("Dosya Boyutu=" + Dizi.Length.ToString());
                        listBox1.Items.Add("İndirilme Tarihi=" + DateTime.Now);
                        listBox1.Items.Add("--------------------------------");
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

我的算法:

if(AnyFileonStream()==true) { GetFile()

//Also continue to listening 51124 port... }

我该怎么做?

2 个答案:

答案 0 :(得分:0)

在本教程中,他们打开端口,然后等待接受连接,而不是在没有连接时弹出消息框:

http://www.eggheadcafe.com/articles/20020323.asp

这是他们代码的精简版本 - 请考虑使用此布局:

Imports System.Net.Sockets
Imports System.Text
Class TCPSrv
    Shared Sub Main()
        ' Must listen on correct port- must be same as port client wants to connect on.
        Const portNumber As Integer = 51124
        Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
        Console.WriteLine("Waiting for connection...")
        Try
            'Accept the pending client connection and return 
            'a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            Console.WriteLine("Connection accepted.")
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            tcpClient.Close()
            tcpListener.Stop()
            Console.WriteLine("exit")
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine(e.ToString())
            Console.ReadLine()
        End Try
    End Sub
   End Class

答案 1 :(得分:0)

这似乎有点低水平。为什么不公开接受文件流对象的net.tcp WCF端点?