Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: abdiel2475 en 20 Febrero 2011, 06:35 am



Título: Duda con winsock
Publicado por: abdiel2475 en 20 Febrero 2011, 06:35 am
Hola :p jaja bueno
mm vengo aqui preguntandoles una cosa que como puedo hacer esque miren
mi idea es crear un server y que en ese server se conecten max 10 connections
bueno todo eso ya lo ise :)

lo qe tengo duda y quiero resolver es este
un ejemplo estan 4 Pcs connectados:

1,2,3,4 <- estan conectados al server entonces

1,2,4 <- el 3º se desconecta
 
y vuelve a entrar o entra otro nuevo (que valor agarraria? 5º o rellenaria el vacio osea que seria el 3º?)

tengo esa duda y pss si incrementa en mi connectionrequest tengo esto

Código:
Private Sub sockMain_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    If lblConnections.Caption = "10" Then
    sServerMsg = Time & " - " & "Cant accept more connections..."
    List1.AddItem (sServerMsg)
    List1.TopIndex = List1.ListCount - 1
    Else
   sServerMsg = "Connection request id " & requestID & " from " & sockMain(Index).RemoteHostIP
    List1.TopIndex = List1.ListCount - 1
  If Index = 0 Then
    List1.AddItem (sServerMsg)
    sRequestID = requestID
    intSockCnt = intSockCnt + 1
    lblConnections.Caption = intSockCnt
    Load sockMain(intSockCnt)
    'Socket(intSockCnt).LocalPort = 7575
    sockMain(intSockCnt).Accept requestID
    List1.TopIndex = List1.ListCount - 1
  End If
     End If

End Sub

bueno y quise hacer esto:
Código:

Private Sub sockMain_ConnectionRequest(Index As Integer, ByVal requestID As Long)
On Error GoTo Erro
    If lblConnections.Caption = "10" Then
    sServerMsg = Time & " - " & "Cant accept more connections..."
    List1.AddItem (sServerMsg)
    List1.TopIndex = List1.ListCount - 1
    Else
   sServerMsg = "Connection request id " & requestID & " from " & sockMain(Index).RemoteHostIP
    List1.TopIndex = List1.ListCount - 1
  If Index = 0 Then
    List1.AddItem (sServerMsg)
    sRequestID = requestID
    intSockCnt = intSockCnt + 1
    lblConnections.Caption = intSockCnt
    Load sockMain(intSockCnt)
    'Socket(intSockCnt).LocalPort = 7575
    sockMain(intSockCnt).Accept requestID
    List1.TopIndex = List1.ListCount - 1
  End If
     End If
    'End If
     Exit Sub
     
Erro:
    Dim TryCnt As Integer
     For TryCnt = 1 To intSockCnt
        If sockMain(TryCnt).State = sckConnected Then
            Else
             intSockCnt = intSockCnt + 1
    lblConnections.Caption = intSockCnt
    Load sockMain(TryCnt)
    'Socket(intSockCnt).LocalPort = 7575
    sockMain(TryCnt).Accept requestID
    MsgBox "Encontrado" & TryCnt
        End If
    Next TryCnt

End Sub

esque sale error ya que si estan 1,2,3,4
y el 3º se sale y vuelve a entrar qeraria tomar el valor de 4
pero ya existe haci qe sale error :p
alguna idea :D?


Título: Re: Duda con winsock
Publicado por: BlackZeroX en 20 Febrero 2011, 07:52 am
.
Usa este codigo para volver a ocupar indices que ya estan los sockets desconectados, es decir que si antes tenias 0,1,2,3,4 se deconecta el 2 en la siguiente vez con estas funciones podras volver a ocupar dicho indice sin problemas.

Código
  1.  
  2. '
  3. '   /////////////////////////////////////////////////////////////
  4. '   // Autor:   BlackZeroX ( Ortega Avila Miguel Angel )       //
  5. '   //                                                         //
  6. '   // Web:     http://InfrAngeluX.Sytes.Net/                  //
  7. '   //                                                         //
  8. '   //    |-> Pueden Distribuir Este Codigo siempre y cuando   //
  9. '   // no se eliminen los creditos originales de este codigo   //
  10. '   // No importando que sea modificado/editado o engrandesido //
  11. '   // o achicado, si es en base a este codigo es requerido    //
  12. '   // el agradacimiento al autor.                             //
  13. '   /////////////////////////////////////////////////////////////
  14. '
  15. Option Explicit
  16. Private Enum SockState
  17.    sckClosed = 0
  18.    sckOpen
  19.    sckListening
  20.    sckConnectionPending
  21.    sckResolvingHost
  22.    sckHostResolved
  23.    sckConnecting
  24.    sckConnected
  25.    sckClosing
  26.    sckError
  27. End Enum
  28. Public Function IndexSocketCerrado(ByRef Socket As Object) As Integer
  29. Dim Index                   As Integer
  30. Dim SockSt                  As SockState
  31.    IndexSocketCerrado = -1
  32.    For Index = Socket.lbound To Socket.UBound
  33.        With Socket(Index)
  34.            SockSt = .State
  35.            If SockSt = sckClosed Or SockSt = sckListening Or SockSt = sckClosing Then
  36.                'If SockSt = sckClosed Or SockSt = sckListening Or SockSt = sckClosing Or SockSt = sckError Then    '   //  Optativo
  37.                IndexSocketCerrado = Index
  38.                Exit For
  39.            End If
  40.        End With
  41.    Next
  42. End Function
  43.  
  44. Public Function AceptarConexion(ByRef Socket As Object, requestid As Long) As Boolean
  45. Dim SocketIndex             As Integer
  46.    SocketIndex = IndexSocketCerrado(Socket)
  47.    If SocketIndex = -1 Then
  48.        SocketIndex = Socket.UBound + 1
  49.        Load Socket(SocketIndex)
  50.    End If
  51.    Socket(SocketIndex).CloseSck  ' // Poner Close en lugar de CloseSck   si se usa el OCX WindSock de M$.
  52.    Socket(SocketIndex).Accept requestid
  53. End Function
  54.  
  55.  

Ejemplo de uso simple:

Código
  1.  
  2. '   //  ---->
  3. '   //  Donde Socket_In es el socket que aceptara la conexion entrante en el Socket_Conexion
  4. '   //  Cabe destacar que Socket_In debera ser una matrix de controles
  5. '   //  ---->
  6. Private Sub Socket_Conexion_ConnectionRequest(Index As Integer, ByVal requestid As Long)
  7.        Call AceptarConexion(Socket_In, requestid)
  8. End Sub
  9.  
  10.  

CSocketMaster... Con CTL.

http://infrangelux.sytes.net/fileX/?file=/BlackZeroX/Programacion/vb6/CSocketMaster.rar&dir=/BlackZeroX/Programacion/vb6&

Temibles Lunas!¡.


Título: Re: Duda con winsock
Publicado por: abdiel2475 en 20 Febrero 2011, 08:04 am
omg :P muchas gracias
lo calaree!! :D