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:
' A simple client using TCP sockets
Option Explicit
Private Sub Form_Load()
cmdSend.Enabled = False
' set up local port and wait for connection
tcpClient.RemoteHost = _
InputBox("Enter the remote host IP address", _
"IP Address", "localhost")
If tcpClient.RemoteHost = "" Then
tcpClient.RemoteHost = "localhost"
End If
tcpClient.RemotePort = 5000 ' server port
Call tcpClient.Connect ' connect to RemoteHost address
End Sub
Private Sub Form_Terminate()
Call tcpClient.Close
End Sub
Private Sub Form_Resize()
On Error Resume Next
Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0)
Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width)
Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _
ScaleHeight - txtSend.Height)
End Sub
Private Sub tcpClient_Connect()
' when connection occurs, display a message
cmdSend.Enabled = True
txtOutput.Text = "Connected to IP Address: " & _
tcpClient.RemoteHostIP & vbCrLf & "Port #: " & _
tcpClient.RemotePort & vbCrLf & vbCrLf
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim message As String
Call tcpClient.GetData(message) ' get data from server
txtOutput.Text = txtOutput.Text & message & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Close()
cmdSend.Enabled = False
Call tcpClient.Close ' server closed, client should too
txtOutput.Text = _
txtOutput.Text & "Server closed connection." & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_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)
Dim result As Integer
result = MsgBox(Source & ": " & Description, _
vbOKOnly, "TCP/IP Error")
End
End Sub
Private Sub cmdSend_Click()
' send data to server
Call tcpClient.SendData("CLIENT >>> " & txtSend.Text)
txtOutput.Text = txtOutput.Text & _
"CLIENT >>> " & txtSend.Text & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
txtSend.Text = ""
End Sub
SERVER CODE:
' A simple server using TCP sockets
Option Explicit
Private Sub Form_Load()
cmdSend.Enabled = False
' set up local port and wait for connection
tcpServer.LocalPort = 5000
Call tcpServer.Listen
End Sub
Private Sub Form_Resize()
On Error Resume Next
Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0)
Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width)
Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _
ScaleHeight - txtSend.Height)
End Sub
Private Sub Form_Terminate()
Call tcpServer.Close
End Sub
Private Sub tcpServer_ConnectionRequest( _
ByVal requestID As Long)
' Ensure that tcpServer is closed
' before accepting a new connection
If tcpServer.State <> sckClosed Then
Call tcpServer.Close
End If
cmdSend.Enabled = True
Call tcpServer.Accept(requestID) ' accept connection
txtOutput.Text = _
"Connection from IP address: " & _
tcpServer.RemoteHostIP & vbCrLf & _
"Port #: " & tcpServer.RemotePort & vbCrLf & vbCrLf
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim message As String
Call tcpServer.GetData(message) ' get data from client
txtOutput.Text = _
txtOutput.Text & message & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpServer_Close()
cmdSend.Enabled = False
Call tcpServer.Close ' client closed, server should too
txtOutput.Text = txtOutput.Text & _
"Client closed connection." & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
Call tcpServer.Listen ' listen for next connection
End Sub
Private Sub tcpServer_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)
Dim result As Integer
result = MsgBox(Source & ": " & Description, _
vbOKOnly, "TCP/IP Error")
End
End Sub
Private Sub cmdSend_Click()
' send data to the client
Call tcpServer.SendData("SERVER >>> " & txtSend.Text)
txtOutput.Text = txtOutput.Text & _
"SERVER >>> " & txtSend.Text & vbCrLf & vbCrLf
txtSend.Text = ""
txtOutput.SelStart = Len(txtOutput.Text)
End Sub