|
-
Nov 14th, 2010, 05:49 PM
#1
Thread Starter
New Member
Newbie
Hi All,
Well after years of VB today I have moved to .NET and yes I have questions already 
OK here is a code snippet
Code:
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 = 29999
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.")
Do While tcpClient.Connected = True
' 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))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(clientdata)
Dim responseString As String = "OK"
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(responseString)
'Any communication with the remote client using the TcpClient can go here.
'Close TcpListener and TcpClient.
Loop
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
when it accepts some data (text) it prints it in the console window but has a load of white spaces after it and before the "OK" I have tried trimming but that did not work, I tried moving into another temp string after trim again that did not work! any ideas?
P.S. code is not mine nicked from WWW
-
Nov 14th, 2010, 06:48 PM
#2
Re: Newbie
Code:
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
That's your problem there. You're getting a String based on the entire Byte array, but the data read may not have filled the entire array. The Read method returns a number that indicates the number of Bytes read, and the Encoding.GetString method allows you to specify the number of Bytes to use to generate the String. That code should be something like this:
Code:
Dim byteCount As Integer = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes, 0, byteCount)
Note that Trim will, by default, remove whitespace from a String. Your String would have contained null characters, which are not considered whitespace.
-
Nov 14th, 2010, 07:00 PM
#3
Thread Starter
New Member
Re: Newbie
Thanks jmcilhinney works a treat now, will take a bit of getting used to all these new objects!
Steve
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|