Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Converting Port number name in integer

Hi There,

I am using VB 5 with Winsock. My VB program first retrieves the hostname and the port number from the local registry. My port number in my registry is defined as name and the number can be retrieved in the services file.

Question : how can I convert the port name in integer or how can I retrieve the socket number in my services file
from VB ??

Second question : how can I check if my remote port number is listening on my port defined in my registry ??
                                which message can I catch from VB if the remote port number is not listening to ??
0
yves_hardy
Asked:
yves_hardy
  • 4
1 Solution
 
alamoCommented:
To get the port number, take a look at the getservbyname function. It's a little tricky since it returns a pointer.
0
 
platiumstarCommented:
I'm not too sure what you're asking but to convert a string to an integer is CInt(PortNumber).

For second question:
so you're running a clientSocket and you want to know if the remote ServerSocket is listening to it's port? I don't think you can, since you don't have a connection with the server yet, otherwise, you can probably get properties like RemotePort and stuff like that.

Hope it helps.
0
 
alamoCommented:
yves_hardy, you should consider splitting this into two separate questions, the two questions aren't directly related and it might help you get an answer. I think the second question also needs to be clearer.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ChizlCommented:
on Question 1:
platiumstar is right all you need is the CInt("123") or CLng("123") if you want a long.

on Question 2:
On you second question, If it's a dialup you can check your RAS (DUN) to see if your connected then do a :
============================

on error resume next
Dim oObjectName as Object
   Set oObjectName = CreatObject("Object.class", "MachineName")

===========================

ohhh sh$#.. I'm sorry your are using VB5?  The above code is VB6, that would be something for you to look forward to int eh future.  

Since your using VB5 go to my site at http://www.karland.com/OurProducts/DCom32/ and download the DCOM32.OCX.

This DCOM32.OCX will alow you to put in two parms like above and it's FREE. If you need a non-interface DLL contact me and I will convert the OCX into a ActiveX DLL and send it to you.  
Then the code would be :

===========================
on error resume next
Dim oObjectName as Object
   Set oObjectName = DCOM321.ConnectToServer("Obeject.class","MachineName")

Then:
===========================

if err.number <> 0 then
   Msgbox "Error: " & err.number & chr(13) & err.description: exit sub
endif
===========================

If you need the DUN call for connection tell me I have written a KRAS32.DLL that will connect, disconnect, add DUN, edit DUN, custom icon, and much much more.
0
 
alamoCommented:
Either you guys are way off, or I misunderstand the question.

As I read it, the question has to do with Winsock communications. yves_hardy has the port as a *name* (for example "nntp", as defined in the services file) not just a number in a string, so Cint won't work. And DCOM, RAS, and DUN are completely irrelevant.

To convert a service name such as "nntp" into a port number, use the getservbyname function in winsock (WSOCK32.DLL). This requires some setup, though - the DLL must be initialized. By far the easiest thing to do is see Microsoft Knowledge Base article Q160215, which is on the web at http://support.microsoft.com/support/kb/articles/Q160/2/15.asp Get that code, including type, constant, and function declarations,  into your project and verify it works for you (so you are sure it's set up properly). Now, the thing is that article doesn't tell you how to to call getservbyname, but once you have all the code in that article you just need to add the following:

Declarations:

Private Declare Function getservbyname Lib "WSOCK32.DLL" (ByVal name As String, ByVal proto As Any) As Long
Private Type servent
    s_name   As Long    ' char FAR *
    s_aliases As Long   ' char FAR * FAR *
    s_port As Integer   ' short
    s_proto As Long     ' char FAR *
End Type

Code:
Dim lpServent As Long
Dim srvinfo As servent
Dim servPort As Long
Dim strService as String

lpServent = getservbyname(strService, vbNullString) ' or replace vbNullString with "tcp"
If lpServent = 0 Then
    MsgBox "getservbyname error: " & WSAGetLastError()
Else
    RtlMoveMemory srvinfo, lpServent, LenB(srvinfo)
End If
servPort = lobyte(srvinfo.s_port) * 256 + hibyte(srvinfo.s_port)
Debug.Print servPort

*That*'s how you convert the service name into a number.

As to your second question - your second question is a little less clear (which is partly why I suggested you post it separately). In general, though, the only way to check if someone is  listening on a particular remote port (on another machine) is to try and connect to them. You can't tell who is listening, either. There's no special call you can make to find out, you just have to connect and see. The errors are defined in the error event listing in Books Online. I think if the remote machine is reachable but noone is listening on the socket sckConnectionRefused is the error, but that depends on the remote machine (sckConnectAborted or another error might occur also, I'm not sure, you should experiment).

Hope I understood properly and this helps, good luck!
0
 
alamoCommented:
of course in my code above strService should be set to the service you want, for example strService = "nntp"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now