Solved

Converting Port number name in integer

Posted on 1998-07-10
6
214 Views
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 ??
0
Comment
Question by:yves_hardy
  • 4
6 Comments
 
LVL 6

Expert Comment

by:alamo
Comment Utility
To get the port number, take a look at the getservbyname function. It's a little tricky since it returns a pointer.
0
 

Expert Comment

by:platiumstar
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:Chizl
Comment Utility
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
 
LVL 6

Accepted Solution

by:
alamo earned 50 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
of course in my code above strService should be set to the service you want, for example strService = "nntp"
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now