Results 1 to 4 of 4

Thread: Winsock not listening

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    111

    Unhappy Winsock not listening

    Hi,

    I have below code which have no error. but after clicking the button it winsock closed automatically and i will not able to connect on winsock via telnet.

    Code:
    Private Sub Command2_Click()
     Dim tcpMailSocket As Object
     Set tcpMailSocket = CreateObject("MSWinsock.Winsock")
        If tcpMailSocket.State <> 2 Then
        tcpMailSocket.Protocol = sckTCPProtocol
        tcpMailSocket.Listen
        End If
        Text1.Text = tcpMailSocket.LocalPort
    End Sub
    Could you please point how to keep listening Winsock using CreateObjet?
    Same code when i use Winsock.ocx component it works well.

    BR

  2. #2
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Winsock not listening

    Why not just use the ocx control? Or the Winsock API? CreateObject is usually used in scripting.
    Last edited by danecook21; Apr 23rd, 2010 at 01:23 PM. Reason: spelling

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Winsock not listening

    Part of your problem is that you are declaring tcpMailSocket object inside your click routine. When the click routine exits, the tcpMailSocket object goes out of scope and VB releases it, at which time the tcpMailSocket object is obviously closing first.

    Suggest either what danecook recommended or declare your tcpMailSocket object at top of your form so it is available to the entire form. If done that way, you will have to call CreateObject once at some point before using it and you should close it and set it to Nothing at some point before your app unloads.

    To better understand declarations and their scope, suggest looking at this FAQ topic and others. The VB FAQ main page is linked in my signature below.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Winsock not listening

    have you checked here
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

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