elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Socket and MultiThread
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Socket and MultiThread  (Leído 5,066 veces)
ntaryl

Desconectado Desconectado

Mensajes: 95



Ver Perfil
Socket and MultiThread
« en: 4 Marzo 2010, 19:26 pm »

Good evening  
I play with a client server application  .
I  know is dificult to make the Server to use  more  than one thread(connection)at the same time .
I mean when i have 2 client connect in the Server . The First execute something and  the Second  Client  make  something  else  in the same time  
If  someone  can explain  the  logic  behind Multithread  or any  book  for  help  

P.s Saw Shark  have  Multithread  ..
    Sorry for my English  


En línea

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: Socket and MultiThread
« Respuesta #1 en: 4 Marzo 2010, 20:37 pm »

are using some sort of loop, it would be good to see some of your code


[Spanish]
esta utilizando algun tipo de bucle, seria bueno ver parte de su codigo


En línea

ntaryl

Desconectado Desconectado

Mensajes: 95



Ver Perfil
Re: Socket and MultiThread
« Respuesta #2 en: 4 Marzo 2010, 20:40 pm »

Thanks for  the Reply  my Friend  
I will  read  
But is Difivuly yo me to make  something  
Iam  not so advance   ....
First  of all must understand  the Logic 
have a nice  day  
En línea

WHK
Moderador Global
***
Desconectado Desconectado

Mensajes: 6.589


Sin conocimiento no hay espíritu


Ver Perfil WWW
Re: Socket and MultiThread
« Respuesta #3 en: 5 Marzo 2010, 15:14 pm »

the multithreads in visual basic 6 is native:

pseudocode:
Código:
while 10 {
 load winsock(i) // when i is number of loop. the socket is a index.
}

while 10 {
 connect winsock(i)
}

event connect winsock(id){
 send data
}
En línea

ntaryl

Desconectado Desconectado

Mensajes: 95



Ver Perfil
Re: Socket and MultiThread
« Respuesta #4 en: 5 Marzo 2010, 17:38 pm »

Thanks man  for the Reply

Read  and  try to understand the  logic  

Questions
1) there  is a main Thread with the  Listen socket  ?
2)when  create  a  new  thread ?
3?How long  the  thread  stay  alive 


see  u  later  
« Última modificación: 5 Marzo 2010, 17:55 pm por ntaryl » En línea

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: Socket and MultiThread
« Respuesta #5 en: 5 Marzo 2010, 20:12 pm »

Hello, This is a simple example of how to work with array of controls

Server

Código
  1. Option Explicit
  2. Dim ColSocket As Collection
  3.  
  4. Private Sub Form_Load()
  5.    Set ColSocket = New Collection
  6.    Winsock1(0).LocalPort = 100
  7.    Winsock1(0).Listen  '<<< Main Conextion (don't close this connections!)
  8. End Sub
  9.  
  10. Private Sub Winsock1_Close(Index As Integer)
  11.    Unload Winsock1(Index)
  12.    ColSocket.Remove "K" & Index
  13. End Sub
  14.  
  15. Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  16.  
  17.  
  18.    Dim FreeIndex As Long, NewKey As String
  19.  
  20.    'FreeIndex = Winsock1.UBound + 1  'Maximum value of the array of controls <(Don't use this)
  21.  
  22.    FreeIndex = GetFreeIndex() '<(use This, prevents the increase of the array)
  23.  
  24.    NewKey = "K" & FreeIndex 'Unique key for control
  25.  
  26.    Load Winsock1(FreeIndex) 'load new control
  27.  
  28.    ColSocket.Add Winsock1(FreeIndex), NewKey 'Add the new control in the collection
  29.  
  30.    ColSocket(NewKey).Accept requestID 'The new control accept the new connections
  31.  
  32.    ColSocket(NewKey).SendData "hola mundo" & FreeIndex 'The new control send a message
  33.  
  34.  
  35. End Sub
  36.  
  37. Private Function GetFreeIndex() As Long 'Get the free index in the controls array
  38.    Dim i As Long, j As Long
  39.  
  40.    For i = 1 To ColSocket.Count
  41.        For j = 1 To ColSocket.Count
  42.            If ColSocket(j).Index = i Then
  43.                GetFreeIndex = 0
  44.                Exit For
  45.            Else
  46.                GetFreeIndex = i
  47.            End If
  48.        Next
  49.        If GetFreeIndex <> 0 Then Exit For
  50.    Next
  51.  
  52.    If GetFreeIndex = 0 Then GetFreeIndex = ColSocket.Count + 1
  53.  
  54. End Function
  55.  
  56. Private Sub Command1_Click()
  57.    SendNewMessageForAllConection
  58. End Sub
  59.  
  60. Private Sub SendNewMessageForAllConection()
  61.    Dim i As Long
  62.    For i = 1 To ColSocket.Count
  63.       ColSocket(i).SendData "New Message for all Connections"
  64.    Next
  65. End Sub
  66.  
  67. Private Sub Command2_Click()
  68.    CloseAllConnections
  69. End Sub
  70.  
  71. Private Sub CloseAllConnections()
  72.    Dim i As Long
  73.    For i = ColSocket.Count To 1 Step -1
  74.        ColSocket(i).Close
  75.        Unload Winsock1(ColSocket(i).Index)
  76.        ColSocket.Remove i
  77.    Next
  78. End Sub
  79.  
  80. 'this works only if the data are small, otherwise it should be modulized or create array index data
  81. Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
  82.    Dim Data As String
  83.    Winsock1(Index).GetData Data 'or ColSocket("K" & Index).GetData Data
  84.    Me.Print Data
  85. End Sub

Client (compile it and run it several instances)

Código
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4.    Winsock1.Connect "127.0.0.1", 100
  5. End Sub
  6.  
  7. Private Sub Winsock1_Close()
  8.    Unload Me
  9. End Sub
  10.  
  11. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  12.    Dim Data As String
  13.    Winsock1.GetData Data
  14.    Me.Caption = Data
  15. End Sub
En línea

ntaryl

Desconectado Desconectado

Mensajes: 95



Ver Perfil
Re: Socket and MultiThread
« Respuesta #6 en: 5 Marzo 2010, 20:20 pm »

Thanks Leandro   
I  have  not problem  to  cretae  a Server  with  socket  in array (accept  many Clients)
Normally  the  Server  wont  with a  client  in time
When  the  first Finish  go to the Next ...
Want to  make  the Server if  is  possible to  work  with  more  than  one  client   
Example 
If  Server  connecte  with  3  clients  .Then  When  the  number 1 send  screen..
the  second  send file 
the  Third  send   Avi  ..
All the clients  work  at  the  same  time   

If  iam  wrong  tell me 


thanks  for the reply 

En línea

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: Socket and MultiThread
« Respuesta #7 en: 6 Marzo 2010, 01:54 am »

Ok, voy a seguir en castellano después usted lo traduce

yo e solucionado eso de la siguiente manera

No envíe el archivo completo, envielo por trozos de 1 kb aproximadamente, cuando se dispara el evento SendComplete active un pulso de un timer para no crear un bucle y así poder formar algo parecido a un Multithreard.

un ejemplo que no lo he probado pero es para que usted tenga una idea de como funciona, es mas lento pero puede enviar varios archivos a la vez


Esto iria dentro de un modulo clase que representa una conexión con un modulo clase de socket y un modulo clase de un timer.

Código:
Const SIZE_OF_BUFFER As Long = 1024

Dim LenData As Long
Dim bData() As Byte
Dim bBuffer() As Byte
Dim lChuncks As Long
Dim lReminder As Long
Dim lPos As Long
Dim SendFileComplete As Boolean

Private Sub SendFile(ByVal FileName As String)
    Dim FF As Integer
    FF = FreeFile
    
    Open FileName For Binary As #FF
      ReDim bData(LOF(FF))
      Get #FF, , bData
    Close #FF
    
    LenData = UBound(bData)
    ReDim bBuffer(SIZE_OF_BUFFER)
    lChuncks = LenData \ SIZE_OF_BUFFER
    lReminder = LenData - lChuncks * SIZE_OF_BUFFER
    SendFileComplete = False
    Call SendSegment
End Sub

        
Private Sub SendSegment()

    If SendFileComplete = True Then Exit Sub

    If lPos <= lChuncks Then
        CopyMemory bBuffer(0), bData(lPos), SIZE_OF_BUFFER
        lPos = lPos + SIZE_OF_BUFFER
        
        SendFileComplete = False
        
        If cSocket.State = 7 Then
            cSocket.SendData bBuffer
        End If
    Else

        If lReminder > 0 Then
            ReDim bBuffer(lReminder)
            CopyMemory bBuffer(0), bData(lPos), lReminder
            
            SendFileComplete = True
            
            If cSocket.State = 7 Then
                cSocket.SendData bBuffer
            End If
        Else
            SendFileComplete = True
        End If
    End If
End Sub

Private Sub cSocket_SendComplete()
    cTimer.StartTimer 1
End Sub

Private Sub cTimer_Timer()
    cTimer.StopTimer
    Call SendSegment
End Sub
En línea

ntaryl

Desconectado Desconectado

Mensajes: 95



Ver Perfil
Re: Socket and MultiThread
« Respuesta #8 en: 6 Marzo 2010, 18:03 pm »

Thanks for the time   
I will check  it  later   
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
A por un MultiThread Decente!
Programación Visual Basic
F3B14N 4 2,993 Último mensaje 23 Febrero 2011, 03:48 am
por F3B14N
Proxy multithread que soporte SSL?
Programación C/C++
RULZY 1 2,492 Último mensaje 17 Junio 2011, 03:44 am
por leogtz
Multithread Socket (Thread per Socket)not Complete « 1 2 »
Programación Visual Basic
ntaryl 12 8,854 Último mensaje 10 Febrero 2012, 18:42 pm
por ntaryl
[?] ¿Multithread? « 1 2 »
Scripting
MeCraniDOS 12 7,169 Último mensaje 30 Agosto 2013, 18:00 pm
por Danyfirex
python diccionario atack multithread bug
Scripting
asdexiva 0 1,895 Último mensaje 16 Junio 2015, 00:42 am
por asdexiva
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines