CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2016
    Posts
    1

    VB.NET 2010 - MSWinsock - how to add event handler.

    Hello,

    I have some problem I cannot to solve.
    I am using MSWinsock library for TCPIP connection. It is very simple.

    What I have done:

    1. I created new project
    2. Added MSWinsock library
    3. Than I used drag-drop function to put component from ToolBox on Form.

    Now in source code on top menu I have many events I can choose. (See attached photo)
    Simple code is following.
    Code:
    Public Class Form1
     
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
     
            TCPIP.RemoteHost = "127.0.0.1"
            TCPIP.RemotePort = 9100
            TCPIP.Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol
            TCPIP.Connect()
     
        End Sub
     
        Private Sub TCPIP_DataArrival(sender As Object, e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles TCPIP.DataArrival
     
        End Sub
     
     
    End Class
    Name:  1.jpg
Views: 468
Size:  14.6 KB


    When I start program and launch any TCPIP server on port 9100 I have connection. When I send any message, TCPIP_DataArrival sub is running.


    But I would like to do this without drag-drop function.
    Just to do this only with source code.

    I have done it as following:

    Code:
    Imports MSWinsockLib
     
    Public Class Form1
     
        Public TCPIP As MSWinsockLib.Winsock
     
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
     
            TCPIP.RemoteHost = "127.0.0.1"
            TCPIP.RemotePort = 9100
            TCPIP.Protocol = ProtocolConstants.sckTCPProtocol
            TCPIP.Connect()
     
        End Sub
     
        Private Sub TCPIP_DataArrival(sender As Object, e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles TCPIP.DataArrival
     
        End Sub
     
     
     
    End Class
    Connection is working, but I don't know how to define DataArrival Event. I have error in syntax. See attached photo.
    It is just event like in many libraries, but I dont know how to programm it
    I was trying Public WithEvents TCPIP As MSWinsockLib.Winsock but without success.

    I was trying also to do this like this way.
    Code:
                TMR = New System.Timers.Timer
                TMR.Interval = 1000
                TMR.Stop()
                AddHandler TMR.Elapsed, AddressOf TMR_tick
    
    
    
        Private Sub TMR_tick(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs)
    
        End Sub
    Can somebody help me?
    Thanks in advance.
    Attached Images Attached Images   
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB.NET 2010 - MSWinsock - how to add event handler.

    I would advise not using the Winsock.
    Rather you should use either the Sockets class or the TCPClient or TCP Listener as needed.
    There are samples included with VB to show how each would be used in a project.

    Then again I am not totally sure about the Winsock lib you refer to. Maybe something has been added that I am not aware of in a later version.
    Last edited by DataMiser; September 26th, 2016 at 07:14 AM.
    Always use [code][/code] tags when posting code.

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

Featured