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
0
Question by GEWLAR · Mar 26, 2014 at 02:50 PM · c#networkingsocketstcp

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 ?

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Mar 26, 2014 at 05:55 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

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 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.

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

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

Related Questions

Unity tcp_client only working on localhost 1 Answer

C# TcpListener is reachable only from local 1 Answer

This script freezes unity. Anyone knows answer? 1 Answer

Multiple Cars not working 1 Answer

Asynchronous socket hangs intermittently on iOS 1 Answer


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