Search Unity

TCP client "actively refuses connection" only in Unity and on remote machine.

Discussion in 'Multiplayer' started by hittingray, Nov 27, 2018.

  1. hittingray

    hittingray

    Joined:
    Nov 22, 2018
    Posts:
    4
    Hi all, I have a TCP server written in Python and a client written in C#, which is running in Unity. If the server and Unity client are both running on a local machine (i.e. client connects to 127.0.0.1), the connection is established properly and data is transferred.

    I also wrote a C# client outside Unity for testing purposes previously. Running this client on both a local and remote machine allows a connection to be established and data can be transferred.

    Only when I run the Unity client on a remote machine, do I get this error. I have turned off firewalls on both machines with no luck. Clearly my server is connectable, as it connects in many other scenarios.

    Both computers are running Windows 10, and I am using Unity 2018.2.16f1. Below is the error I get:


    SocketException: No connection could be made because the target machine actively refused it.
    System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy)
    System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP)
    System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point)
    System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port)


    I feel like I have tried every solution for related problems that I have found, but none of them work. I'm not sure if I've overlooked something. Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If things run fine on the same machine but won't work if different machines, you're encountering firewall, incorrect address/port (either on the client or server), packet filtering, or network configuration issues.
     
  3. hittingray

    hittingray

    Joined:
    Nov 22, 2018
    Posts:
    4
    The firewall has been turned off on both machines. Like I mentioned in the OP, I have a separate C# client which just runs in Visual Studio (not in Unity) on which the Unity client is based on. Running this separate client on the remote machine works perfectly fine. I am 100% sure the IP/port is correct. It really seems like the issue has something to do with Unity, but I don't know how.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well Unity is fully capable of making TCP connections with the Socket class, so not sure what else to suggest.
     
  5. CheKhenKho

    CheKhenKho

    Joined:
    Nov 10, 2018
    Posts:
    3
    Create minimal client in Unity, who simply connects and nothing else. Maybe the issue is with some code that you "adopted" from console client.
     
  6. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    SocketException: No connection could be made because the target machine actively refused it. means that you receive RST segment instead ACK on your connection request. There are only two reason for that
    1. Your server machine doesn't have port listening
    2. Firewall rules

    How to check?
    The easiest way to check if the port open try to >telnet xxx.xxx.xxx.xxx port from client machine to server. If there is a server and all ports are open you will be able to establish tcp connection.
    Add diagnostic to your unity code to be sure that ip address and port are exactly the same as it used during telnet session
     
    Joe-Censored likes this.
  7. hittingray

    hittingray

    Joined:
    Nov 22, 2018
    Posts:
    4
    I'm quite confident the code is fine. If the Unity client is run on the same computer as the server, it connects and data is properly transferred. Only when moved to a remote machine does the Unity client refuse to connect.

    1. I've checked, and my server machine has a listening port. I've confirmed this with the console application and telnet.
    2. I turned all firewalls off already, and I still get this behaviour. Unless I am missing something somewhere else? Both machines only have Windows Firewall, with no extra security programs (i.e. no Norton, McAfee, etc.)
     
  8. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @hittingray Sorry, still unclean for me.
    :) will repeat, are you able to open tcp connection from machine where your client running to machine where your server running. you can use telnet or curl just to check that you able to open connection. So not local telnet but telnet with the same machine where unity running?
    Are you sure that unity create connection exactly to the same ip and port as telnet? (just put debug.log)
     
  9. hittingray

    hittingray

    Joined:
    Nov 22, 2018
    Posts:
    4
    I am able to open a TCP client from the remote machine where Unity is running to the server via telnet and a separate console TCP client I wrote. Only inside Unity, does the client get this error and will not connect.

    I am sure the IP and port are the same.
     
  10. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    gotcha. man don't know :( for me it is something which impossible to happen :(
    You see You can open connection from A to B using one tcp client while for other tcp client from A to B connection rejected :( I would say generate bug about this.
     
  11. sebastiao_lucio

    sebastiao_lucio

    Joined:
    Mar 14, 2018
    Posts:
    17
    Hello! I was trying to figure out a method of checking if there is any server "listening" to clients on a local wifi network and I happened to come across your question! Luckily I have a client ready to run in tcp in unity follow the code
    maybe it helps:
    :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Net;
    6. using System.Net.Sockets;
    7. using System.Net.NetworkInformation;
    8. using System.Text;
    9. using System.Text.RegularExpressions;
    10. using System.Threading;
    11. using UnityEngine.UI;
    12.  
    13. public class TCPSocket {
    14.  
    15.     TcpClient clientSocket;
    16.     public string serverIP;
    17.  
    18.  
    19.     private Thread tListenner;
    20.  
    21.     public bool running;
    22.  
    23.     public bool SearchServer() {
    24.         Debug.Log("Connecting");
    25.  
    26.         try {
    27.             clientSocket = new TcpClient("127.0.0.1", 5000);
    28.             byte[] pack = new byte[256];
    29.             var response = "PING"+','+"^^!!!";
    30.             pack = Encoding.ASCII.GetBytes(response);
    31.             NetworkStream networkStream = clientSocket.GetStream();
    32.             networkStream.Write(pack, 0, pack.Length);
    33.             networkStream.Close();
    34.  
    35.  
    36.             Debug.Log("Server"+ serverIP);
    37.             Debug.Log("Connected");
    38.             return true;
    39.         }
    40.         catch
    41.         {
    42.             return false;
    43.  
    44.         }
    45.         return false;
    46.  
    47.     }
    48.  
    49.  
    50.     // connect
    51.     public void StartTCPServer() {
    52.  
    53.  
    54.         if ( tListenner != null && tListenner.IsAlive) {
    55.             //    disconnect();
    56.             while (tListenner != null && tListenner.IsAlive) {
    57.  
    58.  
    59.             }
    60.         }
    61.  
    62.             // start  listener thread
    63.             tListenner = new Thread(
    64.                 new ThreadStart(OnListeningClients));
    65.             tListenner.IsBackground = true;
    66.             tListenner.Start();
    67.  
    68.     }
    69.  
    70.  
    71.  
    72.     public void  OnListeningClients()
    73.     {
    74.  
    75.         try
    76.         {
    77.             Debug.Log("starting server");
    78.             IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
    79.      
    80.  
    81.             TcpListener tcpListener = new TcpListener( new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000));
    82.             tcpListener.Start();
    83.  
    84.             while(running)
    85.             {
    86.                 if(tcpListener.Pending())
    87.                 {
    88.                     TcpClient tcpClient = tcpListener.AcceptTcpClient();
    89.  
    90.                     byte[] pack = new byte[256];
    91.                     NetworkStream ns = tcpClient.GetStream();
    92.                     ns.Read(pack,0,pack.Length);
    93.  
    94.                     CanvasManager.instance.ShowAlertDialog(Encoding.ASCII.GetString(pack));
    95.                 }
    96.  
    97.                 else
    98.                 {
    99.                     Thread.Sleep(500);
    100.                 }
    101.             }
    102.         }
    103.         catch
    104.         {
    105.             throw;
    106.         }
    107.     }
    108.  
    109.  
    110.  
    111. }
    112.  
     
  12. TheHumanMan

    TheHumanMan

    Joined:
    Apr 12, 2020
    Posts:
    2
    I had the same problem with tcp sockets in unity on windows. When I ran my project in Linux everything worked fine until I updated unity now only some 9f the data is received in windows but on Linux again everything works perfectly