Hello I'm trying to connect my PC to remote PC with socket but I have error 40006.
I'm sure that there is not connection.

Here is code:

SERVER ..............

Option Explicit
Dim iSockets As Integer
Dim sServerMsg As String
Dim sRequestID As String
Dim Income() As String

Private Sub Form_Load()
    Form1.Show
    lblHostID.Caption = Socket(0).LocalHostName
    lblAddress.Caption = Socket(0).LocalIP
    Socket(0).LocalPort = 1
    sServerMsg = "Listening to port: " & Socket(0).LocalPort
    List1.AddItem (sServerMsg)
    Socket(0).Listen
End Sub

Private Sub socket_ConnectionRequest(index As Integer, ByVal requestID As Long)
'Check if there is a connection request, confirm that it is from the right port then allow connection
    sServerMsg = "Connection request id " & requestID & " from " & Socket(index).RemoteHostIP
  If index = 0 Then
    List1.AddItem (sServerMsg)
    sRequestID = requestID
    iSockets = iSockets + 1
    lblConnections.Caption = iSockets
    Load Socket(iSockets)
    Socket(iSockets).LocalPort = 1001
    Socket(iSockets).Accept requestID
  End If
End Sub

Private Sub socket_Close(index As Integer)
    'close a particular connection and decrease the number of concurrent connections
    sServerMsg = "Connection closed: " & Socket(index).RemoteHostIP
    List1.AddItem (sServerMsg)
    Socket(index).Close
    Unload Socket(index)
    iSockets = iSockets - 1
    lblConnections.Caption = iSockets
End Sub

Private Sub socket_DataArrival(index As Integer, ByVal bytesTotal As Long)
Dim strData As String
    ' get data from client
    Socket(index).GetData strData
    sServerMsg = strData
    List1.AddItem (sServerMsg)
End Sub

Public Sub Readout(sItem As String, index As Integer)
Dim strData As String
    strData = Now()
    'send data to client
    sServerMsg = strData & " -> " & Socket(index).RemoteHostIP
    List1.AddItem (sServerMsg)
    Socket(index).SendData strData
End Sub

Public Sub InputBack(sAdder() As String, index As Integer)
End Sub

CLIENT..............

Option Explicit
Private Sub cmddSend_Click()
    If Winsock1.State = sckConnected Then
        Winsock1.SendData Now()
        'shpCond.FillColor = QBColor(14)
        Label3.Caption = "Sending Data"
    Else
        'shpCond.FillColor = QBColor(4)
        Label3.Caption = "Not currently connected to Server"
    End If
End Sub

Private Sub cmdClose_Click()
    Winsock1.Close
    shpCond.FillColor = QBColor(4)
End Sub

Private Sub cmdConnect_Click()
    Winsock1.RemoteHost = "87.15.141.254" 'My host IP
    Winsock1.RemotePort = 2
    Winsock1.Connect
    If Winsock1.State = 6 Then
        'attempting to connect
        shpCond.FillColor = QBColor(14)
    ElseIf Winsock1.State = 7 Then
        'connection has been made
        shpCond.FillColor = QBColor(10)
        txtItem.SetFocus
        cmdConnect.Enabled = False
    Else
        'We could not connect
        shpCond.FillColor = QBColor(4)
        Label3.Caption = "Not Connected to " & Winsock1.RemoteHost
        Winsock1.Close
        cmdConnect.Enabled = True
    End If
End Sub

Private Sub Timer1_Timer()
    'check connection status of  client
    Checkstate (Winsock1.State)
    Confirm
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
    Winsock1.GetData sData
    txtArrival.Text = sData
    shpCond.FillColor = QBColor(10)
End Sub

Private Sub Winsock1_SendComplete()
    Label3.Caption = "Send Complete"
End Sub

I try also with other example program, but nothing.
Help, please

Here's a brief discussion about that problem

Another possibility is the firewall on the pc you want to connect to is blocking your program.

Firewall is OK. Could it be the port?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.