Results 1 to 13 of 13

Thread: VB2010: How to spawn types and sockets?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    VB2010: How to spawn types and sockets?

    Before anything else I did try to use the search option, but it continuously failed to load.

    I'm new to VB.NET, from experienced in VB6. Most I have figured out but I am unsure of how to spawn Types and Sockets.

    Example:
    VB6 -
    Code:
    Dim TypeArray() As ThisType
    Type ThisType
      Bla As String
    End Type
    And for winsock...
    Code:
    Load Winsock(Index)
    well that was easy! But it has me stumped in vb.net, I hear using system.net.socket is the most efficient way to use sockets so I would like to spawn those.

    I need to be able to control which index is assigned to which socket, as each index will refer to certain variables.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB2010: How to spawn types and sockets?

    You might create sockets, but they are generally used as base classes or members for other derived classes in .NET. Connecting with raw sockets doesn't happen all that much. More likely you can use TCPClient and TCPListener, or UDPClient and UDPListener, though people don't use UDP nearly as often as TCP for connections.

    As for the index, I'm not quite sure what that is from your question. If it is an index into the array of types you created in the first snippet, then there may well be better options, though it is hard to be more specific without knowing more about what you are doing. If you intend that certain information be tied to the connection, you might create a class that includes the connection (either a socket or, more likely, a TCPClient instance), and have a List of that class. At the very least, take a look at List (of T), which is likely to be much superior to arrays in the scenario you have somewhat described. Arrays are good if you know the total and exact number of objects right from the start. List is better if the size of the collection can change over time.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: VB2010: How to spawn types and sockets?

    I am designing a proxy scanner. So I need to find the most efficient and fastest way to connect to a proxy, and send a few strings of data.

    Each connection will have to hold data such as, proxy port, http/ssl/socks ect...verification status.. stuff like that. The information will get dumped/replaced on unload or upon making another connection.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB2010: How to spawn types and sockets?

    User Defined Types in .Net consist of either classes or Structures, there is no Type construct in VB.Net as there is in VB6 and earlier versions.
    Code:
    Private Structure MyType
       member1 As Integer
       member2 As String
       '.... and so on
    End Structure
    The difference between a structure and an UDT in earlier versions is that a structure is much more like a class, it can include methods and properties and even events. However a structure is a value type while a class is an instance type.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: VB2010: How to spawn types and sockets?

    Thank you for the information, and sorry for the late reply; I have been a bit busy with school work. Here is what I came up with, but I question it's accuracy...rather if it's even close at all. (I did try to search and search for guides before posting. I eventually would want to spawn the Proxy Structure and probably multithread each connection.

    Code:
    Imports System.Net.Sockets
    
    Module Module1
        Public Structure Proxy
            Server As String
            Port As Long
            Connected As Boolean
            Verified As Boolean
            theSocket as Socket
        End Structure
    
    End Module
    
    Public Shared Sub ConnectProxy(ByRef Index As Integer)
    
        With Proxy
            .Server = "127.0.0.1"
            .Port = 80
            .Connected = False
            .Verified = False
            .theSocket(ConnectSocket(.server, .port))
            'connect
            If .theSocket.Connected Then
                .theSocket.send("Header Here")
            End If
        End With
    
    End Sub
    I'm not sure how to receive data after sending some either..

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB2010: How to spawn types and sockets?

    I would say that you would not want just one instance of the Proxy structure that you have. It seems like you would want one per connection that you were attempting. Therefore, you might make a List (of Proxy), which will prove to be easier than dealing with an array, since you will be adding and removing from the collection.

    However, there then becomes an issue with using a structure rather than a class, because a structure is a value type. This means that you couldn't do something like this:
    Code:
    Dim proxyArray(2) as Proxy
    
    proxyArray(0).Port = 80
    The reason this doesn't work is that proxyArray(2) returns a copy of the Proxy structure in the array at the first index in the array. You are then changing the port attribute of that copy...and then discarding it. To make that change you would have to actually do this:
    Code:
    Dim tmp As Proxy
    tmp = proxyArray(0)
    tmp.Port = 80
    proxyArray(0) = tmp
    Does that seem like a pain? If so, make Proxy a class rather than a structure, and the first code snippet will work. What is happening with a class is that the array holds only the addresses of the objects in memory, so when you access an array index with proxyArray(0), you are not getting a copy of the object, but a copy of the address of the object. There is only one object, and changes made through one address are the same as changes made through any other address.

    On the other hand, if you were to write proxyArray(0) = proxyArray(1), then the two array slots would not be filled with two objects that are similar, but with the address of the exact same object. Changes made to one would be made to the other.

    Therefore, there is a bit of a tradeoff in design between structures and classes.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: VB2010: How to spawn types and sockets?

    Error System.NullReferenceException{"Object reference not set to an instance of an object."}

    Receiving this error on line .Server = theServer code2, I have tried misc things the tooltip suggested without luck, and theServer isn't returning null. I'm slowly piecing vb.net together just a few things I haven't figured out yet. Does anyone by any chance know of a free ebook that's thorough?

    code1
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ReDim aProxy(1)
            ConnectProxy(0, "192.168.1.1", 80)
            ConnectProxy(1, "www.bnetweb.org", 80)
        End Sub
    code2
    Code:
    Imports System.Net.Sockets
    
    Module Module1
    
        Public aProxy() As xyzProxy
        Public Class xyzProxy
            Public Server As String
            Public Port As Long
            Public Connected As Boolean
            Public Verified As Boolean
            Public theSocket As New System.Net.Sockets.TcpClient()
        End Class
    
        Public Sub ConnectProxy(ByRef Index As Integer, ByRef theServer As String, ByRef thePort As Long)
    
            With aProxy(Index)
                .Server = theServer
                .Port = thePort
                .Connected = False
                .Verified = False
                .theSocket.Connect(.Server, .Port)
                'connect
                If .theSocket.Connected Then
                    MsgBox("Woo")
                End If
            End With
    
        End Sub
    
    
    End Module

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB2010: How to spawn types and sockets?

    A couple points:

    1) Don't use an array if you expect to be changing the size. In .NET, you have the List (of T) object, which is VASTLY easier to deal with for dynamically sized collections. It gives you an Add, Insert, Remove, and RemoveAt method, which is much easier than Redimming arrays.

    2) One other advantage of classes over structures is that you don't specifically have to instantiate them, though you can. With classes, however, you always have to create the class. Quite often, this is done explicitly with the New keyword. That is what is missing in your case. In this code:
    Code:
    ReDim aProxy(1)
            ConnectProxy(0, "192.168.1.1", 80)
    you are creating an array that has two slots that can hold Proxy objects, but you haven't actually created any Proxy objects, so those slots are empty. If you stick with the array, you would need to initialize the objects with some code that might look like this (though there are several other options):
    Code:
    ReDim aProxy(1)
    aProxy(0) = New Proxy
    aProxy(1) = New Proxy
    ConnectProxy(0, "192.168.1.1", 80)
    There are more concise ways to do that, but they are all doing the same thing. The added steps create the actual objects and add them to the array.

    3) Sorry, I know of no e-books. I have seen others post various things, though, so I expect others to have better answers to that question.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: VB2010: How to spawn types and sockets?

    The array length won't be modified on a regular basis, it will only be modified when they start the scan. However, data within the array will be modified quite regularly; so it would still be best to use an array right?

    The app has to be high performance, so I can't take shortcuts with easier methods unless those methods are more efficient. When you say..

    Code:
    aProxy(0) = New Proxy
    do you mean aProxy(0) = New xyzProxy? And if so why do I have to declare it above the type and again? Also is there a way to set all objects as new after using 'Redim'?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB2010: How to spawn types and sockets?

    In that case, an array would be fine. Whether one is better than the other, I am not sure. There may be a slight performance advantage to using an array if the size doesn't change.

    Yeah, I did mean xyzProxy.

    You won't get any performance advantage from one form of array initialization over another, as that will happen before the time critical parts, even if there is a difference. There are other ways to initialize arrays. In fact, there are more ways than I am even aware of. One other would be to assign directly, which would look like this (I think):

    Redim aProxy(1) = {New xyzProxy,New xyzProxy}

    You could also initialize the items in a loop. I also seem to remember at least one other way, but at the moment I can't remember anything about it.

    EDIT: I may not have answered your last question. The array doesn't create anything. If it was an array of value types, such as structures, it would be created as a block of memory large enough to hold all the objects in the array, and the object members are set to their default values. When it comes to classes, the array is created with a block of memory large enough to hold the addresses of all the objects. On a 32 bit system, this means that the array holds four bytes for each object. The bytes are 0, though, and the objects have to be created explicitly.
    Last edited by Shaggy Hiker; Sep 2nd, 2010 at 04:40 PM.
    My usual boring signature: Nothing

  11. #11
    Lively Member
    Join Date
    Jul 2010
    Posts
    64

    Re: VB2010: How to spawn types and sockets?

    I'd recommend using List(Of xyzProxy) as previously suggested, as they're a hell of a lot easier than arrays. Also if you want to change the number of elements (and why else would you be using ReDim?) it's a lot more efficient. You could populate it a bit like this:

    Code:
    Dim Proxies as New List(Of xyzProxy)
    Dim ProxyCount as Int32 = 10
    
    For Index = 0 to ProxyCount - 1
       Proxies.Add(New xyzProxy)
    Next
    If you wanted to configure each xyzProxy instance separately, you could use a temporary variable, or a With block, or a custom constructor on the xyzProxy class.

    In general, I advise always using classes, rather than structures, unless you have a very good reason. Many times I have used a structure, thinking it would be better suited, and then eventually had to change it to a class.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB2010: How to spawn types and sockets?

    Quote Originally Posted by stugol View Post
    Many times I have used a structure, thinking it would be better suited, and then eventually had to change it to a class.
    Yeah, so have I, especially when I want to create a collection of them.

    On the other hand, I have a vague memory of somebody (I think it might have been JMC) stating that structures could be more efficient for something. It's only a vague memory, though, and I have never tried testing the two against each other.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: VB2010: How to spawn types and sockets?

    Well.. I managed to get a lot of my vb6 code translated already thanks to the help you have provided. Also got socket settings properly now...except I'm a bit confused as to receiving data from the socket? Everything I read shows it being called manually, as opposed to winsock where it's triggered when data is received. How do I know when to pull data and if data exist?

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