Dim Winsock

Posted on 2005-04-18
Last Modified: 2013-11-13
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
Question by:tonelm54
    LVL 8

    Expert Comment


    Load wsTCP(1)


    Set wsTCP(1) = new WinSock

    LVL 29

    Expert Comment

    '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
    LVL 85

    Accepted Solution

    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:;en-us;313984&Product=vb6

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now