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...
}
我该怎么做?
答案 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端点?