Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di goku370
    Registrato dal
    Oct 2003
    Messaggi
    569

    [VB6] Come fare una chat con l'IP dinamico?

    Vorrei creare una chat per scambiare messaggi con i miei amici...
    Ma come posso sabilire il collegamento tra due pc se l'IP è dinamico?

    E' possibile collegare il Winsock fornendogli un nome statico come un hostname? O c'è qualche altra soluzione?

  2. #2
    aggiungi al progetto un modulo
    e copiaci dentro questo codice
    codice:
    Option Explicit
    
    Private Const MIN_SOCKETS_REQD As Long = 1
    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 SOCKET_ERROR As Long = -1
    Private Const WSADESCRIPTION_LEN = 257
    Private Const WSASYS_STATUS_LEN = 129
    Private Const MAX_WSADescription = 256
    Private Const MAX_WSASYSStatus = 128
    
    Private ERROR_SUCCESS As Variant
    
    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 Type WSADataInfo
        wVersion As Integer
        wHighVersion As Integer
        szDescription As String * WSADESCRIPTION_LEN
        szSystemStatus As String * WSASYS_STATUS_LEN
        iMaxSockets As Integer
        iMaxUdpDg As Integer
        lpVendorInfo As String
    End Type
    
    Private Type HOSTENT
        hName As Long
        hAliases As Long
        hAddrType As Integer
        hLen As Integer
        hAddrList As Long
    End Type
    
    Private Declare Function WSAStartupInfo Lib "WSOCK32" Alias "WSAStartup" _
        (ByVal wVersionRequested As Integer, lpWSADATA As WSADataInfo) As Long
    Private Declare Function WSACleanup Lib "WSOCK32" () As Long
    Private Declare Function WSAGetLastError Lib "WSOCK32" () As Long
    Private Declare Function WSAStartup Lib "WSOCK32" _
        (ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long
    Private Declare Function gethostname Lib "WSOCK32" _
        (ByVal szHost As String, ByVal dwHostLen As Long) As Long
    Private Declare Function gethostbyname Lib "WSOCK32" _
        (ByVal szHost As String) As Long
    Private Declare Sub CopyMemoryIP Lib "KERNEL32" Alias "RtlMoveMemory" _
        (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    
    Public Function GetIPAddress(ByVal sHost As String) 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
            GetIPAddress = ""
            Exit Function
        End If
        sHostName = sHost & vbNullChar
    
        sHostName = Trim$(sHostName)
        lpHost = gethostbyname(sHostName)
        If lpHost = 0 Then
            GetIPAddress = ""
            
            SocketsCleanup
            Exit Function
        End If
        CopyMemoryIP HOST, lpHost, Len(HOST)
        CopyMemoryIP dwIPAddr, HOST.hAddrList, 4
        ReDim tmpIPAddr(1 To HOST.hLen)
        CopyMemoryIP tmpIPAddr(1), dwIPAddr, HOST.hLen
        For i = 1 To HOST.hLen
            sIPAddr = sIPAddr & tmpIPAddr(i) & "."
        Next
        GetIPAddress = 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"
        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 "socket error"
            SocketsInitialize = False
            Exit Function
        End If
        If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
            MsgBox "socket error"
            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 "socket error"
            SocketsInitialize = False
            Exit Function
        End If
        
        SocketsInitialize = True
    End Function
    a questo punto chiamando la funzione GetIPAddress(nome host) ottieni l'ip di quel pc
    Vascello fantasma dei mentecatti nonchè baronetto della scara corona alcolica, piccolo spuccello di pezza dislessico e ubriaco- Colui che ha modificato l'orribile scritta - Gran Evacuatore Mentecatto - Tristo Mietitore Mentecatto chi usa uTonter danneggia anche te

  3. #3
    Utente di HTML.it L'avatar di goku370
    Registrato dal
    Oct 2003
    Messaggi
    569
    Non ho ben chiaro cosa di intende per "host"...
    E' il nome del pc? Come lo trovo?

  4. #4
    mi spieghi come fai a telefonare a uno se non sai il numero e nemmeno come si chiama
    Vascello fantasma dei mentecatti nonchè baronetto della scara corona alcolica, piccolo spuccello di pezza dislessico e ubriaco- Colui che ha modificato l'orribile scritta - Gran Evacuatore Mentecatto - Tristo Mietitore Mentecatto chi usa uTonter danneggia anche te

  5. #5
    Utente di HTML.it L'avatar di goku370
    Registrato dal
    Oct 2003
    Messaggi
    569
    Si certo, ma se io ho davanti un pc, come faccio a trovare il suo nome? Come lo ricavo con codice VB?

  6. #6
    Originariamente inviato da goku370
    Si certo, ma se io ho davanti un pc, come faccio a trovare il suo nome? Come lo ricavo con codice VB?
    codice:
    private Declare Function GetComputerName Lib "kernel32" _
    Alias "GetComputerNameA" (ByVal lpBuffer As String, _
    nSize As Long) As Long
    
    function nome_computer() as string
    const lSize as long = 255
    dim buffer as string
       buffer = space(lsize)
       Call GetComputerName(buffer,lSize)
       nome_computer = trim$(buffer)
    end function
    Vascello fantasma dei mentecatti nonchè baronetto della scara corona alcolica, piccolo spuccello di pezza dislessico e ubriaco- Colui che ha modificato l'orribile scritta - Gran Evacuatore Mentecatto - Tristo Mietitore Mentecatto chi usa uTonter danneggia anche te

  7. #7
    Utente di HTML.it L'avatar di goku370
    Registrato dal
    Oct 2003
    Messaggi
    569
    Grazie mille, vedrò se funziona...

  8. #8
    Utente di HTML.it L'avatar di goku370
    Registrato dal
    Oct 2003
    Messaggi
    569
    Adesso mi viene un dubbio...

    La funzione per ricavare l'hostname che hai postato di fa vedere il nome del computer, quello che si può modificare andando su proprietà sistema nel pannello di controllo...

    Ma per collegarsi a un pc in rete non bisogna avere l'hostname completo (non so come si chiami) una cosa tipo questa:

    host91-16.paal84444.interbusiness.it
    (sarebbe il mio che ho preso da un sito web, ma l'ho modificato a caso).

    Con questo i programmi ti trovano il relativo IP, con l'altro no, giusto?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.