jamesremuscat / pcars

Python client for Project CARS UDP data stream.
15 stars 16 forks source link

OSError on Windows, Socket operation was attempted to an unreachable host #14

Open c-gaskell opened 4 years ago

c-gaskell commented 4 years ago

I'm trying to use this module with Project Cars 2 to send the data over a socket connection to another PC, which acts as a HUD. My code is as follows:

from pcars.stream import PCarsStreamReceiver
import socket, pickle
from udp_stream import Stream

dest, port = "192.168.1.247", 8001
myip = "192.168.1.190"

class MyPCarsListener(object):
    def __init__(self, dest, port, myip):
        self.stream = Stream(dest, port, myip)
        self.stream.create_socket()
        self.stream.bind_socket()

    def handlePacket(self, data):
        data = pickle.dumps(data)

        self.stream.send(data)

listener = MyPCarsListener(dest, port, myip)
stream = PCarsStreamReceiver()
stream.addListener(listener)
stream.start()

The module udp_stream is just a way to get all the connection-related things out of the main code, and is as follows:

import socket, pickle

class Stream:
    def __init__(self, dest, port, myip="127.0.0.1"):
        self.destination = dest
        self.port = port
        self.myip = myip

        self.bound = False

        self.BUFFER = 1024

    def create_socket(self):
        self.socket = socket.socket(socket.AF_INET,
                                    socket.SOCK_DGRAM,
                                    )

    def bind_socket(self):
        self.bound = True
        self.socket.bind((self.myip, self.port))

    def send(self, data):
        self.socket.sendto(data,
                           (self.destination,
                            self.port,
                            )
                           )

    def recv(self):
        if self.bound:
            data = self.socket.recvfrom(self.BUFFER)
            return data
        else:
            return RuntimeError("Socket is unbound, cannot recieve data")

When I run the code, I get the following error:

Traceback (most recent call last):
  File "D:\Program Files\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "D:\Program Files\Python38-32\lib\site-packages\pcars\stream.py", line 29, in run
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
OSError: [WinError 10065] A socket operation was attempted to an unreachable host

Project Cars 2 is setup correctly, with the API set to Project Cars 1, and the UDP frequency as 1. How can I fix this?

jamesremuscat commented 4 years ago

Hmm, it's been quite a long time since I've looked at this project...

Does it work without being wired in to the udp_stream stuff? If not then it looks like you might be encountering this issue, to which the tl;dr is that Windows Firewall is blocking the multicast connection.

c-gaskell commented 4 years ago

I already suspected windows firewall, and I completely disabled it on all networks and retried, to get the same error.

I also commented out all calls to udp_stream and tried again, with just print(data) in handlePacket() and the same error occurred.

Finally, the error states it is raised from within the pcars module, specifically line 29 of pcars\stream.py.