Friday, June 28, 2013

BeginReceive trouble...

I don't really understand what you wrote. But in my TCP listener, which is not Async, I set it to thread.sleep in order to allow the IP stack to release the logical port before I have it perform a Button Click to reinitialize the receiver. Which is an infinite loop of course.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim ipAddress As IPAddress = ipAddress.Parse(TextBox1.Text)
Dim portNumber As Integer = TextBox2.Text
Dim tcpListener As New TcpListener(ipAddress, portNumber)
tcpListener.Start()
RichTextBox1.AppendText("Waiting for connection..." & vbCrLf)
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
RichTextBox1.AppendText("Connection accepted." & vbCrLf)

Dim FileSizeAndName As NetworkStream = tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
FileSizeAndName.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim clientdata As String = Encoding.ASCII.GetString(bytes)

Dim Split() As String
Dim delimiterStr As String = "/"
Dim delimiter As Char() = delimiterStr.ToCharArray()
Split = clientdata.Split(delimiter, 3)

Dim File As NetworkStream = tcpClient.GetStream()
Dim FileLength(CInt(Split(0) - 1)) As Byte
File.Read(FileLength, 0, CInt(Split(0) - 1))
My.Computer.FileSystem.WriteAllBytes(TextBox3.Text & "\" & Split(1), FileLength, False)
Label6.Text = TextBox3.Text & "\" & Split(1)

Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
File.Write(sendBytes, 0, sendBytes.Length)

RichTextBox1.AppendText(vbCrLf & "Message Sent /> : " & responseString)
tcpClient.Close()
tcpListener.Stop()
RichTextBox1.AppendText("exit")
Catch ex As Exception
RichTextBox1.AppendText(ex.Message)
End Try
System.Threading.Thread.Sleep(200)
Button1.PerformClick()
End Sub





You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8



No comments:

Post a Comment