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 eje211 · Oct 24, 2012 at 09:52 PM · errorsockettcp

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));
         }        
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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
avatar image eje211 · Oct 24, 2012 at 11:15 PM 0
Share

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.

avatar image Bunny83 · Oct 27, 2012 at 11:35 PM 0
Share

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.

avatar image SV_Janjo · Jul 15, 2014 at 10:10 AM 0
Share

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?

avatar image
0

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.

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
avatar image
0

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);
 }


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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android Tcp Socket Problem 1 Answer

Why i can't receive 2 messages in a row from my server in Unity? 2 Answers

TCP/IP Socket problems 1 Answer

How can I receive UDP messages or similar in WebGL? 1 Answer

Networking error CreateSocketOrThreadFailure 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