No connection could be made because the target machine actively refused it.

So trying to get a connection from my game to the database on my server, and Unity complains with this error message:

SocketException: No connection could be made because the target machine actively refused it.

I thought at first maybe I gave it the wrong username and password, but that’s not the case. Code:

import System;
import System.Data;
import System.Data.SqlClient;

var DBi : String;
var DBs: String;
var DBu: String;
var DBp: String;

function Start () {
	var dbcon : IDbConnection;
	var connectionString : String = "Server=****; Database=****; Uid=****; Pwd=****";
	dbcon = new SqlConnection(connectionString);

Anyone know what I’ve done wrong? Or if the problem is even on Unity’s side?

  1. Make sure you can ping to the server from your machine. Some companies will turn of ping, so a failure doesn’t necessary mean the connection is blocked. However, if the ping responds you do know that you can reach the server.
  2. Try to telnet to the server and port ( from dos: telnet )
    Again, if it responds, you know it isn’t a comm issue on that port.
    If it fails, I’d begin to suspect it is a comm issue.
  3. try to connect to the server using a different mySQL client. This test will confirm that your username and password are correct and accepted by the server.

I had the same problem and found out it was unity blocking it. Heres how to stop unity from blocking it.

 void Connect()
       //need to tell unity to let me use the port im about to use
       //now I can connect to my server
       ClientSocket.Connect(IP, PORT);

You should never connect directly to a database from your game client. You should create a web service on your server, and expose the endpoints on that service to your game client.

The reason why its not working for you is the port of the SQL Server is most likely 1443, which is not open to being accessed from external clients/servers unless they are in a white list.

My opinion, save yourself the hassle and look at a service like GameSparks which offers easy database functionality and whole ton of other features instead of battling with trying to get your own infrastructure working.