Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Alpha_ThePro · Sep 06, 2016 at 01:40 PM · c#networking

Is this a c# error or unity bug - networking

so i built these two as sperate c# projects and it worked fine, the problem is as soon as i tried to use the code in unity i get this error: "No connection could be made cause the target machine actively refused it"

https://www.youtube.com/watch?v=PCwS7F2uK3Q

https://www.youtube.com/watch?v=bbT4IzCoQjs

here is the main class (Connect_Server.cs) (it contains quite a lot of different code to the one in the video due to it not being a form)

 using System.Net.Sockets;
 using System.Net;
 using UnityEngine;
 using System.Threading;
 using System.Text;
 using System;
 using System.IO;
 
 public class Connect_Server : MonoBehaviour {
     Thread login;
     Client client;
     string ip = "127.0.0.1";
     int port = 9825;
 
 
     void Start () {
         client = new Client();
         client.OnConnect += new Client.OnConnectEventHandler(Client_OnConnect);
         client.OnSend += new Client.OnSendEventHandler(Client_OnSend);
         client.OnDisconnect += new Client.OnDisconnectEventHandler(Client_OnDisconnect);
         Debug.Log("well we got this far");
         if (!client.Connected)
             client.Connect(ip, port);
     }
 
     private void Client_OnDisconnect(Client sender)
     {
         Debug.Log("DC'd");
     }
     private void Client_OnSend(Client sender, int sent)
     {
         Debug.Log(string.Format("Data Sent: {0}", sent));
     }
     private void Client_OnConnect(Client sender, bool connected)
     {
         Debug.Log("woooo");
         if (connected)
             Debug.Log("we're in");
     }
 
     void SendText(string text)
     {
         BinaryWriter bw = new BinaryWriter(new MemoryStream());
         bw.Write((int)Commands.String);
         bw.Write(text);
         bw.Close();
         byte[] data = ((MemoryStream)bw.BaseStream).ToArray();
         bw.BaseStream.Dispose();
         client.Send(data, 0, data.Length);
         data = null;
     }
     void SendImage(string path)
     {
         MemoryStream ms = new MemoryStream();
         BinaryWriter bw = new BinaryWriter(ms);
         byte[] b = File.ReadAllBytes(path);
         bw.Write((int)Commands.Image);
         bw.Write(b.Length);
         bw.Write(b);
         bw.Close();
         b = ms.ToArray();
         ms.Dispose();
         client.Send(b, 0, b.Length);
     }
 
     void OnApplicationQuit()
     {
 
     }
     void Update () {
         if (client.Connected)
         {
             if (Input.GetKeyDown(KeyCode.Return))
             {
                 SendText("Hi all");
             }
         }
     }
     
     Socket socket()
     {
         return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     }
 }
 

then the other class (client.cs)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Net;
 using System.Net.Sockets;
 using System;
 
 enum Commands 
 {
     String = 0,
     Image
 }
 public class Client {
 
     public delegate void OnConnectEventHandler(Client sender, bool connected);
     public event OnConnectEventHandler OnConnect;
 
     public delegate void OnSendEventHandler(Client sender, int sent);
     public event OnSendEventHandler OnSend;
 
     public delegate void OnDisconnectEventHandler(Client sender);
     public event OnDisconnectEventHandler OnDisconnect;
 
     Socket skt;    
     public bool Connected
     {
         get
         {
             if (skt != null)
                 return skt.Connected;
             return false;
         }
     }
 
 
     public Client()
     {
         skt = socket();
     }
 
     public void Connect(string ip, int port)
     {
         if (skt == null)
             skt = socket();
         skt.BeginConnect(ip, port, connectCallback, null);
     }
     void connectCallback(IAsyncResult ar)
     {
         try
         {
             Debug.Log("noot noot");
             skt.EndConnect(ar);
             if (skt.Connected)
                 OnConnect(this, Connected);
         }
         catch (Exception ex)
         {
             Debug.Log(ex.Message);
         }
     }
 
     public void Send(byte[] data, int index, int length)
     {
         skt.BeginSend(BitConverter.GetBytes(length), 0, 4, SocketFlags.None, sendCallback, null);
         skt.BeginSend(data, index, length, SocketFlags.None, sendCallback, null);
     }
     void sendCallback(IAsyncResult ar)
     {
         try
         {
             int sent = skt.EndSend(ar);
             if (OnSend != null)
                 OnSend(this, sent);
         }
         catch (Exception ex)
         {
             Debug.Log(ex.Message);
         }
     }
 
     public void Disconnect()
     {
         try
         {
             if (skt.Connected)
             {
                 skt.Close();
                 skt = null;
                 if (OnDisconnect != null)
                     OnDisconnect(this);
             }
         }
         catch (Exception ex)
         {
             Debug.Log(ex.Message);
         }
     }
 
     Socket socket()
     {
         return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UNet: Giving authority to all clients for scene objects 0 Answers

Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer

Download Multiple Images from Server and show in unity Images 0 Answers

Network Transport Layer API does not work with iOS to PC???? 0 Answers

How to optimize this scripts ? 2 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