Solved

Converting Port number name in integer

Posted on 1998-07-10
6
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

734 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