VB6 Winsock Trouble

Reply

Join Date: Sep 2006
Posts: 1
Reputation: one-trick-pony is an unknown quantity at this point 
Solved Threads: 0
one-trick-pony one-trick-pony is offline Offline
Newbie Poster

VB6 Winsock Trouble

 
0
  #1
Sep 18th, 2006
Hi,

New user here. I recently got into developing a simple communications application for client-server model over the internet. I am using winsock in VB6 and trying to send text based messages just to get me going. However, i have been unsuccessful. I am able to connect on same computer, i.e, run client-server software on same computer and communicating with another computers on my home LAN. I can not connect to a computer over the internet. I tried connecting with few friends of mine and i get the message tcp/ip error:Connection Timed out. They are not behind firewall or a router. Following is my client and server code in VB6:

CLIENT CODE:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. ' A simple client using TCP sockets
  3. Option Explicit
  4. Private Sub Form_Load()
  5. cmdSend.Enabled = False
  6.  
  7. ' set up local port and wait for connection
  8. tcpClient.RemoteHost = _
  9. InputBox("Enter the remote host IP address", _
  10. "IP Address", "localhost")
  11.  
  12. If tcpClient.RemoteHost = "" Then
  13. tcpClient.RemoteHost = "localhost"
  14. End If
  15.  
  16. tcpClient.RemotePort = 5000 ' server port
  17. Call tcpClient.Connect ' connect to RemoteHost address
  18. End Sub
  19. Private Sub Form_Terminate()
  20. Call tcpClient.Close
  21. End Sub
  22. Private Sub Form_Resize()
  23. On Error Resume Next
  24. Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0)
  25. Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width)
  26. Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _
  27. ScaleHeight - txtSend.Height)
  28. End Sub
  29. Private Sub tcpClient_Connect()
  30. ' when connection occurs, display a message
  31. cmdSend.Enabled = True
  32. txtOutput.Text = "Connected to IP Address: " & _
  33. tcpClient.RemoteHostIP & vbCrLf & "Port #: " & _
  34. tcpClient.RemotePort & vbCrLf & vbCrLf
  35. End Sub
  36. Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
  37. Dim message As String
  38. Call tcpClient.GetData(message) ' get data from server
  39. txtOutput.Text = txtOutput.Text & message & vbCrLf & vbCrLf
  40. txtOutput.SelStart = Len(txtOutput.Text)
  41. End Sub
  42. Private Sub tcpClient_Close()
  43. cmdSend.Enabled = False
  44. Call tcpClient.Close ' server closed, client should too
  45. txtOutput.Text = _
  46. txtOutput.Text & "Server closed connection." & vbCrLf
  47. txtOutput.SelStart = Len(txtOutput.Text)
  48. End Sub
  49. Private Sub tcpClient_Error(ByVal Number As Integer, _
  50. Description As String, ByVal Scode As Long, _
  51. ByVal Source As String, ByVal HelpFile As String, _
  52. ByVal HelpContext As Long, CancelDisplay As Boolean)
  53. Dim result As Integer
  54. result = MsgBox(Source & ": " & Description, _
  55. vbOKOnly, "TCP/IP Error")
  56. End
  57. End Sub
  58. Private Sub cmdSend_Click()
  59. ' send data to server
  60. Call tcpClient.SendData("CLIENT >>> " & txtSend.Text)
  61. txtOutput.Text = txtOutput.Text & _
  62. "CLIENT >>> " & txtSend.Text & vbCrLf & vbCrLf
  63. txtOutput.SelStart = Len(txtOutput.Text)
  64. txtSend.Text = ""
  65. End Sub


SERVER CODE:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. ' A simple server using TCP sockets
  3. Option Explicit
  4. Private Sub Form_Load()
  5. cmdSend.Enabled = False
  6.  
  7. ' set up local port and wait for connection
  8. tcpServer.LocalPort = 5000
  9. Call tcpServer.Listen
  10. End Sub
  11. Private Sub Form_Resize()
  12. On Error Resume Next
  13. Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0)
  14. Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width)
  15. Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _
  16. ScaleHeight - txtSend.Height)
  17. End Sub
  18. Private Sub Form_Terminate()
  19. Call tcpServer.Close
  20. End Sub
  21. Private Sub tcpServer_ConnectionRequest( _
  22. ByVal requestID As Long)
  23. ' Ensure that tcpServer is closed
  24. ' before accepting a new connection
  25. If tcpServer.State <> sckClosed Then
  26. Call tcpServer.Close
  27. End If
  28.  
  29. cmdSend.Enabled = True
  30. Call tcpServer.Accept(requestID) ' accept connection
  31. txtOutput.Text = _
  32. "Connection from IP address: " & _
  33. tcpServer.RemoteHostIP & vbCrLf & _
  34. "Port #: " & tcpServer.RemotePort & vbCrLf & vbCrLf
  35. End Sub
  36. Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
  37. Dim message As String
  38. Call tcpServer.GetData(message) ' get data from client
  39. txtOutput.Text = _
  40. txtOutput.Text & message & vbCrLf & vbCrLf
  41. txtOutput.SelStart = Len(txtOutput.Text)
  42. End Sub
  43. Private Sub tcpServer_Close()
  44. cmdSend.Enabled = False
  45. Call tcpServer.Close ' client closed, server should too
  46. txtOutput.Text = txtOutput.Text & _
  47. "Client closed connection." & vbCrLf & vbCrLf
  48. txtOutput.SelStart = Len(txtOutput.Text)
  49. Call tcpServer.Listen ' listen for next connection
  50. End Sub
  51. Private Sub tcpServer_Error(ByVal Number As Integer, _
  52. Description As String, ByVal Scode As Long, _
  53. ByVal Source As String, ByVal HelpFile As String, _
  54. ByVal HelpContext As Long, CancelDisplay As Boolean)
  55. Dim result As Integer
  56. result = MsgBox(Source & ": " & Description, _
  57. vbOKOnly, "TCP/IP Error")
  58. End
  59. End Sub
  60. Private Sub cmdSend_Click()
  61. ' send data to the client
  62. Call tcpServer.SendData("SERVER >>> " & txtSend.Text)
  63. txtOutput.Text = txtOutput.Text & _
  64. "SERVER >>> " & txtSend.Text & vbCrLf & vbCrLf
  65. txtSend.Text = ""
  66. txtOutput.SelStart = Len(txtOutput.Text)
  67. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC