Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Alishev · Feb 20, 2015 at 09:57 AM · unity 4.6.netsockettcp

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges