Anton-
08-11-2004, 11:38 AM
Hi,
Further to my last post I have discovered that my original program which used bits from the MSDN example does actually work! However, it takes 15 secs to initiate the tcp/ip connection which is why I missed it before.
I am now successfully transmitting and receiving using the .NET sockets namespace. I have stripped the relevant code from my app below.
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Dim tcpClient As New System.Net.Sockets.TcpClient
To connect:
Try
tcpClient.Connect(ethernetip.Text, 10001)
Catch
End Try
To Send:
Try
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() =Encoding.ASCII.GetBytes(Senddata + Chr(13) + Chr(10))
networkStream.Write(sendBytes, 0, sendBytes.Length)
End If
Catch
End Try
To Receive:
Try
If networkStream.CanRead Then
' Reads the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
' Read can return anything from 0 to numBytesToRead.
' This method blocks until at least one byte is read.
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
txtstatus.Text = (returndata)
End If
Catch
End Try
Does anyone know why it takes 15-18 secs to connect to the socket? Everything works fine after this point.
I have just discovered that if the connection is done via the Internet on my laptop using the analogue modem and I loop back through my DSL I can connect to my target device instantly!!!
To prove the problem is not with my Laptop I have run the VB windows application from another PC on my network with the same results.
Any help would be appreciated.
Regards,
Anton
Further to my last post I have discovered that my original program which used bits from the MSDN example does actually work! However, it takes 15 secs to initiate the tcp/ip connection which is why I missed it before.
I am now successfully transmitting and receiving using the .NET sockets namespace. I have stripped the relevant code from my app below.
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Dim tcpClient As New System.Net.Sockets.TcpClient
To connect:
Try
tcpClient.Connect(ethernetip.Text, 10001)
Catch
End Try
To Send:
Try
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() =Encoding.ASCII.GetBytes(Senddata + Chr(13) + Chr(10))
networkStream.Write(sendBytes, 0, sendBytes.Length)
End If
Catch
End Try
To Receive:
Try
If networkStream.CanRead Then
' Reads the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
' Read can return anything from 0 to numBytesToRead.
' This method blocks until at least one byte is read.
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
txtstatus.Text = (returndata)
End If
Catch
End Try
Does anyone know why it takes 15-18 secs to connect to the socket? Everything works fine after this point.
I have just discovered that if the connection is done via the Internet on my laptop using the analogue modem and I loop back through my DSL I can connect to my target device instantly!!!
To prove the problem is not with my Laptop I have run the VB windows application from another PC on my network with the same results.
Any help would be appreciated.
Regards,
Anton