I have a program that works fine when there is only 1 socket. However, I am trying to adapt the program so that it can receive multiple connections. This part wokrs fine, it is when the server tries and sends a file back to the client that doesn't work.

The code is quite moderatley long, so if anyone would care to take a look, I would appreciate it.

This is the code

VB Code:
  1. Option Explicit
  2. Private Const chunkSize     As Long = 3072 ' 3Kb chunk size
  3. Private blnTransferring     As Boolean     ' status flag
  4. Private lngFilePos          As Long        ' current vb file position
  5. Private strFilename         As String      ' current string being sent
  6. Private filePath            As String
  7. Dim loopcount As Integer
  8. Dim serverIndex As Long
  9. Dim sckIndex As Long
  10.  
  11. Private Sub Form_Load()
  12. filePath = App.Path & "\Test.jpg"
  13. loopcount = 0
  14. serverIndex = 0
  15. Call startServer
  16. End Sub
  17.  
  18. Private Sub sck_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  19. 'With sck(0)
  20.  '   Call .Close
  21.   '  Call .Accept(requestID)
  22. 'End With
  23.  
  24. Dim i As Long, place As Long, freeSock As Long, itemx As Object
  25.    
  26.     ' Search through the array to see if there is a closed
  27.     '  control that we can re-use.
  28.     freeSock = 0
  29.     For i = 1 To serverIndex
  30.         If sck(i).State = sckClosed Then
  31.             freeSock = i
  32.             Exit For
  33.         End If
  34.     Next i
  35.     ' If freeSock is still zero there are no free controls
  36.     '  so load a new one.
  37.     '
  38.     If freeSock = 0 Then
  39.         serverIndex = serverIndex + 1
  40.         Load sck(serverIndex)
  41.    
  42.         sck(serverIndex).Accept requestID
  43.         place = serverIndex
  44.     Else
  45.         sck(freeSock).Accept requestID
  46.         place = freeSock
  47.     End If
  48.     '  If there were not free controls then we added one above
  49.     '   so create an entry in the ListView control for the new
  50.     '   control.  In either case set the state of the new
  51.     '   connection to sckConnected.
  52.     '
  53.     'If freeSock = 0 Then
  54.     '    Set itemx = lstStates.ListItems.Add(, , _
  55.      '       sockServer(serverIndex).RemoteHostIP)
  56.     'Else
  57.     '    Set itemx = lstStates.ListItems.Item(freeSock + 2)
  58.      '   lstStates.ListItems.Item(freeSock + 2).Text = _
  59.     '        sockServer(freeSock).RemoteHostIP
  60.     'End If
  61.     'itemx.SubItems(2) = sockServer(place).RemotePort
  62. End Sub
  63.  
  64. Private Sub sck_DataArrival(Index As Integer, ByVal bytesTotal As Long)
  65. Dim macAdd As String
  66. Dim strData As String
  67.  
  68. sckIndex = Index
  69. 'For the first piece of data arrival the loopcount will be equal to 0
  70. 'and ready to accept the MAC address
  71. 'If loopcount = 0 Then
  72.     'Recieve the MAC address
  73.     Call sck(Index).GetData(macAdd)
  74.     'Increase loopcount
  75.  '   loopcount = 1
  76.  
  77.     If macAdd = "00:11:50:33:CE:B1" Then
  78.         Call sck(Index).SendData("ACCEPT")
  79.     Else
  80.         loopcount = 0
  81.         Call sck(Index).SendData("CLOSE")
  82.     End If
  83.  
  84. 'Else if the MAC address has already been recieved and is valid then send the advert
  85. 'ElseIf loopcount = 1 Then
  86.     Call SendFile(filePath)
  87.     'Increase loopcount so doesnt resend file
  88.  '   loopcount = 2
  89. 'Else
  90.     'Get incoming data and check it
  91.     Call sck(Index).GetData(strData)
  92.     Call SendNextChunk
  93. 'End If
  94. End Sub
  95.  
  96. Private Sub Timer1_Timer()
  97. Call updateState
  98. End Sub
  99.  
  100. Private Sub updateState()
  101. Text1.Text = serverIndex
  102. Text2.Text = sckIndex
  103.  
  104. Select Case sck(0).State
  105.     Case 0
  106.         lblState.Caption = "0 - socket closed"
  107.     Case 1
  108.         lblState.Caption = "1 - socket open"
  109.     Case 2
  110.         lblState.Caption = "2 - socket listening"
  111.     Case 3
  112.         lblState.Caption = "3 - socket connection pending"
  113.     Case 4
  114.         lblState.Caption = "4 - socket resolving host"
  115.     Case 5
  116.         lblState.Caption = "5 - socket host resolved"
  117.     Case 6
  118.         lblState.Caption = "6 - socket connecting"
  119.     Case 7
  120.         lblState.Caption = "7 - socket connected"
  121.     Case 8
  122.         lblState.Caption = "8 - socket closing"
  123.         Call startServer
  124.        
  125.     Case 9
  126.         lblState.Caption = "9 - socket error"
  127. End Select
  128. End Sub
  129.  
  130. Private Sub startServer()
  131. With sck(0)
  132.     Call .Close
  133.     .LocalPort = 10101
  134.     Call .Listen
  135. End With
  136. 'loopcount = 0
  137. End Sub
  138.  
  139. Private Sub SendFile(ByVal strFile As String)
  140.    ' Store the filename
  141.    strFilename = strFile
  142.  
  143.    'txtStatus.Text = txtStatus.Text & vbCrLf & "Sending File"
  144.    'Call lstEvents.AddItem("Sending BOF: " & strFilename)
  145.    
  146.    ' Set the transferring flag, and send the BOF
  147.    ' marker - tell the remote host that a file is coming.
  148.    blnTransferring = True
  149.    Call sck(sckIndex).SendData("BOF" & strFilename)
  150.    DoEvents
  151. End Sub
  152.  
  153. Private Sub SendNextChunk()
  154.    
  155. Dim hFile         As Long
  156. Dim lngChunkSize  As Long
  157. Dim strData       As String
  158.    
  159.    ' If not transferring and or not connected
  160.    If (Not blnTransferring) Then Exit Sub
  161.    
  162.    ' Open the file in Binary mode (generic for file format)
  163.    hFile = FreeFile
  164.    Open strFilename For Binary As #hFile
  165.    
  166.    ' Read next unset piece of the file, move vb file position to after what already read,
  167.    ' then read into strData next bytes
  168.    If (lngFilePos = 0) Then lngFilePos = 1
  169.    Seek hFile, lngFilePos
  170.    
  171.    ' Work out size of this chunk
  172.    ' Normally set in declerations, if less to send than chunksize hen adjust
  173.    lngChunkSize = LOF(hFile) + 1 - lngFilePos
  174.    If (lngChunkSize > chunkSize) Then lngChunkSize = chunkSize
  175.    
  176.    ' If the chunksize is 0, no data left to send,
  177.    ' transfer complete
  178.    If (lngChunkSize = 0) Then
  179.      
  180.       ' Send EOF marker so the remote host knows thats all the data
  181.       strData = "EOF"
  182.       blnTransferring = False
  183.       'txtStatus.Text = txtStatus.Text & vbCrLf & "Transfer Completed"
  184.       'Call lstEvents.AddItem("0 bytes, transfer completed. Sending EOF.")
  185.      
  186.       ' Send the data to the remote host
  187.       Call sck(sckIndex).SendData(strData)
  188.       DoEvents
  189.       Call startServer
  190.     Else
  191.    
  192.       ' Take data from file, increment file pointer for next read
  193.       strData = String$(lngChunkSize, 0)
  194.       Get #hFile, , strData
  195.       lngFilePos = lngFilePos + lngChunkSize
  196.      
  197.       ' Send the data to the remote host
  198.       Call sck(sckIndex).SendData(strData)
  199.      
  200.       'Call lstEvents.AddItem("Sent " & lngChunkSize & " bytes")
  201.       DoEvents
  202.      
  203.    End If

Close #hFile

End Sub