Question TcpClient communication

hankypanky

New member
Joined
Jan 24, 2013
Messages
1
Programming Experience
10+
Hello everyone!

I am currently writing a console application that should communicate with a digital display that will show different kind of messages.

I have used a software called 'Putty' to try this communication the manual way and it works really great. You put in an IP, a Port and then you simply type in your message and press enter. The message is displayed directly.

Now to the tricky part. I have tried to implement the same functionality using the TcpClient and NetworkStream objects in .Net, and I think that I have succeeded in some way, but not completely.

The problem that I have is that the messages doesn't always end up on the display directly; i have to send them a couple of times and suddenly the appear. As I understand it, it might have something to do
with the size of the package that I am sending. It might be to small, and due to that it isn't sent until it is large enough. But that is just a guess of course...

My solution at the moment is to send the package ten times in the same update, it always works... But that solution sucks.

Here is my code:

'<Diagnostics.DebuggerHidden()> _

Public Function SendMessage(Carriers As List(Of CarrierDB), Logs As List(Of LogDB)) As Boolean

Dim ret As Boolean

Try

If PrepareMessage(Carriers, Logs) Then

Dim MaxChars As Integer = 83 - 3

Dim StaticChars As Integer = 5 + 1


' StartString

Dim startString As String = ""

If Message = Config.DisplayStaticEnglish Then
'
startString = Chr(27) & "11A0"

'
Else
'
startString = Chr(27) & "11A5"
'
End If
' Message
If Message.Length > (MaxChars - StaticChars) Then
'
Message = Message.Substring(0, MaxChars - StaticChars) & "..."
'
End If
' SendString1
Dim sendString1 As String = startString & Message
' SendString2
Dim sendString2 As String = Chr(13) & Chr(10)

' Client
Client = New TcpClient
Client.Connect(IP, Port)
Client.NoDelay = True
Stream = Client.GetStream
' Write

If stream.CanWrite Then

'
Dim WriteBuffer1() As Byte = GetWriteBuffer(Stream, sendString1)
Dim WriteBuffer2() As Byte = GetWriteBuffer(Stream, sendString2)

For i As Integer = 0 To 0
'
Stream.Write(WriteBuffer1, 0, WriteBuffer1.Length)
Stream.Write(WriteBuffer2, 0, WriteBuffer2.Length)
'
Next
ret = True
'
End If
'
End If

Catch ex As Exception
'
ret = False
'
Finally
'
Client.Close()
Client = Nothing
Stream.Close()
Stream = Nothing
'
End Try
Return ret
'
End Function

Private Function GetWriteBuffer(stream As NetworkStream, sendString As String) As Byte()
'

Dim writeBuffer(sendString.Length - 1) As Byte
Try

If stream.CanWrite Then
Dim i As Integer = 0

For i = 0 To sendString.Length - 1
'
Dim c As Char = sendString.Chars(i)

Select Case c
Case "Å"

writeBuffer(i) = 93

Case "å"

writeBuffer(i) = 125

Case "Ä"

writeBuffer(i) = 91

Case "ä"

writeBuffer(i) = 123

Case "Ö"

writeBuffer(i) = 92

Case "ö"

writeBuffer(i) = 124

Case Else

writeBuffer(i) = Asc(c)

End Select
'
Next

End If
'
Catch ex As Exception
'
'
End Try

Return writeBuffer
'
End Function


 



Anys ideas?

Thanks!

/Henrik
 

Latest posts

Back
Top