Results 1 to 4 of 4

Thread: winsock with .NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    1

    winsock with .NET

    I have a friend that sent me a little chat program using winsock.
    So, I started working on a little program as well.
    His program is in VB 6, and I have VB.NET.

    I got the control to work with my .NET environment, but I have noticed that a few things are different in .NET, and have been giving me tons of problems.

    for example
    VB Code:
    1. Private Sub winsock_ConnectionRequest(ByVal requestID As Long)
    This is the VB6 ConnectionRequest sub.

    When I choose my winsock control, and find ConnectionRequest it creates this..
    VB Code:
    1. Private Sub winsock_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles winsock.ConnectionRequest

    I've seen winsock.Accept requestID all over the place, but this is an error, obviously because requestID isn't defined in the sub definition. So I tried to use sender. Also, when I do this, Visual Studio.NET changes the command to winsock.Accept(sender). I dont know if this is wrong or not, but it does not work.

    As of right now I'm sitting at a rather nasty error: "Exception has been thrown by the target of an invocation." This comes up from this code...

    VB Code:
    1. Try
    2.     winsock.Accept(sender)
    3. Catch ex As Exception
    4.     MessageBox.Show(ex.Message)
    5. End Try

    What am I doing wrong? or where can I find a .NET example of how to use winsock?

  2. #2
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: winsock with .NET

    First of all, people will tell you how bad it is to use OCX controls in .NET. They work, but it is considered a bad practice, since .NET plans to escape all that mess.

    However, Microsoft has not presented us with anything as useful as winsock in .NET, and only a few client-based classes exist that implement .NET sockets, and server apps are even fewer.

    Anyway, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent looks like what you need to use. Just so you know, Sender is a variable that the event passes which refers directly to your Winsock object. If you put winsock.accept(blah), sender.accept(blah) would do the same.

    I assume the "e" variable contains what you need. Just try typing in winsock.accept(e.) - when you type the period after the e, some methods/properties in e can be selected. I don't know how that works with it, but hopefully it will just be something like e.RequestID!

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: winsock with .NET

    Correct, there is no Windsock control in .NET. You need to re-write most of the app that your friend sent you. It will not convert
    correctly, at least 99.9% of the time. Your almost always better off re-writting the old VB6 apps in pure .NET

    Check out the System.Net.Sockets.NetworkStream namespace. There is a GetStream function that can use with the System.IO.StreamWritter
    object to read data from the incoming stream.

    Also, the System.Net.Sockets.TcpClient & Listener


    Ps, you might get a better response having the thread moved to VB.NET or post a link to this thread.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: winsock with .NET

    Here is a sample senddata procedure in .NET. No COM control needed.
    VB Code:
    1. Private client As TcpClient
    2.  
    3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     client = New TcpClient("localhost", PORT_NUM)
    5.     'Blah, blah, blah...
    6.     '...
    7. End Sub
    8.  
    9. 'Use a StreamWriter to send a message to server.
    10. Private Sub SendData(ByVal data As String)
    11.     Dim writer As New IO.StreamWriter(client.GetStream)
    12.     writer.Write(data & vbCr)
    13.     writer.Flush()
    14. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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