.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress......... .Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
Go Back  Xtreme Visual Basic Talk > > > .Net Sockets. Some Progress.........


Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2004, 12:38 PM
Anton- Anton- is offline
Newcomer
 
Join Date: Jul 2004
Posts: 10
Default .Net Sockets. Some Progress.........


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
Reply With Quote
  #2  
Old 08-20-2004, 10:40 PM
spaXam spaXam is offline
Newcomer
 
Join Date: Aug 2004
Posts: 9
Default

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
Reply With Quote
  #3  
Old 08-20-2004, 10:48 PM
spaXam spaXam is offline
Newcomer
 
Join Date: Aug 2004
Posts: 9
Default

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
Reply With Quote
  #4  
Old 08-31-2004, 03:43 PM
PrOpHeT's Avatar
PrOpHeT.Net Sockets.  Some Progress......... PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Code:
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.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...

Last edited by PrOpHeT; 08-31-2004 at 04:39 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress......... .Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress......... .Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
 
.Net Sockets.  Some Progress.........
.Net Sockets.  Some Progress.........
 
-->