mostrar ip local

Iniciado por 4D1cTo, 29 Enero 2006, 06:55 AM

0 Miembros y 1 Visitante están viendo este tema.

Kizar

Y sin usar el control winsock como podria conocer mi ip, es k con las apis estuve mirando y son codes muy largos para algo tan sencillo....

Salu2

{_The_Alwar_}

haber, en tu lan (tu red logica y fisica) tienes una ip, que suele ser 192.168.0.x el router tiene 192.168.0.1 pero si alguien desde internet se quiere conectar a tu ordenador a de saber la ip externa, la que utilizas de cara a internet, como puedo averiguar esa ip? supongo que el winsock no puede averiguarla ya que esta en el router que es como otro ordenador, por tanto tendria que preguntarsela al router, pero no se como

#Borracho.-

Pero para eso ya tendría que haber un programa en tu pc y otro en el router... O si no si está conectado con vos... Con shell que se fije el netstat y te diga la ip del router que vos queres saber.

No es asi?
Saludos
Si nos quedamos en este mundo, que no sea con hambre...

BenRu

Cita de: KiZaR en 31 Enero 2006, 15:10 PM
Y sin usar el control winsock como podria conocer mi ip, es k con las apis estuve mirando y son codes muy largos para algo tan sencillo....

Salu2

Pues utiliza el emulador, socketmaster o algo asi verdad?
Busca info en el foro

{_The_Alwar_}

nose, digo yo q se le podra enviar algun comando al router y que te devuelva datos, en linux seria facil, haces un traceroute y con el grep cojes la segunda ip, en windows no se como pueda filtrar la salida estandar que de tracert

Kizar

ya usaba el socketmaster peor este pones ws.localIP y no te muestra nada  :P

Salu2

scod

 no puedes conocer la ip publica de tu red desde el winsock u otra clase que emule a este control por medio del .localip, tendrias que usar el winsock, y conectarte a una pagina  de esas que te dicen la ip, osea todo por medio de http, capturas los priemros datos que te mandan,.. me explico,..

por ejemplo en la pagina http://whatsmyip.org/ te da tu ip publica, lo que tendrias que hacer es conectarte a esta page y buscar entre el code html las palabras "Your IP Address is" y luego sacar la infoq ue sigue, que seria tu ip,.. me explique? :p

y bue, pa sacar la ip local sin el winsock puedes usar este code que encontre hace mucho,...

Option Explicit
Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128
Private Const ERROR_SUCCESS As Long = 0
Private Const WS_VERSION_REQD As Long = &H101
Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD  &H100 And &HFF&
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD As Long = 1
Private Const SOCKET_ERROR As Long = -1
Private Type HOSTENT
    hName As Long
    hAliases As Long
    hAddrType As Integer
    hLen As Integer
    hAddrList As Long
    End Type
Private Type WSADATA
    wVersion As Integer
    wHighVersion As Integer
    szDescription(0 To MAX_WSADescription) As Byte
    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    wMaxSockets As Integer
    wMaxUDPDG As Integer
    dwVendorInfo As Long
    End Type
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" _
    (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function GetHostName Lib "WSOCK32.DLL" _
    Alias "gethostname" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" _
    (ByVal szHost As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Public Function GetLocalHost() As String
    Dim sHostName As String * 256
    If Not SocketsInitialize() Then
        GetLocalHost = ""
        Exit Function
    End If
    If GetHostName(sHostName, 256) = SOCKET_ERROR Then
        GetLocalHost = ""
''        MsgBox "Windows Sockets Error " & str$(WSAGetLastError()) & _
''        " has occurred. Unable To successfully Get Host Name."
        SocketsCleanup
        Exit Function
    End If
    GetLocalHost = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
    SocketsCleanup
End Function

Public Function GetLocalIP() As String
    Dim sHostName As String * 256
    Dim lpHost As Long
    Dim HOST As HOSTENT
    Dim dwIPAddr As Long
    Dim tmpIPAddr() As Byte
    Dim i As Integer
    Dim sIPAddr As String
    If Not SocketsInitialize() Then
        GetLocalIP = ""
        Exit Function
    End If
    If GetHostName(sHostName, 256) = SOCKET_ERROR Then
        GetLocalIP = ""
''        MsgBox "Windows Sockets Error " & Str$(WSAGetLastError()) & _
''        " has occurred. Unable To successfully Get Host Name."
        SocketsCleanup
        Exit Function
    End If
    sHostName = Trim$(sHostName)
    lpHost = gethostbyname(sHostName)
    If lpHost = 0 Then
        GetLocalIP = ""
''        MsgBox "Windows Sockets are Not responding. " & _
''        "Unable To successfully Get Host Name."
        SocketsCleanup
        Exit Function
    End If
    CopyMemory HOST, lpHost, Len(HOST)
    CopyMemory dwIPAddr, HOST.hAddrList, 4
    ReDim tmpIPAddr(1 To HOST.hLen)
    CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
    For i = 1 To HOST.hLen
        sIPAddr = sIPAddr & tmpIPAddr(i) & "."
    Next
    GetLocalIP = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
    SocketsCleanup
End Function
Private Function HiByte(ByVal wParam As Integer)
    HiByte = wParam  &H100 And &HFF&
   
End Function
Private Function LoByte(ByVal wParam As Integer)
    LoByte = wParam And &HFF&
End Function
Private Sub SocketsCleanup()
    If WSACleanup() <> ERROR_SUCCESS Then
        MsgBox "Socket Error occurred In Cleanup."
    End If
End Sub
Private Function SocketsInitialize() As Boolean
    Dim WSAD As WSADATA
    Dim sLoByte As String
    Dim sHiByte As String
    If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
        MsgBox "The 32-bit Windows Socket is Not responding."
        SocketsInitialize = False
        Exit Function
    End If
    If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
        MsgBox "This application requires a minimum of " & _
        CStr(MIN_SOCKETS_REQD) & " supported sockets."
        SocketsInitialize = False
        Exit Function
    End If
    If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
    (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
    HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
   
    sHiByte = CStr(HiByte(WSAD.wVersion))
    sLoByte = CStr(LoByte(WSAD.wVersion))
   
    MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
    " is Not supported by 32-bit Windows Sockets."
   
    SocketsInitialize = False
    Exit Function
   
End If
SocketsInitialize = True
End Function


que como puedes ver hay dos funciones que rtetornan la ip local y el nombre del host local o nombre de red

saludos
yeah,.. weeeell,...

not now,... u_u,... soon my webpage,... just dont know how soon xD

#Borracho.-

Cita de: KiZaR en 31 Enero 2006, 15:10 PM
son codes muy largos para algo tan sencillo....

Si, es muy largo el code aunque es una buena opción y funcional. Alguien sabe de otra mas corta y funcional?

Saludos
Si nos quedamos en este mundo, que no sea con hambre...

Kizar

Lo k encontre yo, pero no es mas corto...

Muestra todas las ips de un ekipo.
FUENTE: Api-Guide

Citar
Private Sub Command1_Click()
    Module1.Start
End Sub

''In Module1:

''******************************************************************
''Created By Verburgh Peter.
'' 07-23-2001
'' verburgh.peter@skynet.be
''-------------------------------------
''With this small application , you can detect the IP''s installed on your computer,
''including subnet mask , BroadcastAddr..
''
''I''ve wrote this because i''ve a programm that uses the winsock control, but,
''if you have multiple ip''s  installed on your pc , you could get by using the Listen
'' method the wrong ip ...
''Because Winsock.Localip => detects the default ip installed on your PC ,
'' and in most of the cases it could be the LAN (nic) not the WAN (nic)
''So then you have to use the Bind function ,to bind to your right ip..
''but how do you know & find that ip ?
''you can find it now by this appl.. it check''s in the api.. IP Table..
''******************************************************************


Const MAX_IP = 5   ''To make a buffer... i dont think you have more than 5 ip on your pc..

Type IPINFO
     dwAddr As Long   '' IP address
    dwIndex As Long ''  interface index
    dwMask As Long '' subnet mask
    dwBCastAddr As Long '' broadcast address
    dwReasmSize  As Long '' assembly size
    unused1 As Integer '' not currently used
    unused2 As Integer ''; not currently used
End Type

Type MIB_IPADDRTABLE
    dEntrys As Long   ''number of entries in the table
    mIPInfo(MAX_IP) As IPINFO  ''array of IP address entries
End Type

Type IP_Array
    mBuffer As MIB_IPADDRTABLE
    BufferLen As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
Sub main()
Form1.Show
End Sub

''converts a Long  to a string
Public Function ConvertAddressToString(longAddr As Long) As String
    Dim myByte(3) As Byte
    Dim Cnt As Long
    CopyMemory myByte(0), longAddr, 4
    For Cnt = 0 To 3
        ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
    Next Cnt
    ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function

Public Sub Start()
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE

Form1.Text1 = ""

On Error GoTo END1
    GetIpAddrTable ByVal 0&, Ret, True

    If Ret <= 0 Then Exit Sub
    ReDim bBytes(0 To Ret - 1) As Byte
    ''retrieve the data
    GetIpAddrTable bBytes(0), Ret, False
     
    ''Get the first 4 bytes to get the entry''s.. ip installed
    CopyMemory Listing.dEntrys, bBytes(0), 4
    ''MsgBox "IP''s found : " & Listing.dEntrys    => Founded ip installed on your PC..
    Form1.Text1 = Listing.dEntrys & "   IP addresses found on your PC !!" & vbCrLf
    Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf
    For Tel = 0 To Listing.dEntrys - 1
        ''Copy whole structure to Listing..
       '' MsgBox bBytes(tel) & "."
        CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
         Form1.Text1 = Form1.Text1 & "IP address                   : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
         Form1.Text1 = Form1.Text1 & "IP Subnetmask            : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
         Form1.Text1 = Form1.Text1 & "BroadCast IP address  : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
         Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf
    Next

''MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
Exit Sub
END1:
MsgBox "ERROR"
End Sub

Salu2

Ar_mx

Insserta un label, un command y un winsock
en el command codifica asì:
private sub command1_click()
label1=winsock1.localip
end sub
----------------------------------------------
solamente, espero que te funcione
saludos