Results 1 to 5 of 5

Thread: Winsock Control - Identical simple code works in one project but not the other

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    51

    Winsock Control - Identical simple code works in one project but not the other

    Ok, this one is a little embarrassing, I can't seem to sort it out. I have found a lot of examples in forums about Winsock controls that won't connect, but strangely not in the very simple way it isn't connecting for me.

    I have identical code to connect a Winsock object to a network in two applications. The first application is a simple demonstration program which just connects to the port and transmits and receives some data. Very simple. The second program is enormous and I am just adding the ability to talk to an Ethernet device instead of a COM port (it is a data-logging program for my company).

    Here is the simple setup:

    TCPClient.Close
    TCPClient.RemoteHost = "192.168.0.24"
    TCPClient.RemotePort = "10001"
    TCPClient.Connect


    If TCPClient.State = sckConnected Then
    i = i 'never ends up here, even when I step through (in other words there has been time to connect, a delay)
    Else
    i = i 'always ends up here, and the state is always 6, connecting. However, in the demonstration program, the program always ends up here.
    End If

    When I step through in the demonstration program, the .State value is always 7 when I get to that line. However, in the giant logging program, even when I move these lines to the very first lines of the startup object, the .State value is always 6, or connecting. That said, somehow, when it is added to the communication routines, it does seem to connect and work most of the time. However, I need for it to work perfectly, and what I am observing is not perfect, and at times, not functional.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Winsock Control - Identical simple code works in one project but not the other

    Is there a server listening and accepting the connection request?

    btw RemotePort should not be a string, remove the "s

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock Control - Identical simple code works in one project but not the other

    From what little we've been shown I suspect this may be another example of a program written as if it was QBasic on a single-tasking DOS system. This is where you try to write massive amounts of the program as running within one or a very few event handlers, then using busy-wait loops and DoEvents and Sleep calls to try to force this straight-line QBasic code to work after a fashion.

    Sadly this sort of shabby coding has been popularized by heavy COM port users over the years. By and large these people are not professional programmers, yet bizarrely enough many have even had books published teaching others how to "talk wrong."

    This is never reliable, but at network speeds the problems can show up much more often than when using slow COM ports.


    Aside from Sub Main (when you have one) all of your code in a VB6 program is event handling code. Event handlers are meant to handle their events and exit back to the UI thread's message loop as soon as possible.

    When your TCP client calls the Connect method its next step is to exit the current event handler. Eventually either the Connect or Error event will be raised, and then it does the next appropriate bit of work there.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    51

    Re: Winsock Control - Identical simple code works in one project but not the other

    There are devices on the network whose MAC addresses I am finding to make sure they are the right kind of device. Then I need to connect to the port and send commands to them and receive data back. The code I attached is just test code to see if my Winsock object has connected or not. My question is why does it not connect when stepping through these lines in one program, but in the other program it does.

    Update: I went back to this today and stepped through it again. Now it is showing up as connected.

    Again, I've put the whole thing together and it works, but it is a little flaky. And this basic connection problem doesn't make any sense to me.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    51

    Re: Winsock Control - Identical simple code works in one project but not the other

    Oh, and I did remove the "s. This was left over from when I was accepting the port from a text box, which works without issue. But there is no difference.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width