.Net Sockets. Some Progress.........

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

spaXam
08-20-2004, 09:40 PM
erm... How fast is the ethernet? How far away are the two computers?

Also, thanks for the provided code. Heh, you just showed me how to use sockets in .NET.

-spaXam

spaXam
08-20-2004, 09:48 PM
though, is this to send files or just text?

I was wondering if you could show me some example code of sending text between two connected TCP clients, copying the text out of a text box ("txtMessage.Text") on execution of a button ("btnSend.Click"). All i really need to see is the portion that actually sends the text through the connection.

-spaXam

PrOpHeT
08-31-2004, 02:43 PM
Imports System.net
Imports System.Net.sockets
Imports System.text

Dim NewClient As New TcpClient
NewClient.Connect("127.0.0.1", 9999)
Dim networkStream As NetworkStream = NewClient.GetStream()
'This is the part you need I left the rest incase there were some mis-understood references
'********************************************************
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(MsgOut.Text)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'********************************************************
Dim bytes(NewClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(NewClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
MsgIn.Text = returndata


Where MsgOut is the textbox containing data to send and MsgIn is the textbox to recieve the data returned from the server in response.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum