- Home /
tcp socket error message (C#)
I'm trying to make a tcp socket. So at the momment I just want to use an example code posted on Unity Answers (http://answers.unity3d.com/questions/15422/unity-project-and-3rd-party-apps.html). using UnityEngine; using System.Collections; using System; using System.IO; using System.Net.Sockets;
public class tcptest : MonoBehaviour {
internal Boolean socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "localhost";
Int32 Port = 843;
void Start () {
setupSocket();
writeSocket("Hello");
}
void Update () {
}
void OnApplicationQuit()
{
try
{
closeSocket();
}
catch(Exception e)
{
Debug.Log(e.Message);
}
}
// **********************************************
public void setupSocket() {
try {
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e) {
Debug.Log("Socket error: " + e);
}
}
public void writeSocket(string theLine) {
if (!socketReady)
return;
String foo = theLine + "\r\n";
theWriter.Write(foo);
theWriter.Flush();
}
public String readSocket() {
if (!socketReady)
return "";
if (theStream.DataAvailable)
return theReader.ReadLine();
return "";
}
public void closeSocket() {
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}
} // end class s_TCP
But the thing is: I allways get an error message: "System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it."
What is the problem and how can I fix it ?
What do you have listening on port 843? Your Unity app needs to connect to something and I guess you don't have that something.
Answer by fholm · Mar 27, 2014 at 05:07 AM
This error message usually indicates that the target computer doesn't have the correct port opened. You can try opening the correct port in your firewall, but you can also try to use a port which is >=1024 as ports 0-1023 usually requires administration privileges of the application for it to answer on.
Answer by fholm · Mar 27, 2014 at 05:07 AM
This error message usually indicates that the target computer doesn't have the correct port opened. You can try opening the correct port in your firewall, but you can also try to use a port which is >=1024 as ports 0-1023 usually requires administration privileges of the application for it to answer on.