开发者

Listening Port Permanently. if file is on my stream, Get file. How to?

i writed 2 client and server program. client send file also server listen port and than get file.But i need My server App must listen on 51124 port permanently. if any file on my stream, show me a messagebox "there is a file on your stream" and than show me savefile dialog. But my server app in "Infinite loop".

1) listen 51124 port every time

2) do i have a file on my stream, show me a messagebox.



   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);
                  开发者_JAVA技巧      listBox1.Items.Add("--------------------------------");
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

My Algorithm:

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

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

How can i do that?


In this tutorial, they open the port and then it waits to accept a connection, instead of popping up a messagebox when there's not connction:

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

Here's a stripped down version of their code - consider using this layout instead:

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


That seems a bit low level. Why not expose a net.tcp WCF endpoint that accepts filestream objects?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜