944,149 Members | Top Members by Rank

Ad:
Sep 18th, 2006
0

VB6 Winsock Trouble

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
one-trick-pony is offline Offline
1 posts
since Sep 2006
Mar 8th, 2010
0

how to connect LAN

how to connect LAN
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jinojpmathew is offline Offline
1 posts
since Mar 2010
Mar 8th, 2010
0
Re: VB6 Winsock Trouble
Hi One_Trick_Pony,
There is no error in your code.

I find Internet and LAN is different. When you are trying to Connect to Internet your IP will be different from your original IP Address. Such chating facilities should be used on LAN but ofcourse you can use on Internet if your friend know Host IP after connecting Internet (remember this IP will not remain same.).

If you want to know more about this. Try Below.

Go to Start then Click Run.
Type ipconfig /all and click OK
You will see your Original IP Adderess. After connecting to internet again Run ipconfig /all. This time I am sure you will get different IP address.

Hope this helps.
Reputation Points: 17
Solved Threads: 42
Posting Whiz in Training
kinwang2009 is offline Offline
243 posts
since Feb 2010
Aug 28th, 2010
0
Re: VB6 Winsock Trouble
eeeuhm your internal ip doesn't change every 5 minutes.... and then you can still make your internal ip static on your router config page. same for external ip... you can use a dns like noip.com to update your ip if changed. your external ip may change once a half year.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jason8100 is offline Offline
1 posts
since Aug 2010
Oct 9th, 2011
0
Re: VB6 Winsock Trouble
maybe this answer to be late.
your problem is connection is not live.i live same problem.
Just add timer and send every 1-2 second ping.
Just send every second ping or you will be disconnected.
example:
function timer1_tick.......
server.send "ping"
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mustibh is offline Offline
5 posts
since Dec 2009
Oct 9th, 2011
0
Re: VB6 Winsock Trouble
Hey,
i need some guidance.Im working on a project in vba(in excel).I am trying to create a shift cipher(example dog would be fqi)
i want the input to be in an input box(have that part) with the text to be shifted and amount that it will be shifted.The output will be in excel in a 20 column by n row array.I made a function to remove all non alphabetic lower case characters.This is what i have so far(theres a lot of things I just need help finishing and putting it all together).

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
<img src="/cgi-bin/mimetex.cgi?'Ask for dimensions of array and output numbers in arraySub NumberArray() 'this puts up input boxes and then stores the inputs to variables NumRows = InputBox("Input the number of rows.") NumColumns = InputBox("Input the number of columns.") For i = 1 To NumRows ' completes 1 row at a time For j = 1 To NumColumns 'goes through each column 1 at time Cells(i, j).Value = j + (i - 1) * NumColumns Next j Next iEnd Sub 'change all letters to lowercase'remove all non alphabetic charactersFunction Remove(x) tempText = LCase(x) 'change all letters to lowercase For i = 1 To Len(x) 'len is number of characters in the script If Asc(Mid(tempText, i, 1)) >= 97 And Asc(Mid(tempText, i, 1)) <= 122 Then '97 is ascii a and 122 is ascii z tempOutput = tempOutput & (Mid(tempText, i, 1)) End If Next i Remove = tempOutput 'outputs the textEnd Function Function ShiftCipher(text, shiftamount) shiftamount = InputBox("Input how many numbers to shift by") Select Case letter Case "a" letter = Chr(97 + shiftamount) Case "b" letter = Chr(98 + shiftamount) Case "c" letter = Chr(99 + shiftamount) Case "d" letter = Chr(100 + shiftamount) Case "e" letter = Chr(101 + shiftamount) Case "f" letter = Chr(102 + shiftamount) Case "g" letter = Chr(103 + shiftamount) Case "h" letter = Chr(104 + shiftamount) Case "i" letter = Chr(105 + shiftamount) Case "j" letter = Chr(106 + shiftamount) Case "k" letter = Chr(107 + shiftamount) Case "l" letter = Chr(108 + shiftamount) Case "m" letter = Chr(109 + shiftamount) Case "n" letter = Chr(110 + shiftamount) Case "o" letter = Chr(111 + shiftamount) Case "p" letter = Chr(112 + shiftamount) Case "q" letter = Chr(113 + shiftamount) Case "r" letter = Chr(114 + shiftamount) Case "s" letter = Chr(115 + shiftamount) Case "t" letter = Chr(116 + shiftamount) Case "u" letter = Chr(117 + shiftamount) Case "v" letter = Chr(118 + shiftamount) Case "w" letter = Chr(119 + shiftamount) Case "x" letter = Chr(120 + shiftamount) Case "y" letter = Chr(121 + shiftamount) Case "z" letter = Chr(122 + shiftamount) End Select ShiftCipher = text + shiftamount End Function" alt="'Ask for dimensions of array and output numbers in arraySub NumberArray() 'this puts up input boxes and then stores the inputs to variables NumRows = InputBox("Input the number of rows.") NumColumns = InputBox("Input the number of columns.") For i = 1 To NumRows ' completes 1 row at a time For j = 1 To NumColumns 'goes through each column 1 at time Cells(i, j).Value = j + (i - 1) * NumColumns Next j Next iEnd Sub 'change all letters to lowercase'remove all non alphabetic charactersFunction Remove(x) tempText = LCase(x) 'change all letters to lowercase For i = 1 To Len(x) 'len is number of characters in the script If Asc(Mid(tempText, i, 1)) >= 97 And Asc(Mid(tempText, i, 1)) <= 122 Then '97 is ascii a and 122 is ascii z tempOutput = tempOutput & (Mid(tempText, i, 1)) End If Next i Remove = tempOutput 'outputs the textEnd Function Function ShiftCipher(text, shiftamount) shiftamount = InputBox("Input how many numbers to shift by") Select Case letter Case "a" letter = Chr(97 + shiftamount) Case "b" letter = Chr(98 + shiftamount) Case "c" letter = Chr(99 + shiftamount) Case "d" letter = Chr(100 + shiftamount) Case "e" letter = Chr(101 + shiftamount) Case "f" letter = Chr(102 + shiftamount) Case "g" letter = Chr(103 + shiftamount) Case "h" letter = Chr(104 + shiftamount) Case "i" letter = Chr(105 + shiftamount) Case "j" letter = Chr(106 + shiftamount) Case "k" letter = Chr(107 + shiftamount) Case "l" letter = Chr(108 + shiftamount) Case "m" letter = Chr(109 + shiftamount) Case "n" letter = Chr(110 + shiftamount) Case "o" letter = Chr(111 + shiftamount) Case "p" letter = Chr(112 + shiftamount) Case "q" letter = Chr(113 + shiftamount) Case "r" letter = Chr(114 + shiftamount) Case "s" letter = Chr(115 + shiftamount) Case "t" letter = Chr(116 + shiftamount) Case "u" letter = Chr(117 + shiftamount) Case "v" letter = Chr(118 + shiftamount) Case "w" letter = Chr(119 + shiftamount) Case "x" letter = Chr(120 + shiftamount) Case "y" letter = Chr(121 + shiftamount) Case "z" letter = Chr(122 + shiftamount) End Select ShiftCipher = text + shiftamount End Function" border="0" /><img src="/cgi-bin/mimetex.cgi?'Ask for dimensions of array and output numbers in array
Sub NumberArray()
'this puts up input boxes and then stores the inputs to variables
NumRows = InputBox("Input the number of rows.")
NumColumns = InputBox("Input the number of columns.")
For i = 1 To NumRows ' completes 1 row at a time
For j = 1 To NumColumns 'goes through each column 1 at time
Cells(i, j).Value = j + (i - 1) * NumColumns
Next j
Next i
End Sub

'change all letters to lowercase
'remove all non alphabetic characters
Function Remove(x)
tempText = LCase(x) 'change all letters to lowercase

For i = 1 To Len(x) 'len is number of characters in the script
If Asc(Mid(tempText, i, 1)) >= 97 And Asc(Mid(tempText, i, 1)) <= 122 Then '97 is ascii a and 122 is ascii z
tempOutput = tempOutput & (Mid(tempText, i, 1))
End If
Next i
Remove = tempOutput 'outputs the text
End Function
Function ShiftCipher(text, shiftamount)
shiftamount = InputBox("Input how many numbers to shift by")
Select Case letter
Case "a"
letter = Chr(97 + shiftamount)
Case "b"
letter = Chr(98 + shiftamount)
Case "c"
letter = Chr(99 + shiftamount)
Case "d"
letter = Chr(100 + shiftamount)
Case "e"
letter = Chr(101 + shiftamount)
Case "f"
letter = Chr(102 + shiftamount)
Case "g"
letter = Chr(103 + shiftamount)
Case "h"
letter = Chr(104 + shiftamount)
Case "i"
letter = Chr(105 + shiftamount)
Case "j"
letter = Chr(106 + shiftamount)
Case "k"
letter = Chr(107 + shiftamount)
Case "l"
letter = Chr(108 + shiftamount)
Case "m"
letter = Chr(109 + shiftamount)
Case "n"
letter = Chr(110 + shiftamount)
Case "o"
letter = Chr(111 + shiftamount)
Case "p"
letter = Chr(112 + shiftamount)
Case "q"
letter = Chr(113 + shiftamount)
Case "r"
letter = Chr(114 + shiftamount)
Case "s"
letter = Chr(115 + shiftamount)
Case "t"
letter = Chr(116 + shiftamount)
Case "u"
letter = Chr(117 + shiftamount)
Case "v"
letter = Chr(118 + shiftamount)
Case "w"
letter = Chr(119 + shiftamount)
Case "x"
letter = Chr(120 + shiftamount)
Case "y"
letter = Chr(121 + shiftamount)
Case "z"
letter = Chr(122 + shiftamount)
End Select

ShiftCipher = text + shiftamount



End Function" alt="'Ask for dimensions of array and output numbers in array
Sub NumberArray()
'this puts up input boxes and then stores the inputs to variables
NumRows = InputBox("Input the number of rows.")
NumColumns = InputBox("Input the number of columns.")
For i = 1 To NumRows ' completes 1 row at a time
For j = 1 To NumColumns 'goes through each column 1 at time
Cells(i, j).Value = j + (i - 1) * NumColumns
Next j
Next i
End Sub

'change all letters to lowercase
'remove all non alphabetic characters
Function Remove(x)
tempText = LCase(x) 'change all letters to lowercase

For i = 1 To Len(x) 'len is number of characters in the script
If Asc(Mid(tempText, i, 1)) >= 97 And Asc(Mid(tempText, i, 1)) <= 122 Then '97 is ascii a and 122 is ascii z
tempOutput = tempOutput & (Mid(tempText, i, 1))
End If
Next i
Remove = tempOutput 'outputs the text
End Function
Function ShiftCipher(text, shiftamount)
shiftamount = InputBox("Input how many numbers to shift by")
Select Case letter
Case "a"
letter = Chr(97 + shiftamount)
Case "b"
letter = Chr(98 + shiftamount)
Case "c"
letter = Chr(99 + shiftamount)
Case "d"
letter = Chr(100 + shiftamount)
Case "e"
letter = Chr(101 + shiftamount)
Case "f"
letter = Chr(102 + shiftamount)
Case "g"
letter = Chr(103 + shiftamount)
Case "h"
letter = Chr(104 + shiftamount)
Case "i"
letter = Chr(105 + shiftamount)
Case "j"
letter = Chr(106 + shiftamount)
Case "k"
letter = Chr(107 + shiftamount)
Case "l"
letter = Chr(108 + shiftamount)
Case "m"
letter = Chr(109 + shiftamount)
Case "n"
letter = Chr(110 + shiftamount)
Case "o"
letter = Chr(111 + shiftamount)
Case "p"
letter = Chr(112 + shiftamount)
Case "q"
letter = Chr(113 + shiftamount)
Case "r"
letter = Chr(114 + shiftamount)
Case "s"
letter = Chr(115 + shiftamount)
Case "t"
letter = Chr(116 + shiftamount)
Case "u"
letter = Chr(117 + shiftamount)
Case "v"
letter = Chr(118 + shiftamount)
Case "w"
letter = Chr(119 + shiftamount)
Case "x"
letter = Chr(120 + shiftamount)
Case "y"
letter = Chr(121 + shiftamount)
Case "z"
letter = Chr(122 + shiftamount)
End Select

ShiftCipher = text + shiftamount



End Function" border="0" />
Reputation Points: 10
Solved Threads: 1
Newbie Poster
diamondw is offline Offline
18 posts
since Sep 2011
Oct 12th, 2011
0
Re: VB6 Winsock Trouble
I think you may need to forward the ports on your router.
Reputation Points: 43
Solved Threads: 0
Light Poster
Joe Shmoe is offline Offline
36 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Split a string into words
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Sorting an Array of numbers in VB 2008





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC