• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Dim Winsock

Im trying to upload a file using winsock, it works fine if I add the component onto the form, but if I try and make it a declaration in the form it fails, any ideas?

The code that doenst work is below:-

Dim buffer() As Byte
Dim lBytes As Long
Dim temp As String
Dim wsTCP(2) As Winsock

Private Sub Form_Load()
  lBytes = 0
  ReDim buffer(FileLen("c:\Katie.jpg") - 1)
  Open "c:\Katie.jpg" For Binary As 1
  Get #1, 1, buffer
  Close #1
  Load wsTCP(1)
  wsTCP(1).RemoteHost = "localhost"
  wsTCP(1).RemotePort = 1111
  lblStatus = "Connecting..."
End Sub
1 Solution

Load wsTCP(1)


Set wsTCP(1) = new WinSock

'This is how I have my server side set up, this will also accept multiple connection,
'Name the winsock control TCP1 and set the index property to  0 (zero)

Private  gSockInstance
Private Sub Form_Load()

If App.PrevInstance = True Then
End If

On Error GoTo a:
         gSockInstance = 0
         TCP1(0).LocalPort = 12007
        If Err = 10048 Then
      Debug.Print "address in use"
      Exit Sub
End If
      End Sub

Private Sub TCP1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
On Error Resume Next

If Index = 0 Then
      gSockInstance = gSockInstance + 1
      Load TCP1(gSockInstance)
      TCP1(gSockInstance).LocalPort = 0
      TCP1(gSockInstance).Accept requestID
      Load TCP1(gSockInstance)
   End If
 End Sub

Private Sub TCP1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

On Error Resume Next   'to prevent any more error's
TCP1(1).Close   'Close the connection
TCP1(1).Listen   'listen again

End Sub

Private Sub TCP1_DataArrival(Index As Integer, ByVal bytesTotal _
                                   As Long)

      Dim myData As String
      TCP1(gSockInstance).GetData myData
      'do something

End Sub
Mike TomlinsonMiddle School Assistant TeacherCommented:
You MUST place the winsock control on the form.  If you attempt to load a winsock control dynamically via code only it will only work on develop machines that have the VB6 IDE installed on them.  When you attempt to run the code on a machine without the IDE you will get an error.

See here for more details:

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now