Issue using MS Winsock control in VB6

Posted on 2006-06-07
Last Modified: 2013-11-13

I have a VB 6 app that recieves data from another machine. Right now I can recieve data the machine is sending and send back an Ack to the sending machines port number. The issue is I want to use another port number to send the ack to the sending machine.  Doing something like this tcpServer.RemotePort = 3000 does not work since I cannot change the remote port number while the connection is open. I tried to close the current connection then open a scond one (second winsock control on the form but when it hits the TCPserver2.Connect statement it fails with "Address already in use" even thuogh I have closed the previous connection.
Question by:bfeddish
    LVL 13

    Expert Comment


    you should add a delay because the other computer has to respond to your connection close
    LVL 12

    Accepted Solution

    A better solution to that is to create a "control array" for the Winsock control and put it as control array 0.
    When you first make a connection to the remote machine, you load a second instance of the control and assign it to port 0.
    Using Port 0 (zero) will cause the Winsock control to dynamically select an unused port on the local machine.
    This will then be the port that the local machine will communicate with the remote machine in any subsequent communication.

    The remote machine, on the other hand, must also have a control array and will have to load a second instance to accept
    incoming connection requests in the ConnectionRequest event of the Winsock control.

    In your connection sub-routine, try this:
      m_iSockets = m_iSockets + 1
      Load Wsock(m_iSockets)
      Wsock(m_iSockets).LocalPort = 0
      Wsock(m_iSockets).Connect "The-Remote-Machine", "3000"

    In the "ConnectionRequest" of the Winsock control, you will also have to load
    a second instance of the control as follows to accept the incoming connection.
     m_iSockets = m_iSockets + 1
     Load Wsock(m_iSockets)
     Wsock(m_iSockets).Accept requestID

    The "m_iSockets" variable keeps all the current connections made so far, so
    be sure the loop thru when attempting to close all connections to get a clean
    disconnect and free up any memory allocations.
      If m_iSockets >= 1 Then
         For ii = 1 To m_iSockets
           Unload Wsock(m_iSockets)
         m_iSockets = 0
      End If
      ' Close whatever connection is still active
      For ii = Wsock.LBound To Wsock.UBound
    LVL 12

    Expert Comment

    ... Your remote machine (server) just sits and listens for incoming connections on port 3000, but when sending/receiving ACK to/from
    "client" machines, must be on a different port.  This is to keep the listening port (3000) open for any new incoming connections.

    In this way, the server can accept multiple connections simultaneously.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now