- Home /
TCP/IP Socket problems
Hello.
Using .net 4.0 we have written server side. So, cannot connect to it via unity (via .net connect works fine)
Client working in .net 2.0
So here some code from my server side
Server:
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(Environment.CurrentDirectory);
DBConnector.Initialize();
//Console.Write("IP: "); string ip = Console.ReadLine();
//Console.Write("Port: "); int port = Convert.ToInt32(Console.ReadLine());
//AgentSocket agent = new AgentSocket(ip, port);
AgentSocket agent = new AgentSocket("127.0.0.1", 25001);
agent.RunAsync();
string key = "";
do
{
key = Console.ReadLine();
}
while (key != "exit");
AgentSocket.StopAll();
Console.WriteLine("Session is finished");
Console.ReadLine();
}
}
public class AgentSocket
{
private static List<Thread> _threads = new List<Thread>();
private static List<AgentSocket> _agents = new List<AgentSocket>();
public static void StopAll()
{
}
private IPHostEntry _ipHost;
private IPEndPoint _ipEndPoint;
private IPAddress _ipAdress;
private Socket _listener;
private Socket _handler;
public AgentSocket(string hostNameOrAdress, int port, int ipIndex = 0)
{
_ipHost = Dns.GetHostEntry(hostNameOrAdress);
_ipAdress = _ipHost.AddressList[ipIndex];
_ipEndPoint = new IPEndPoint(_ipAdress, port);
_agents.Add(this);
}
public void RunAsync()
{
_listener = new Socket(_ipAdress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_listener.Bind(_ipEndPoint);
_listener.Listen(10);
_threads.Add(new Thread(Listening));
_threads.Last().Start();
}
private void Listening()
{
while (true)
{
Console.WriteLine("Waiting connection.. {0}", _ipEndPoint);
_handler = _listener.Accept();
string data = null;
byte[] bytes = new byte[1024];
int bytesRec = _handler.Receive(bytes);
GameRequestType request = (GameRequestType)bytes[0];
Console.WriteLine(request);
data += Encoding.UTF8.GetString(bytes, 1, bytesRec - 1);
Console.Write("Received data: " + request + data + "\n\n");
string reply = "Request has " + data.Length.ToString() + " symbols";
byte[] msg = Encoding.UTF8.GetBytes(reply);
_handler.Send(msg);
if (data.IndexOf("<TheEnd>") > -1)
{
Console.WriteLine("The End");
break;
}
_handler.Shutdown(SocketShutdown.Both);
_handler.Close();
}
}
}
Client:
public class GameSocket : MonoBehaviour
{
public string host;
public int port;
private Thread thread;
private void Start()
{
thread = new Thread(() => SendMessageFromSocket(host, port));
thread.Start();
}
private void OnApplicationQuit()
{
thread.Abort();
}
static void SendMessageFromSocket(string host, int port)
{
byte[] bytes = new byte[1024];
IPHostEntry ipHost = Dns.GetHostEntry(host);
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);
Debug.Log(ipAddr);
Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
bool connect = false;
while (!connect)
{
try
{
sender.Connect(ipEndPoint);
connect = true;
}
catch (Exception e)
{
Debug.LogError(e);
Thread.Sleep(500);
}
}
string message = "Hello World";
Debug.Log("Socket is connected with.. " + sender.RemoteEndPoint.ToString());
List<byte> msg = new List<byte>() { (byte)2 };
msg.AddRange(Encoding.UTF8.GetBytes(message));
int bytesSent = sender.Send(msg.ToArray());
int bytesRec = sender.Receive(bytes);
Debug.Log("And Server Say: " + Encoding.UTF8.GetString(bytes, 0, bytesRec));
if (message.IndexOf("<TheEnd>") == -1)
SendMessageFromSocket(host, port);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
}
I'm using client written in .net 4.0 (tried in 2.0 it works fine too).
So unity cannot connect to my server. Debugger says
No connection could be made because the target computer actively refused it unity
Need some help
Answer by thelghome · Jul 31, 2019 at 05:57 PM
If someone is still looking for solution, you may try FMETP STREAM | Forum It provides Streaming Demo with TCP solution, and Web browser demo with Socket.IO. They are tested with .Net4.0 & .Net2.0.
All source code are written in C# and easy to modify.
Supported: Android/iOS/Mac/PC
Your answer
Follow this Question
Related Questions
Unity project and 3rd Party apps 2 Answers
Unity sockets 1 Answer
How can I write to a socket accepted from a tcp listener? 1 Answer
Tcp server 0 Answers
Unhandled Exception: Mono.Linker.ResolutionException: Can not resolve reference 0 Answers