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?