Server Connection Between Python and Unity C# not Working

-1

I am making a server in python and making a unity game listen to it, the python server works fine, but the Unity C# client side produces this error :

SocketException : {0}System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it.

also i have tried disabling my antivirus and thoroughly checked my code to see for any mistakes.

i dont know whats happening. any help would be amazing!

CODE:

python (server):

import socket

s = socket.socket()       
print ("Socket successfully created") 
  

port = 12345        
  
 
s.bind(('', port))        
print ("socket binded to %s" %(port))  
  
  
s.listen(5)   
print ("socket is listening")            
  
  
while True:  


    # Establish connection with client.  
    c, addr = s.accept()      
    print ('Got connection from', addr ) 
    output = 'Thank you for connecting' 
    # send a thank you message to the client.  
    c.send(output.encode('utf-8'))  
    
    # Close the connection with the client  
    c.close()  

Unity C#(client):

using System.Net.Sockets;
using System;
using System.Net;
using System.Text;

public class PythonScriptFeeder : MonoBehaviour
{
   
    void Start()
    {
        StartClient();
        
    }


    public static void StartClient()
    {
        byte[] bytes = new byte[1024];

        try
        {
            // Connect to a Remote server  
            // Get Host IP Address that is used to establish a connection  
            // In this case, we get one IP address of localhost that is IP : 127.0.0.1  
            // If a host has multiple addresses, you will get a list of addresses  
            IPHostEntry host = Dns.GetHostEntry("localhost");
            IPAddress ipAddress = host.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 12345);

            // Create a TCP/IP  socket.    
            Socket sender = new Socket(ipAddress.AddressFamily,
                SocketType.Stream, ProtocolType.Tcp);

            // Connect the socket to the remote endpoint. Catch any errors.    
            try
            {
                // Connect to Remote EndPoint  
                sender.Connect(remoteEP);

                string sp4 = "Socket connected to {0}" + sender.RemoteEndPoint.ToString();

                Debug.Log(sp4);

                // Encode the data string into a byte array.    
                byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

                // Send the data through the socket.    
                int bytesSent = sender.Send(msg);

                // Receive the response from the remote device.    
                int bytesRec = sender.Receive(bytes);

                string sp5 = "Echoed test = {0}" + Encoding.ASCII.GetString(bytes, 0, bytesRec);

                Debug.Log(sp5);

                // Release the socket.    
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

            }
            catch (ArgumentNullException ane)
            {
                string sp = "ArgumentNullException : {0}" + ane.ToString();
                Debug.Log(sp);
            }
            catch (SocketException se)
            {
                string sp1 = "SocketException : {0}" + se.ToString();
                Debug.Log(sp1);
            }
            catch (Exception e)
            {
                string sp2 = "Unexpected exception : {0}" + e.ToString();
                Debug.Log(sp2);
            }

        }
        catch (Exception e)
        {
            string sp3 = e.ToString();
            Debug.Log(sp3);
        }
    }
}

Thank You In advance :)

python
c#
sockets
unity3d
server

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0