2

I'm trying to send data to a python server:

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',6000))

s.listen(5)

while True:
    clientsocket,address = s.accept()
    print(f"Got connection from {address} !")

from godot:

var socket = PacketPeerUDP.new()
socket.set_dest_address("127.0.0.1",6000)
socket.put_packet("quit".to_ascii())

based on this link

but it doesn't seem to be working, How do I send the data?

cak3_lover
  • 1,440
  • 5
  • 26

2 Answers2

2

i'm not that familiar with python servers, but it looks like you have a python server that listens for TCP connections but in godot you connect via UDP Client.

As seen in this Answer SOCK_STREAM is for TCP Server and SOCK_DGRAM for UDP.

I am not sure which of those you want to use. An example server for UDP would be:

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6000))
bufferSize  = 1024

#s.listen(5)
print("running")
while True:
    bytesAddressPair = s.recvfrom(bufferSize)
    message = bytesAddressPair[0]
    clientMsg = "Message from Client:{}".format(message)
    print(clientMsg)

I copied most of it from here : Sample UDP Server

If you wanted to have a TCP Server you should alter the Godot part to use a TCP Client. See the official docs here

Bugfish
  • 725
  • 5
  • 14
1

figured it out thanks to @René Kling 's answer

just incase someone wants the complete version

python server:

import socket

HOST = "127.0.0.1"
PORT = 6000

s= socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind((HOST, PORT))

while True:
    message, addr = s.recvfrom(1024)
    print(f"Connected by {addr}")

Godot:

extends Node2D
tool

export(bool) var btn =false setget set_btn

var socket = PacketPeerUDP.new()


func set_btn(new_val):
    socket.set_dest_address("127.0.0.1", 6000)
    socket.put_packet("Time to stop".to_ascii())
cak3_lover
  • 1,440
  • 5
  • 26