Results 1 to 5 of 5

Thread: Winsock background questions

  1. #1

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Question Winsock background questions

    Please answer these questions for 2 scenarios.
    1. The other user is on a LAN
    2. The other user is NOT on the same LAN.

    Q1. I have seen code examples where a cmdDisconnect is not coded. Some programs just close with the "End" statement. Other programs close with "sckMain.Close". What's happening in the background with these two statements? Does it really matter which method is being used?

    Q2. I think the Host is in a dangerous position. I think that there could be programs out there that look for active programs that are listening (Hosts). They could request a connection. How can I grant access to the correct requester? I hope you are not going to say that I need their IP. I would rather not have that info (probably a very naive statement). Could I use a pswd somehow?

    Q3. Are ALL port numbers from 0 to about 64000 really available? Or are there reserved ports, or ports I should stay away from?

    Thanks

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,925

    Re: Winsock background questions

    Quote Originally Posted by AccessShell View Post
    Please answer these questions for 2 scenarios.
    1. The other user is on a LAN
    2. The other user is NOT on the same LAN.


    Q1. I have seen code examples where a cmdDisconnect is not coded. Some programs just close with the "End" statement. Other programs close with "sckMain.Close". What's happening in the background with these two statements? Does it really matter which method is being used?
    Exiting an application with just an End statement is generally bad practice anyway, so I would avoid doing things that way....

    I am assuming cmdDisconnect is a specific example, but it is considered good practice to close down a socket when you are finished with it. If you just exit the application without closing down a socket then it will be closed on the client side anyway; however this could leave the server side socket open until it times out - normally you would want to close the connection cleanly so both sides close the socket down.

    Quote Originally Posted by AccessShell View Post
    Q2. I think the Host is in a dangerous position. I think that there could be programs out there that look for active programs that are listening (Hosts). They could request a connection. How can I grant access to the correct requester? I hope you are not going to say that I need their IP. I would rather not have that info (probably a very naive statement). Could I use a pswd somehow?
    Ultimately that becomes your problem to deal with, if the host is on a private LAN then it might not be too much of an issue but if it is reachable over the internet then potentially anything on the internet could try and connect. Depending on what your application does then whitelisting IP addresses may be a valid option, your firewalling solution might be able to restrict access, but quite often you would need to use something like a username / password (or other security mechanism such as a certificate or ssh key) to authenticate and validate requests.

    Quote Originally Posted by AccessShell View Post
    Q3. Are ALL port numbers from 0 to about 64000 really available? Or are there reserved ports, or ports I should stay away from?

    Thanks
    Technically yes, but some may already be used by other services. Most established applications will have a well known port number that you probably shouldn't use for your own purposes e.g. http listens on port 80, ssh on 22, and so on. https://en.wikipedia.org/wiki/List_o...P_port_numbers has a list of ones that are typically used. Also port numbers below 1024 are generally considered privileged, you might need your app to run with elevated permissions to use these numbers.

  3. #3

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Re: Winsock background questions

    PlausiblyDamp - WOW - great ans. Thanks

    Q1. I was doing the close with the "sckMain.Close" statement anyway. But your explanation was great.
    Q2. If I use a username/pswd, I think I would have to set ip ub beforehand and hardcode it into the program. How would I check that during the request connection?
    Q3. Ill check on Wikipedia


  4. #4

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Re: Winsock background questions

    My current client side code to connect (cmdConnect) is

    Code:
        Me.sckMain.RemoteHost = gstrMachineName             Me.sckMain.RemotePort = Me.lblPort.Caption
        Me.sckMain.Connect
    When I try to move off a LAN into the WORLD, I believe my code ould be

    Code:
       me.sckMain.RemoteHost = Winsock.LocalIP
        Me.sckMain.RemotePort = Me.lblPort.Caption
        Me.sckMain.Connect
    Please confirm.

    My current Host side code for the connection request is

    Code:
    Private Sub sckMain_ConnectionRequest(ByVal requestID As Long)
    
    
        If Me.sckMain.State <> sckConnected Then
            Me.sckMain.Close        'if the socket isn't already connected then close the winsock to accept the request
        End If
    
    
        Me.sckMain.Accept requestID     'accept the connection request
        Me.lblConnection.Visible = True
        Me.lblConnection.Caption = "Connected to: " & Me.sckMain.RemoteHostIP
    
    
    End Sub
    I think this does not change with the change to the cmdConnect procedure. The question is, even though I think I know the answer, I just want to verify, before the red line in the code, can I get the Client IP addr to verify in a local table to determine if I should accept the ConnectionRequest?

  5. #5
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,925

    Re: Winsock background questions

    [QUOTE=AccessShell;5578565]
    When I try to move off a LAN into the WORLD, I believe my code ould be

    Code:
    me.sckMain.RemoteHost = Winsock.LocalIP
    Me.sckMain.RemotePort = Me.lblPort.Caption
    Me.sckMain.Connect
    If you are dealing with your application running over the internet i.e. client and server on different networks then things get a lot more complicated. Often the LocalIP will be a private IP Address (10.x.x.x, 192.168.x.x, maybe a 172.16-31.x.x) and as such will not be reachable over the internet directly. In this case you would need to know the public IP address of the firewall or router the remote system uses to connect to the internet. This router would need to be configured to allow traffic in and have either port forwarding, NAT, or similar configured to allow communication to and from the private address.

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