ModSci
Feb 18th, 2010, 05:28 PM
Greetings,
Below is an attempt at a TCP Client. I'm able to send data just fine but not receive it. I call ReadSocket from a new thread but no matter when I do I cannot get data out of it.
The only time it works is when I uncomment the following two lines:
'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("ALIVE")
'stream.Write(sendBytes, 0, sendBytes.Length)
But I don't really want it sending a message just to read a stream.
What am I doing wrong?
Thanks in advance!
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class TCPIPClient
Dim tcpclnt As TcpClient
Public readBuffer() As Byte
Public writeBuffer() As Byte
Private stream As NetworkStream
Public Event ReceivedData(ByVal Data As String)
Public Sub startServer(ByVal ipAddress As String, ByVal portNumber As Integer)
'Me.loginName = loginName
tcpclnt = New TcpClient()
tcpclnt.Connect(ipAddress.Trim(), portNumber)
stream = tcpclnt.GetStream()
End Sub 'startServer
Public Sub ReadSocket()
While True
Try
'readBuffer = New [Byte](100) {}
'stream.Read(readBuffer, 0, 100)
' RaiseEvent ReceivedData(System.Text.Encoding.ASCII.GetString(readBuffer))
'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("ALIVE")
'stream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcpclnt.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(tcpclnt.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
RaiseEvent ReceivedData(returndata)
Catch e As Exception
Exit While
End Try
End While
End Sub 'ReadSocket
Public Sub writeToServer(ByVal strn As String)
Dim encord As New System.Text.ASCIIEncoding()
writeBuffer = encord.GetBytes(strn)
If Not (stream Is Nothing) Then
stream.Write(writeBuffer, 0, writeBuffer.Length)
End If
End Sub 'writeToServer
End Class
Below is an attempt at a TCP Client. I'm able to send data just fine but not receive it. I call ReadSocket from a new thread but no matter when I do I cannot get data out of it.
The only time it works is when I uncomment the following two lines:
'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("ALIVE")
'stream.Write(sendBytes, 0, sendBytes.Length)
But I don't really want it sending a message just to read a stream.
What am I doing wrong?
Thanks in advance!
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class TCPIPClient
Dim tcpclnt As TcpClient
Public readBuffer() As Byte
Public writeBuffer() As Byte
Private stream As NetworkStream
Public Event ReceivedData(ByVal Data As String)
Public Sub startServer(ByVal ipAddress As String, ByVal portNumber As Integer)
'Me.loginName = loginName
tcpclnt = New TcpClient()
tcpclnt.Connect(ipAddress.Trim(), portNumber)
stream = tcpclnt.GetStream()
End Sub 'startServer
Public Sub ReadSocket()
While True
Try
'readBuffer = New [Byte](100) {}
'stream.Read(readBuffer, 0, 100)
' RaiseEvent ReceivedData(System.Text.Encoding.ASCII.GetString(readBuffer))
'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("ALIVE")
'stream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcpclnt.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(tcpclnt.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
RaiseEvent ReceivedData(returndata)
Catch e As Exception
Exit While
End Try
End While
End Sub 'ReadSocket
Public Sub writeToServer(ByVal strn As String)
Dim encord As New System.Text.ASCIIEncoding()
writeBuffer = encord.GetBytes(strn)
If Not (stream Is Nothing) Then
stream.Write(writeBuffer, 0, writeBuffer.Length)
End If
End Sub 'writeToServer
End Class