Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Socket connection error. SOS!

Discussion in 'Multiplayer' started by GoodBeaver, Apr 19, 2014.

  1. GoodBeaver

    GoodBeaver

    Joined:
    Apr 19, 2014
    Posts:
    1
    Hi,I'm trying to create a connection between unity and my server, but unity gives me SocketExeption: No connection could be made because the target machine actively refused it.
    When I Use this code just in the C # it works great, but Unity does not want to be connected...
    Here is my code:

    SERVER:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7.  
    8. using System.Net;
    9. using System.Net.Sockets;
    10.  
    11. namespace Server001
    12. {
    13.     class Program
    14.     {
    15.         static void Main(string[] args)
    16.         {
    17.             //Set sockets
    18.             IPHostEntry ipHost = Dns.GetHostEntry("localhost");
    19.             IPAddress ipAddr = ipHost.AddressList[0];
    20.             IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 8081);
    21.  
    22.             // Create socket Tcp/Ip
    23.             Socket sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    24.  
    25.             try
    26.             {
    27.                 sListener.Bind(ipEndPoint);
    28.                 sListener.Listen(10);
    29.  
    30.                 //StartListening
    31.                 while (true)
    32.                 {
    33.                     Console.WriteLine("Waiting for connection on port {0}", ipEndPoint);
    34.  
    35.                     // Wait
    36.                     Socket handler = sListener.Accept();
    37.                     string data = null;
    38.  
    39.                     // Talk to client
    40.                    
    41.                     byte[] bytes = new byte[1024];
    42.                     int bytesRec = handler.Receive(bytes);
    43.                    
    44.                     data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
    45.                    
    46.                     Console.Write("Receive from Client" + data + "\n\n");
    47.                    
    48.                     string reply = "Length of your request is" + data.Length.ToString()
    49.                             + " symbols";
    50.                     byte[] msg = Encoding.UTF8.GetBytes(reply);
    51.                     handler.Send(msg);
    52.  
    53.                     if (data.IndexOf("<TheEnd>") > -1)
    54.                     {
    55.                         Console.WriteLine("The End");
    56.                         break;
    57.                     }
    58.                    
    59.                     handler.Shutdown(SocketShutdown.Both);
    60.                     handler.Close();
    61.                 }
    62.             }
    63.             catch (Exception ex)
    64.             {
    65.                 Console.WriteLine(ex.ToString());
    66.             }
    67.             finally
    68.             {
    69.                 Console.ReadLine();
    70.             }
    71.         }
    72.     }
    73. }
    74.  
    75.  
    CLIENT:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. using System.Net;
    6. using System;
    7. using System.Net.Sockets;
    8. using System.Text;
    9.  
    10. public class SEnder : MonoBehaviour {
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.                 // buffer
    15.                 byte[] bytes = new byte[1024];
    16.        
    17.                 //
    18.        
    19.                 //
    20.                 IPHostEntry ipHost = Dns.GetHostEntry ("127.0.0.1");
    21.                 IPAddress ipAddr = ipHost.AddressList [0];
    22.                 IPEndPoint ipEndPoint = new IPEndPoint (ipAddr, 8081);
    23.        
    24.                 Socket sender = new Socket (ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    25.        
    26.                 //
    27.                 sender.Connect (ipEndPoint);
    28.        
    29.                 string message = "Hi, from Unity3d";
    30.        
    31.                 byte[] msg = Encoding.UTF8.GetBytes (message);
    32.        
    33.                 int bytesSent = sender.Send (msg);
    34.        
    35.                 int bytesRec = sender.Receive (bytes);
    36.  
    37.                 sender.Shutdown (SocketShutdown.Both);
    38.                 sender.Close ();
    39.         }
    40.     // Update is called once per frame
    41.     void Update () {
    42.    
    43.     }
    44. }
    45.  
    46.  
    So we just send to the server line "Hi, from Unity3d"
    But got SocketException.
    Help me please. I really do not know what to do =((

    P.S: I think the problem is not the firewall becaus I've tried to disable it but nothing has changed.