- Home /
Unity's TCP socket connection attempt "actively refused"
I'm trying to read data from a Java service on an Android device. But Unity always refuses to connect to the TCP socket. It refuses in the editor, it refuses on Android and it refuses if I try to build for Windows. In the Editor, it says: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.
My code is below. It's a adapted from an example from a Microsoft API page. `uPrint` is a function I wrote to print text both in the console and in a GUIText.
The code is called with:
Connect("127.0.0.1", "Hello!");
I've tried `"0.0.0.0"` and `"localhost"` for the host name. It didn't work.
I haven't found a way to get at which line exactly the error occurs. Even removing the try/catch blocks does not give me the exact line where it fails.
The Windows firewall allows the Unity editor full access to the network.
void Connect(string server, string message) {
try {
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
int port = 15219;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream(); // GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
// stream.Write(data, 0, data.Length);
uPrint(string.Format("Sent: {0}", message));
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
string responseData = string.Empty;
// Read the first batch of the TcpServer response bytes.
int bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
uPrint(string.Format("Received: {0}", responseData));
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e) {
uPrint(string.Format("ArgumentNullException: {0}", e));
}
catch (SocketException e) {
uPrint(string.Format("SocketException: {0}", e));
}
}
Answer by Bunny83 · Oct 24, 2012 at 09:57 PM
Well, do you have any other software that can connect to your "Java service"? TCP socket connections have to be accepted on the server. When you "connect" you just send a connection request to the server socket. The server has to call accept which will open another socket for the client. It seems your server doesn't do this maybe due to security settings. I'm pretty sure it has nothing to do with the way you connect.
This example is just a test. It sends a message to itself. It's supposed to work even without the Java part. It comes from a $$anonymous$$icrosoft page on the C# API.
Sorry, I'm quite busy these days. Well a message to itself doesn't make much sense with the code you've posted. Where is your listening socket (aka server)?
I've written a simple delphi application year ago which can be used as tcp server and / or client on any port. Unfortunately I don't have my pc available at the moment ;) Anyway i'm sure there are sample applications out there which can be used in a similar way.
You might want to take a look at the server-client example on the Unifycommunity wiki. It comes with a C# standalone application as server.
Hi, I'm using the modification of the C# standalone from your example. I can connect to it via telnet but not via Unity - the same error appears: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it. I'm using the webplayer but I'm succesfully connecting to socket policy server before... Anybody knows what should be the problem?
Answer by Interverse · Feb 14, 2016 at 02:04 AM
@eje211 The Reason your have the actively refused connection problem is because you're probably not using your external Ip and you probably don't have the port forwarded to you computer in your router. Because if you don't have the port forwarded to your router then you can't send and receive messages to other clients.
Answer by iamthecoolguy11 · Nov 12, 2016 at 12:11 AM
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
Security.PrefetchSocketPolicy(IP,PORT);
//now I can connect to my server
ClientSocket.Connect(IP, PORT);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613082828im_/https://answers.unity.com/themes/thub/images/avi.jpg)