Results 1 to 37 of 37

Thread: Socket example

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176

    Socket example

    Could someone make me a very simple prog that listens to port 2500, waits for incomming data and puts this data into a messagebox?? I know about the examples that are included with visual studio.net but i dont really get them.

    Thnx

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ok I make no guarantee that this will work..it is the TCP socket class I am wroking on...but it SHOULD work as is......

    First off..here is the class

    Code:
    Imports System
    Imports System.Net.Sockets
    Imports System.Text
    
    Namespace Sockets
    	' All socket connection types
    	Namespace TCP
    		' For TCP comm
    		Public Class TCPServer
    			' For TCP servers
    			Private m_Port As Integer
    			Private tcpConn As TcpClient
    			Private m_gID As Guid = Guid.NewGuid
    			Private mobjClient As TcpClient
    			Private marData(1024) As Byte
    			Private m_ClientIP As String
    			Private m_State As Short
    			
    			Public Event ConnectionRequest()
    			Public Event Connected(ByVal sender As TCPServer)
    			Public Event Disconnected(ByVal sender As TCPServer)
    			Public Event DataReceived(ByVal sender As TCPServer, ByVal Data As String)
    
    			Public Sub New(ByVal client As TcpClient)
        				' A new instance of the class started meaning a new connection
    				mobjClient = client
        				RaiseEvent Connected(Me)
        				mobjClient.GetStream.BeginRead(marData, 0, 1024, _
          				AddressOf Receive, Nothing)
      			End Sub
    
    
    			Public Sub SendData(ByVal Data As String)
    				' Server sending to client. String Data
    				SyncLock mobjClient.GetStream
            				Dim w As New IO.StreamWriter(mobjClient.GetStream)
            				w.Write(Data)
            				w.Flush()
          				End SyncLock
    			End Sub
    			
    			Priate Sub Receive()
    				' Start the data receiving process
    			    	Dim intCount As Integer
    
    			    	Try
    					SyncLock mobjClient.GetStream
    						intCount = mobjClient.GetStream.EndRead(ar)
    					End SyncLock
    					If intCount < 1 Then
    						RaiseEvent Disconnected(Me)
    						Exit Sub
    					End If
    
    					BuildString(marData, 0, intCount)
    					SyncLock mobjClient.GetStream
    						mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)
    					End SyncLock
    				Catch e As Exception
    					RaiseEvent Disconnected(Me)
        				End Try
    			End Sub
    			
    			Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
    			    	Dim intIndex As Integer
    
    			    	For intIndex = offset To offset + count - 1
    			      		If Bytes(intIndex) = 13 Then
    						RaiseEvent DataReceived(Me, mobjText.ToString)
    						mobjText = New StringBuilder()
    			      		Else
    						mobjText.Append(ChrW(Bytes(intIndex)))
    			      		End If
    			    	Next
      			End Sub
    
    			Public ReadOnly Property requestID() As String
        				' A connections requestID to identify it.
    				Get
          					Return m_gID.ToString
        				End Get
      			End Property
    			
    			Public Property Port() As Integer
    				' Port property
            			Get
                				Return m_Port
            			End Get
            			Set(ByVal Value As Integer)
                				m_Port = Value
            			End Set
    			End Property
    			
    			Public ReadOnly Property ClientIP() As String
    				' The ip address of the connected client
    				Get
    					Return m_ClientIP
    				End Get
    			End Sub
    			
    			Public ReadOnly Property State() As Short
    				' The ip address of the connected client
    				Get
    					Return m_State
    				End Get
    			End Sub
    		End Class
    	End Namespace
    End Namespace
    now give me a minute or 2 to type up the usage......
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    from form or whatever

    Code:
    ' The imports
    Imports System.Net
    Imports System.Net.Sockets
    Imports Sockets.TCP

    Code:
    Private mobjListener As TcpListener
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       DoListen()
    End Sub
    
    Private Sub DoListen()
        Try
          mobjListener = New TcpListener(2500)
    
          mobjListener.Start()
          Do
            Dim x As New TCPServer(mobjListener.AcceptTcpClient)
            AddHandler x.DataReceived, AddressOf OnLineReceived
          Loop Until False
        Catch
        End Try
    End Sub
    
    Private Sub OnDataReceived(ByVal sender As TCPServer, ByVal Data As String)
       MessageBox.Show(Data)
    End Sub


    That SHOULD work...I hope..it would be nice if it does.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    there seem to be some mistakes in the code, could u put them into a project, zip it and attach it to this post? i would be very thankfull if you did.

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    what you see is all there is.....Like I said..it has not been tested yet, but I will be as soon as I can.


    It was written in notepad so it could be just a bunch of minor things.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    yeah i fixed the minor mistakes, were just some spelling mistakes.

    The last things that needs fixing is this:

    mobjText is not declared, what should i declare it as?

    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)

    Receive aint OK according to VB...

    thnx
    Last edited by JpEgy; Mar 13th, 2002 at 04:20 PM.

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    oopps..

    Private mobjText As New StringBuilder()

    put that with the other declarations in the class
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ill have to look into why AddressOf Receive is not ok.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ahhh change the REceive delcaration to


    Private Sub Receive(ByVal ar As IAsyncResult)
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    ok thnx, just 1 last question, were do i need to place the namespace? i just put it in a .vb file and it doesn't seem to work cause the main form says that namespace for TCP can't be found.

  11. #11
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    try compilling the namespace/class to a dll then add a ref to it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    OK, i compiled the DLL, included it into thw project and Imported it. Now i found 1 more mistake in the form code

    Do
    Dim x As New TCPServer(mobjListener.AcceptTcpClient)
    AddHandler x.DataReceived, AddressOf OnLineReceived
    Loop Until False

    OnLineReceived is not declared.


    For the rest i found no mistakes, thnx, this helped me very much :-)

  13. #13
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    that should be OnDataReceived..I thought I had changed that when I posted the class..I guess I didnt

    stupid me......


    Please let it work now!!!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    hmmm it works once, after that u have to restart in order to use it again, otherwise the window doesn;t come up. I think that has to to with the fact that the socket isn't unbind at close or something....

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well it partially working makes me very happy....hmmm let me think...
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ahh I have an idea..in the form code...put that DoListen into another Thread


    Add this Imports

    Imports System.Threading

    This variable declaration:
    Private ListenThread As Thread

    and isntead of just DoListen()

    do this:
    Code:
    ListenThread = New Thread(AddressOf DoListen)
    ListenThread.Start()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  17. #17
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Can you post the class as you have it now so I can record the changes and spelling mistakes you fixed? I will give you credit for the finished product for basically being an alpha tester for it!

    I am about to get .NET reinstalled here at work so I can actually test my own component....
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    ok. BTW, it does allow a client to connect.

    Imports System.Net.Sockets
    Imports System.Text

    Namespace Sockets
    ' All socket connection types
    Namespace TCP
    ' For TCP comm
    Public Class TCPServer
    ' For TCP servers
    Private m_Port As Integer
    Private tcpConn As TcpClient
    Private m_gID As Guid = Guid.NewGuid
    Private mobjClient As TcpClient
    Private marData(1024) As Byte
    Private m_ClientIP As String
    Private m_State As Short
    Private mobjText As New StringBuilder()

    Public Event ConnectionRequest()
    Public Event Connected(ByVal sender As TCPServer)
    Public Event Disconnected(ByVal sender As TCPServer)
    Public Event DataReceived(ByVal sender As TCPServer, ByVal Data As String)

    Public Sub New(ByVal client As TcpClient)
    ' A new instance of the class started meaning a new connection
    mobjClient = client
    RaiseEvent Connected(Me)
    mobjClient.GetStream.BeginRead(marData, 0, 1024, _
    AddressOf Receive, Nothing)
    End Sub


    Public Sub SendData(ByVal Data As String)
    ' Server sending to client. String Data
    SyncLock mobjClient.GetStream
    Dim w As New IO.StreamWriter(mobjClient.GetStream)
    w.Write(Data)
    w.Flush()
    End SyncLock
    End Sub

    Private Sub Receive(ByVal ar As IAsyncResult)
    ' Start the data receiving process
    Dim intCount As Integer

    Try
    SyncLock mobjClient.GetStream
    intCount = mobjClient.GetStream.EndRead(ar)
    End SyncLock
    If intCount < 1 Then
    RaiseEvent Disconnected(Me)
    Exit Sub
    End If

    BuildString(marData, 0, intCount)
    SyncLock mobjClient.GetStream
    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)
    End SyncLock
    Catch e As Exception
    RaiseEvent Disconnected(Me)
    End Try
    End Sub

    Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
    Dim intIndex As Integer

    For intIndex = offset To offset + count - 1
    If Bytes(intIndex) = 13 Then
    RaiseEvent DataReceived(Me, mobjText.ToString)
    mobjText = New StringBuilder()
    Else
    mobjText.Append(ChrW(Bytes(intIndex)))
    End If
    Next
    End Sub

    Public ReadOnly Property requestID() As String
    ' A connections requestID to identify it.
    Get
    Return m_gID.ToString
    End Get
    End Property

    Public Property Port() As Integer
    ' Port property
    Get
    Return m_Port
    End Get
    Set(ByVal Value As Integer)
    m_Port = Value
    End Set
    End Property

    Public ReadOnly Property ClientIP() As String
    ' The ip address of the connected client
    Get
    Return m_ClientIP
    End Get
    End Property

    Public ReadOnly Property State() As Short
    ' The ip address of the connected client
    Get
    Return m_State
    End Get
    End Property
    End Class
    End Namespace
    End Namespace

  19. #19
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    arrghh ...couldnt use the [ code ] tags could ya?



    jk
    Thanks.

    Did the threading make any difference?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    they have any use? because they are made for VB6 and not for .net...

  21. #21
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    The indentation....makes it easier to look at...its ok I got it straight now though.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    when u got it finished could i have it :-> ?

  23. #23
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    here are the changes I have made.
    Added a Connect method for use as a client
    Added an Error event
    Added State property
    changed the class name to TCPConnection

    make sure you cahnge TCPServer to TCPConnection in your form code

    Code:
    ' Thanks goes out to my hommies at www.vbforums.com!
    ' Special Thanks To:
    ' JpEgy for fixing many minor mistakes and being the first tester.
    
    Imports System
    Imports System.Net.Sockets 
    Imports System.Text 
    
    Namespace Sockets 
    	' All socket connection types 
    	Namespace TCP 		
    		Public Class TCPConnection
    			' For TCP connections intercepted by the TcpListener 
    			Private tcpConn As TcpClient 
    			Private m_requestID As Guid = Guid.NewGuid 
    			Private objClient As TcpClient 
    			Private arData(1024) As Byte 
    			Private m_ClientIP As String 
    			Private m_State As Short 
    			Private objText As New StringBuilder() 
    			
    			Public Event Connected(ByVal sender As TCPConnection) 
    			Public Event Disconnected(ByVal sender As TCPConnection) 
    			Public Event DataReceived(ByVal sender As TCPConnection, ByVal Data As String, ByVal BytesReceived As Long) 
    			Public Event Error(ByVal strMessage As String)
    			
    			Public Sub New(ByVal client As TcpClient) 
    				' A new instance of the class started meaning a new connection 
    				objClient = client 
    				RaiseEvent Connected(Me)
    				m_State = 1
    				objClient.GetStream.BeginRead(arData, 0, 1024, _ 
    					AddressOf Receive, Nothing) 
    			End Sub 
    
    			Public Sub SendData(ByVal Data As String) 
    				' Server sending to client. String Data 
    				m_State = 3
    				SyncLock objClient.GetStream 
    					Dim w As New IO.StreamWriter(objClient.GetStream) 
    					w.Write(Data) 
    					w.Flush() 
    				End SyncLock 
    				m_State = 1
    			End Sub 
    
    			Private Sub Receive(ByVal ar As IAsyncResult) 
    				' Start the data receiving process 
    				Dim intCount As Integer 
    
    				Try 
    					SyncLock objClient.GetStream 
    						intCount = objClient.GetStream.EndRead(ar) 
    					End SyncLock 
    					If intCount < 1 Then 
    						RaiseEvent Disconnected(Me)
    						m_State = 0
    						Exit Sub 
    					End If 
    
    					BuildString(arData, 0, intCount) 
    					SyncLock objClient.GetStream 
    						objClient.GetStream.BeginRead(arData, 0, 1024, AddressOf Receive, Nothing) 
    					End SyncLock 
    				Catch e As Exception 
    					RaiseEvent Disconnected(Me) 
    					m_State = 0
    				End Try 
    			End Sub 
    
    			Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) 
    				Dim intIndex As Integer 
    				
    				m_State = 2
    				For intIndex = offset To offset + count - 1 
    					If Bytes(intIndex) = 13 Then 
    						RaiseEvent DataReceived(Me, objText.ToString. objText.Length) 
    						objText = New StringBuilder() 
    					Else 
    						objText.Append(ChrW(Bytes(intIndex))) 
    					End If 
    				Next 
    				m_State = 1
    			End Sub 
    
    			Public ReadOnly Property requestID() As String 
    				' A connections requestID to identify it. 
    				Get 
    					Return m_gID.ToString 
    				End Get 
    			End Property 
    
    			Public ReadOnly Property ClientIP() As String 
    				' The ip address of the connected client 
    				Get
    					Return m_ClientIP 
    				End Get 
    			End Property 
    
    			Public ReadOnly Property State() As Short 
    				' The ip address of the connected client 
    				' Possible values:
    				' 0 - Disconnected
    				' 1 - Connected And Ready
    				' 2 - Receiving
    				' 3 - Sending
    				Get 
    					Return m_State 
    				End Get 
    			End Property
    			
    			Public Sub Connect(ByVal strDomain As String, ByVal intPort As Integer)
    			        Try
    			            objClient = New TcpClient(strDomain, intPort)
    			            RaiseEvent Connected(Me)
    			
    			            objClient.ReceiveBufferSize = 1024
    			            objClient.NoDelay = True
    			        Catch e As Exception
    			            RaiseEvent Error(e.Message.ToString)
    			        End Try
        			End Sub
    		End Class 
    	End Namespace 
    End Namespace
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    Public Event Error(ByVal strMessage As String)

    was not allowed, i changed it into

    Public Event Errorl(ByVal strMessage As String)

    and changed

    RaiseEvent Errorl(e.Message.ToString)

    into

    RaiseEvent Errorl(e.Message.ToString)

    just 1 thing left that i can't get done :-|,

    For intIndex = offset To offset + count - 1
    If Bytes(intIndex) = 13 Then
    RaiseEvent DataReceived(Me, objText.ToString.objText.Length)
    objText = New StringBuilder()
    Else
    objText.Append(ChrW(Bytes(intIndex)))
    End If
    Next


    it says that objText aint a member of string....

    Could u fix this plz?? thnx

  25. #25
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Me, objText.ToString.objText.Length)

    oops should be a comma between ToString and the second objText

    objText.Length should return the BytesRecieved in the Event
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    OK, last thing (i hope :-))

    m_gID is not declared

  27. #27
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    change m_gID to m_requestID

    I just changed some variable names to reflict better what they where for
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    OK, here's an update again :-)

    Do
    Dim x As New TCPConnection(mobjListener.AcceptTcpClient)
    AddHandler x.DataReceived, AddressOf OnDataReceived
    Loop Until False


    OnDataReceived doesnt have the same signature as the sub itself, that's (in short) what the IDE tells me....

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    could u plz fix this i wanna go on with my prog :->...

  30. #30
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    check that the parameters for you OnDataRecceived sub coincides with the DataReceived event...Remember we added that thrid parameter for BytesReceived....

    so I assume from the error, you Sub in the main form only still has the 2 orginal parameters... Add ByVal BytesReceived As Long for the third one.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    doesnt work :-( it doesn't bring up the window.... i recompiled the DLL and i restarted the IDE and stuff but it doesn;t give me a window...

  32. #32
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well try some debugging with breakpoints and all that and see where it goes and what it does...Unfortunatly that is the best I can offer until I can get to a point to be able to intall .NET here so I can start testing...
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    then i will wait for u :-)

    the error has to be somewhere in the DLL so it's hard to debug...

  34. #34
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    i think now that i think about it, if you just add the class to the project and dont put in that imports I mentioned before, it should work then., then it can be debugged....Ill try to get .NET installed soon to test all this.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  35. #35
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    ahem...

    what happened to winsock? heh... i liked my winsock control.. kept things nice n simple..

    i'm assuming you guys are working on some kind of wrapper for this code?

  36. #36
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ok made some changes to the class and tested myself..it works


    Code:
    ' Thanks goes out to my hommies at www.vbforums.com!
    ' Special Thanks To:
    ' JpEgy for fixing many minor mistakes and being the first tester.
    
    Imports System
    Imports System.Net.Sockets 
    Imports System.Text 
    Imports Microsoft.VisualBasic
    
    Namespace Sockets 
    	' All socket connection types 
    	Namespace TCP 		
    		Public Class TCPConnection
    			' For TCP connections intercepted by the TcpListener 
    			Private tcpConn As TcpClient 
    			Private m_requestID As Guid = Guid.NewGuid 
    			Private objClient As TcpClient 
    			Private arData(1024) As Byte 
    			Private m_ClientIP As String 
    			Private m_State As Short 
    			Private objText As New StringBuilder() 
    			
    			Public Event Connected(ByVal sender As TCPConnection) 
    			Public Event Disconnected(ByVal sender As TCPConnection) 
    			Public Event DataReceived(ByVal sender As TCPConnection, ByVal Data As String, ByVal BytesReceived As Long) 
    			Public Event SockError(ByVal strMessage As String)
    			
    			Public Sub New(ByVal client As TcpClient) 
    				' A new instance of the class started meaning a new connection 
    				objClient = client 
    				RaiseEvent Connected(Me)
    				Console.Writeline("Test")
    				m_State = 1
    				objClient.GetStream.BeginRead(arData, 0, 1024, _ 
    					AddressOf Receive, Nothing) 
    			End Sub
    
    			Public Sub SendData(ByVal Data As String) 
    				' Server sending to client. String Data 
    				m_State = 3
    				SyncLock objClient.GetStream 
    					Dim w As New IO.StreamWriter(objClient.GetStream) 
    					w.Write(Data) 
    					w.Flush() 
    				End SyncLock 
    				m_State = 1
    			End Sub 
    			
    			Private Sub Receive(ByVal ar As IAsyncResult) 
    				' Start the data receiving process 
    				Dim intCount As Integer 
    				Console.Writeline("Received")
    				Try 
    					SyncLock objClient.GetStream 
    						intCount = objClient.GetStream.EndRead(ar) 
    					End SyncLock 
    					If intCount < 1 Then 
    						RaiseEvent Disconnected(Me)
    						m_State = 0
    						Exit Sub 
    					End If 
    
    					BuildString(arData, 0, intCount) 
    					SyncLock objClient.GetStream 
    						objClient.GetStream.BeginRead(arData, 0, 1024, AddressOf Receive, Nothing) 
    					End SyncLock 
    				Catch e As Exception
    					RaiseEvent SockError(e.Message.ToString)
    					RaiseEvent Disconnected(Me) 
    					m_State = 0
    				End Try 
    			End Sub 
    
    			Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) 
    				Dim intIndex As Integer 
    				Dim txt As String
    				Console.Writeline("BuildString")
    				m_State = 2
    				
    				objText = New StringBuilder() 
    				For intIndex = offset To offset + count - 1 
    					txt = ChrW(Bytes(intIndex))
    					objText.Append(txt)
    				Next 
    				RaiseEvent DataReceived(Me, objText.ToString, objText.Length)
    				m_State = 1
    			End Sub 
    
    			Public ReadOnly Property requestID() As String 
    				' A connections requestID to identify it. 
    				Get 
    					Return m_requestID.ToString 
    				End Get 
    			End Property 
    
    			Public ReadOnly Property ClientIP() As String 
    				' The ip address of the connected client 
    				Get
    					Return m_ClientIP 
    				End Get 
    			End Property 
    
    			Public ReadOnly Property State() As Short 
    				' The ip address of the connected client 
    				' Possible values:
    				' 0 - Disconnected
    				' 1 - Connected And Ready
    				' 2 - Receiving
    				' 3 - Sending
    				' 4 - Connecting
    				Get 
    					Return m_State 
    				End Get 
    			End Property
    			
    			Public Sub Connect(ByVal strDomain As String, ByVal intPort As Integer)
    			        m_State = 4
    				Try
    			            	objClient = New TcpClient(strDomain, intPort)
    			            	RaiseEvent Connected(Me)
    				    	m_State = 1
    			            	objClient.ReceiveBufferSize = 1024
    			            	objClient.NoDelay = True
    			        Catch e As Exception
    			            	RaiseEvent SockError(e.Message.ToString)
    				    	m_State = 0
    			        End Try
        			End Sub
    		End Class 
    	End Namespace 
    End Namespace
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  37. #37
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    there are some console.writelines in the class I used for debugging..remove them if you wish..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width