TCP Client...

Chrisz

Newcomer
Joined
Oct 24, 2003
Messages
7
I need either help on this or a GREAT tutorial that actually works. The 'Adventures in Visual Basic.net' on microsoft TCP chat tutorial.. only seems to work on "localhost" as soon as we start to introduce IP's instead of localhost... it stops working.. it wont connect at all... so any help on that, great, or even better a VB.net 2003 TCP tutorial or something.
 
i'm pretty much retarted by the way..... but i didn't really understand that article at all either.. i'm trying to figure out how to simply do what i did with all my server/client app's in vb 6 with winsock.. send commands to server.. server looks at commands, calls corresponding sub and away she went.. but now .NET's communications deal looks so much more confusing.. i've been trying to understand this stuff for about 3 weeks now.. it's driving me ccccccrrrrrrazyyy...i'm essentially trying to figure out what your triyng to do ..

i'm not understanding a bit of this.. but i'm determined.. so if i have any luck i'll pass it along..

until then, i'm gonna keep trying

:)

--Val
 
This is a pair of TCP classes I came up with for making .Net sockets much easier to manage, it's event driven making it easy to manage incoming connections and includes the ability to see if a connection has been closed remotely as well as a DataArrival event
 

Attachments

This isn't my thread, but I'm gonna ask a question or two.

That helped a lot, it seemed easier for me to understand than everything else i've read. Thanks. :)

What does the 'dispose' and 'finalize' do? Why do I need to do that? I've just never heard of it and was wondering. I saw it a few times and thought I outta figure out what that does.

:) Thanks.

--Val
 
Dispose releases resources of a class that may be using other classes or Unmanaged resources(Win32 Handles and such). Finalize does the same thing but is only called when the class is collected by the Garbage Collector.

In the 2 classes I provided, the Dispose event shuts off the sub-TcpListener or TcpClient class

Additional Notes on the classes:
You need to call the Poll function in the Listener and Client classes at regular intervals for the events to be raised, there are properties that you can check instead though.
 
Calling code for socket.vb

Andre,

Would you have some vb.net code available that shows how to call your socket.vb code? I would very much appreciate any assistance. Thank you. (I am a new asp.net programmer, converting some vb winsock code.)

Lori
 
Here's a simple example of how to use the classes:
Visual Basic:
Dim Link As TCP.SocketTransfer

Public Shared Sub Main()
'Open a connection to this computer on port 90
Link = New TCP.SocketTransfer(System.Net.IPAddress.Parse("127.0.0.1"), 90)

Link.SendData("Requesting Data") 'Send the command
Do Until Link.DataAvaliable Or Link.ConnectionActive = False
Loop 'Loop until we recieve some data or the connection is closed

If Link.DataAvaliable Then 'If we got data before the connection closed
    Console.WriteLine(Link.RecieveData()) 'Display it to the user
    Console.Read()
End If

Link.Dispose() 'Close connection
End Sub

The Server side code would be:
Visual Basic:
Dim Listener As TCP.SocketListen
Dim Trans As TCP.SocketTransfer

Public Shared Sub Main()
Listener = New TCP.SocketListen(90)

Do 'Loop until there is a pending connection
Loop Until Listener.ConnectionsPending

Trans = Listener.AcceptPendingRequest() 'Get the request at the top of the que
Do Until Trans.DataAvaliable Or Trans.ConnectionActive = False
Loop
If Trans.DataAvaliable = True Then 'If we got some data
    If Trans.RecieveData() = "Requesting Data" Then 'If it's our custom command
       Trans.SendData("Hello") 'Send some text back
    End If
End If

Trans.Dispose() 'Close the connection to the client
Listener.Dispose() 'Stop Listening
End Sub
Note that in the above code it closes down after recieving 1 connection, you need to restructure the loops to keep it going but you'll need a method of telling it to shutdown

In Windows Forms applications, put a Timer on the form and add:
Visual Basic:
Private Sub Timer1_Tick(...) Handles Timer1.Tick
Listener.Poll()

Dim i As Int32
For i = 0 To OpenConnections.GetUpperBound(0)
     OpenConnections(i).Poll()
Next i
End Sub
Now you can use AddHandler or WithEvents to recieve events
Visual Basic:
Dim WithEvents Listener As TCP.SocketListen

Private Sub Listener_ConnectionsRequested() Handles Listener.ConnectionsRequested
'Accept the clients using Listener.AcceptPendingRequest
'You can handle the client events using AddHandler
End Sub

I added another function that makes sure you receive the required amount of data if you're expecting something no shorter/longer than the specified amount
 

Attachments

Back
Top