Trying to get python to connect to a GM Server

H

haloflooder

Guest
I'm having a bit of difficulty trying to understand how to get a python client to communicate with a server on a gamemaker program I'm creating.

I did get python to connect to the program but if I try to send any data, the console in GMS 2 says "Error: Login failed".

This is what I'm doing to attempt to send some data from python to a GM server.
Code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
data = struct.pack('<b',*[50])
time.sleep(.1);
s.send(data)
time.sleep(.1);
s.close()
The reason why I'm doing this is because I have some data on a headless OS with some inputs that I need to pass onto the GM program. I can't read MIDI messages with GM without having to fork over $25 for that MIDI extension on the yoyogames marketplace.
 
H

haloflooder

Guest
Alright. I have fixed the issue.

The problem was that I wasn't using "network_create_server_raw" on gamemaker studio. I was originally using "network_create_server".

For those of you who are just interested in creating a python script to send data to gamemaker. I'll post my code here for everyone to use :)

python script
Code:
import socket
import struct
import time

TCP_IP = '192.168.1.115'
TCP_PORT = 8008

buffer_u16 = "H"
buffer_u8 = "b"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
time.sleep(.1)
message = 50
data = 128
buffer_type = buffer_u8+buffer_u16
buffer = struct.pack('<'+buffer_type,*[message,data])

s.send(buffer)
time.sleep(.1)
s.close()
GML
Code:
// Create Event
server = network_create_server_raw(network_socket_tcp, 8008, 10);
sBuffer = buffer_create(1024,buffer_fixed,1);
sSocketList = ds_list_create()

// Async Networking Event
var nEvent = ds_map_find_value(async_load, "type");
switch (nEvent) {
    case network_type_connect:
        var socket = ds_map_find_value(async_load, "socket");
        var ip = ds_map_find_value(async_load, "ip");
        ds_list_add(sSocketList, socket);
        show_debug_message("Client connected! "+ip);
    break;
    case network_type_connect:
        var socket = ds_map_find_value(async_load, "socket");
        var findSocket = ds_list_find_index(sSocketList, socket);
        var ip = ds_map_find_value(async_load, "ip");
        if (findSocket >= 0) {
            ds_list_delete(sSocketList, findSocket);
        }
        show_debug_message("Client disconnected! "+ip);
    break;
    case network_type_data:
        var buffer = ds_map_find_value(async_load, "buffer");
        var socket = ds_map_find_value(async_load,"id");
        var ip = ds_map_find_value(async_load, "ip");
        show_debug_message("Got packet from "+ip);
        buffer_seek(buffer, buffer_seek_start, 0);
        var msgid = buffer_read(buffer, buffer_u8);
        switch (msgid) {
            case 50:
                var data = buffer_read(buffer,buffer_u16);
                show_debug_message("Data: "+string(data));
            break;
        }
    break;
}

// Game End Event
network_destroy(server);
buffer_delete(sBuffer);
ds_list_destroy(sSocketList);
 
Last edited by a moderator:
Top