Solved

Converting Port number name in integer

Posted on 1998-07-10
6
226 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
ID: 1465433
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
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.
0
 
LVL 6

Expert Comment

by:alamo
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:Chizl
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 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
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 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
ID: 1465438
of course in my code above strService should be set to the service you want, for example strService = "nntp"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 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

15 Experts available now in Live!

Get 1:1 Help Now