locked
No connection could be made because the target machine actively refused it 127.0.0.1:101 RRS feed

  • Question

  • Hi All,

             I was trying out the exmaple application for a chat application given in http://msdn.microsoft.com/en-us/library/aa478452.aspx
    but  i keep get the exception as below no matter what which port i specify, i even turned off the firewall  

    System.Net.Sockets.SocketException was unhandled
      ErrorCode=10061
      Message="No connection could be made because the target machine actively refused it 127.0.0.1:101"
      NativeErrorCode=10061
      Source="System"
     
    please help me out because i have use this application in my application

    Regards
    Archana Muralidhar
    achu
    Tuesday, October 21, 2008 9:01 AM

Answers

  • I have updated my code to reflect the following, and this works to a degree, the only problem is the lblstatus wont update so I threw a try catch round that.


    Run the server app first and then the client app and you should get a connection.

    The DoListen Sub of the server app

      Private Sub DoListen()
            Try

                Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
                Dim ipLocalEndPoint As New IPEndPoint(localAddr, 5000)
                mobjListener = New TcpListener(ipLocalEndPoint)

                mobjListener.Start()
                Do
                    Dim x As New Client(mobjListener.AcceptTcpClient())
                    AddHandler x.Connected, AddressOf OnConnected
                    AddHandler x.Disconnected, AddressOf OnDisconnected
                    AddHandler x.LineReceived, AddressOf OnLineReceived

                    mcolClients.Add(x.ID, x)
                    Dim params() As Object = {"New connection"}
                    Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
                Loop Until False
            Catch
            End Try
      End Sub


    The form load event of the client app

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                ' Connect to the server
                mobjClient = New TcpClient("127.0.0.1", 5000)

                If (mobjClient.Connected) Then
                    DisplayText("Connected to host" & vbCrLf)
                    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
                    Send("New client online")
                End If

            Catch ex As Exception
            End Try

        End Sub


    In the server App I had to throw a try cqtch round the update as there is a threading issue.

        Private Sub UpdateStatus(ByVal t As String)
            Try
                lstStatus.Items.Add(t)
                lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
            Catch ex As Exception
            End Try
        End Sub

    Tuesday, October 21, 2008 1:06 PM

All replies

  • Hi Archana,

    Things you could check,


    Ensure that your server application is running,

    from the run menu type cmd to open a command prompt,
        
    Type NETSTAT -A to check that your server application is listening on the port you specify.

    Try running telnet to see if you can get a connection to your server application, worth a try.

    Regards

    Tony
    Tuesday, October 21, 2008 9:48 AM
  • even when i try running the server i am getting the same exception
    achu
    Tuesday, October 21, 2008 9:52 AM
  • Are you getting the exception when you try to connect from the client app or are you getting the exception when you start the server app?

    Regards

    Tony
    Tuesday, October 21, 2008 9:55 AM
  • i am getting the same exception with both i.e. when trying to connecting to the server or when try to strat the server


    achu
    Tuesday, October 21, 2008 10:15 AM
  •  

    OK Archana,

     

    In your form load event try this code, I managed to simulate your error using the MSDN code.  I also switched my windows firewall off.
    I had to force the connection by calling connect directly after creating the TcpClient. 

    Let me know if this works for you, This is code for the server app form load event. :)  You will want to tidy the code a bit it's a little messy :)

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                ' Get server related information.
                Dim heserver As System.Net.IPHostEntry = Dns.GetHostEntry("localhost")
                Dim curAdd As IPAddress
                For Each curAdd In heserver.AddressList
                    
                    Try
                        Dim ipE As New IPEndPoint(curAdd, 5000)
                        mobjClient = New TcpClient(ipE)
                        mobjClient.Connect("127.0.0.1", 5000)
                        If (mobjClient.Connected) Then
                            Exit For
                        End If
                    Catch ex As Exception

                    End Try

                Next

                If (mobjClient.Connected) Then
                    DisplayText("Connected to host" & vbCrLf)
                    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
                    Send("New client online")
                End If

            Catch ex As Exception
            End Try

        End Sub

    Tuesday, October 21, 2008 11:15 AM
  •  Hi Tony

            I am getting the following Exception



         System.Net.Sockets.SocketException was caught
      ErrorCode=10057
      Message="A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"
      NativeErrorCode=10057
      Source="System"
     
    achu
    Tuesday, October 21, 2008 11:39 AM
  • I have updated my code to reflect the following, and this works to a degree, the only problem is the lblstatus wont update so I threw a try catch round that.


    Run the server app first and then the client app and you should get a connection.

    The DoListen Sub of the server app

      Private Sub DoListen()
            Try

                Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
                Dim ipLocalEndPoint As New IPEndPoint(localAddr, 5000)
                mobjListener = New TcpListener(ipLocalEndPoint)

                mobjListener.Start()
                Do
                    Dim x As New Client(mobjListener.AcceptTcpClient())
                    AddHandler x.Connected, AddressOf OnConnected
                    AddHandler x.Disconnected, AddressOf OnDisconnected
                    AddHandler x.LineReceived, AddressOf OnLineReceived

                    mcolClients.Add(x.ID, x)
                    Dim params() As Object = {"New connection"}
                    Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
                Loop Until False
            Catch
            End Try
      End Sub


    The form load event of the client app

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                ' Connect to the server
                mobjClient = New TcpClient("127.0.0.1", 5000)

                If (mobjClient.Connected) Then
                    DisplayText("Connected to host" & vbCrLf)
                    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
                    Send("New client online")
                End If

            Catch ex As Exception
            End Try

        End Sub


    In the server App I had to throw a try cqtch round the update as there is a threading issue.

        Private Sub UpdateStatus(ByVal t As String)
            Try
                lstStatus.Items.Add(t)
                lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
            Catch ex As Exception
            End Try
        End Sub

    Tuesday, October 21, 2008 1:06 PM
  • Hi Tony

           Yes this works, something imilar i have done, but one problem is that the message gets broadcasted to every on who are connected is there a way where in it can be intended to a particular person on the LAN

    Regards
    Archana Muralidhar
    achu
    Tuesday, October 21, 2008 1:25 PM
  •  I believe you will need to play around with this bit of code in the server app.

      Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
        UpdateStatus("Line:" & Data)

        Dim objClient As Client
        Dim d As DictionaryEntry

        For Each d In mcolClients
          objClient = d.Value
          objClient.Send(Data & vbCrLf)  'this is where it send to all clients.  This will need to be filtered to only send to who you want it to.
        Next
      End Sub

    You could possibly put a property in the client class that holds the ip address of the client. 

    Kind Regards

    Tony

    Tuesday, October 21, 2008 1:42 PM
  • Thank you Tony please be in touch i will ge back if i face any problem as i am new to Socket programming
    achu
    Tuesday, October 21, 2008 1:45 PM