I
Izaskun
Hello,
I'm trying to establish a connection between my PC an my PDA.
The PDA has a GPRS connection open and read data from the 14001 port.
The PC try to connect with the PDA but the PDA seems not to hear
anything.
The PDA keeps listening in the following sentence:
client = tcpListener.AcceptTcpClient() 'Always keep here waiting
Here you have my source code. Ay help will be appreciated.
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports OpenNETCF.Net.Bluetooth
Imports OpenNETCF.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Dim workerThread As System.Threading.Thread
workerThread = New Threading.Thread(AddressOf receiveLoop)
workerThread.Start()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub
Public Sub receiveLoop()
Dim strReceived As String
tcpListener = New System.Net.Sockets.TcpListener(14001)
tcpListener.Start()
strReceived = receiveMessage(MAX_MESSAGE_SIZE)
While True
If strReceived <> "" Then
Dim updateDelegate As New myDelegate(AddressOf
UpdateTextBox)
updateDelegate.Invoke(strReceived)
strReceived = receiveMessage(MAX_MESSAGE_SIZE)
End If
End While
End Sub
Private Function receiveMessage(ByVal BufferLen As Integer) As
String
Dim bytesRead As Integer = 0
Dim client As TcpClient = Nothing
Dim stream As System.IO.Stream = Nothing
Dim Buffer(MAX_MESSAGE_SIZE) As Byte
Dim str As String
Try
client = tcpListener.AcceptTcpClient() 'Always keep here
waiting
stream = client.GetStream()
bytesRead = stream.Read(Buffer, 0, BufferLen)
str = "->" +
System.Text.Encoding.Unicode.GetString(Buffer, 0, bytesRead)
Catch e As Exception
'dont display error if we are ending the listener
If True Then
MsgBox("Error listening to incoming message")
End If
Finally
If (Not stream Is Nothing) Then
stream.Close()
End If
If (Not client Is Nothing) Then
client.Close()
End If
End Try
Return str
End Function
Private Delegate Sub myDelegate(ByVal str As String)
Private Sub UpdateTextBox(ByVal str As String)
'---delegate to update the textbox control
Dim sMessagesArchive As String
sMessagesArchive += str
MsgBox(sMessagesArchive)
End Sub
End Class
I'm trying to establish a connection between my PC an my PDA.
The PDA has a GPRS connection open and read data from the 14001 port.
The PC try to connect with the PDA but the PDA seems not to hear
anything.
The PDA keeps listening in the following sentence:
client = tcpListener.AcceptTcpClient() 'Always keep here waiting
Here you have my source code. Ay help will be appreciated.
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports OpenNETCF.Net.Bluetooth
Imports OpenNETCF.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Dim workerThread As System.Threading.Thread
workerThread = New Threading.Thread(AddressOf receiveLoop)
workerThread.Start()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub
Public Sub receiveLoop()
Dim strReceived As String
tcpListener = New System.Net.Sockets.TcpListener(14001)
tcpListener.Start()
strReceived = receiveMessage(MAX_MESSAGE_SIZE)
While True
If strReceived <> "" Then
Dim updateDelegate As New myDelegate(AddressOf
UpdateTextBox)
updateDelegate.Invoke(strReceived)
strReceived = receiveMessage(MAX_MESSAGE_SIZE)
End If
End While
End Sub
Private Function receiveMessage(ByVal BufferLen As Integer) As
String
Dim bytesRead As Integer = 0
Dim client As TcpClient = Nothing
Dim stream As System.IO.Stream = Nothing
Dim Buffer(MAX_MESSAGE_SIZE) As Byte
Dim str As String
Try
client = tcpListener.AcceptTcpClient() 'Always keep here
waiting
stream = client.GetStream()
bytesRead = stream.Read(Buffer, 0, BufferLen)
str = "->" +
System.Text.Encoding.Unicode.GetString(Buffer, 0, bytesRead)
Catch e As Exception
'dont display error if we are ending the listener
If True Then
MsgBox("Error listening to incoming message")
End If
Finally
If (Not stream Is Nothing) Then
stream.Close()
End If
If (Not client Is Nothing) Then
client.Close()
End If
End Try
Return str
End Function
Private Delegate Sub myDelegate(ByVal str As String)
Private Sub UpdateTextBox(ByVal str As String)
'---delegate to update the textbox control
Dim sMessagesArchive As String
sMessagesArchive += str
MsgBox(sMessagesArchive)
End Sub
End Class