Results 1 to 7 of 7

Thread: [RESOLVED] Visual Basic 6 - Winsock Connection Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    10

    Resolved [RESOLVED] Visual Basic 6 - Winsock Connection Help

    Alrighty, I made a program for 1 on 1 chat sorta thing, not that effecient really, just for practice. We both have the program open, and try to connect to each other at diffirent times just to make sure there is no interference. Both of us get the "Connection timed out." error message. Heres the code:

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. End Sub
    4.  
    5. Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    6.  
    7. Dim strData As String
    8.  
    9.     ' get the data
    10.  
    11.     Winsock.GetData strData
    12.  
    13.     ' display it in the main textbox
    14.  
    15.     LogData strData
    16.  
    17. End Sub
    18.  
    19. Private Sub Connect_Click()
    20.  
    21. 'Check if theres an IP or not.
    22.  
    23. If IP.Text = "" Then
    24.     MsgBox "No IP Inserted"
    25.     Exit Sub
    26. ElseIf Port.Text = "" Then
    27.     MsgBox "No Port Inserted"
    28.     Exit Sub
    29. End If
    30.  
    31. 'Prevent From Multiple Socket Connections
    32.  
    33.     Winsock.Close
    34.  
    35. 'Set the IP and Port from Txts
    36.  
    37.     Winsock.RemoteHost = IP.Text
    38.     Winsock.RemotePort = Port.Text
    39.  
    40. 'Connect
    41.  
    42.     Winsock.Connect
    43.  
    44. End Sub
    45.  
    46. Private Sub LogData(ByVal strData As String)
    47.  
    48.     ' add string to the end of the textbox
    49.  
    50.     Message.Text = Message.Text & vbCrLf & strData
    51.  
    52.     ' scroll the textbox down
    53.  
    54.     Message.SelStart = Len(Message.Text)
    55.  
    56. End Sub
    57.  
    58. Private Sub Send_Click()
    59.  
    60. 'Check if Anything is Entered
    61. If Message.Text = "" Then
    62.     MsgBox "You have not entered a message."
    63.  
    64. 'Check if You're Connected
    65. ElseIf Winsock.State = sckConnected Then
    66.  
    67. 'Send Message
    68.     Winsock.SendData Message.Text
    69.     DoEvents
    70.  
    71. 'Log
    72.  
    73. LogData Message.Text
    74.  
    75. 'Erase Last Message
    76.     Message.Text = ""
    77.  
    78. Else
    79.  
    80.     MsgBox "You aren't connected."
    81.  
    82. End If
    83.  
    84. End Sub
    85.  
    86. Private Sub Winsock_Connect()
    87.  
    88. 'Tell the User They've Connected
    89.  
    90.     MsgBox "Connected to: " & Winsock.RemoteHostIP
    91.  
    92. End Sub
    93.  
    94. Private Sub Winsock_Error(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)
    95.  
    96. 'Alert the User of Error
    97.  
    98.     MsgBox "Error: " & Description
    99.  
    100. End Sub

    Thanks.

  2. #2
    Lively Member
    Join Date
    Dec 2005
    Posts
    97

    Re: Visual Basic 6 - Winsock Connection Help

    heyy dude...im totally not an expert on this...but i just noticed that you havent set a protocol...i dont know whether it REALLY matters or not..but atleast its a good programming practice in my opinion...

    Try it out and see whether it resolves ur error or not!

  3. #3
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Visual Basic 6 - Winsock Connection Help

    the problem is not the protocol, as the winsock class will determine that he wants TCP when he uses 'Winsock.Connect' method, there is no connection involved with a UDP app.

    how do you expect that to work if there is no code to handle a connection attempt? that is the problem

    somebody needs to be the client, and somebody needs to be the server

    client - this one is the app which tries to connect to the other i.e 'Winsock.Connect'
    server - this one is the app which receives the connection i.e 'Winsock.Listen'

    so basically you need to add this to the server app
    VB Code:
    1. 'in form load
    2. Winsock.Close
    3. Winsock.LocalPort = 8989 'set the port to listen on
    4. Winsock.Listen 'begin waiting for a connection attempt
    also you need some code in the server app to handle the connection request
    VB Code:
    1. Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
    2. Winsock.Close
    3. Winsock.Accept requestID
    4. 'connected !
    5. End Sub

    note that the above code is only for the server app.

    I recommend you make two seperate apps first, client and server, then you will understand it much better, after you mastered that you can implement the client & the server into one single app. So for example you would have a radio button saying do you want to be the client or server

    there is many tutorials @ www.winsockvb.com there is a basic tutorial for making a 1to1 chat application, have a look at that as it explains everything well
    Chris

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    10

    Re: Visual Basic 6 - Winsock Connection Help

    Actually I know about that method, I was trying a diffirent method I found on the site you mentioned. I actually just added a listen feature to the client itself for listening, but now I'm getting that I'm being forcefully rejected...

  5. #5
    Lively Member
    Join Date
    Dec 2005
    Posts
    97

    Re: Visual Basic 6 - Winsock Connection Help

    thanx for the extra info n tutorial 182guy

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Visual Basic 6 - Winsock Connection Help

    the socket can either listen or make connect attempts, a single socket cant listen, and then connect to itself, you need two sockets if your going to put them on the same form. Did you download the attachment from the site? its the completed projects....

    http://www.winsockvb.com/code/chat/chat.zip
    Chris

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    10

    Re: Visual Basic 6 - Winsock Connection Help

    I need two eh? Alright, thanks mate. You were a great help. ^^

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