Converting Port number name in integer

Posted on 1998-07-10
Last Modified: 2013-11-13
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 ??
Question by:yves_hardy
  • 4

Expert Comment

ID: 1465433
To get the port number, take a look at the getservbyname function. It's a little tricky since it returns a pointer.

Expert Comment

ID: 1465434
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.

Expert Comment

ID: 1465435
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.
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Expert Comment

ID: 1465436
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 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")


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

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.

Accepted Solution

alamo earned 50 total points
ID: 1465437
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 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:


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

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()
    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!

Expert Comment

ID: 1465438
of course in my code above strService should be set to the service you want, for example strService = "nntp"

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question