Changing from TCP to UDP

DonMaklesso

Member
So basically I have a basic multiplayer (server - client) in my game running on TCP protocol, but I wanted to see how it works on UDP. But when I do this (change from tcp to udp) client can't connect to the server, like it entered wrong IP (same result). Could you please tell me if I should change something more in my code instead of just replacing all "tcp" with "udp"?

Here's my server's create event:
Code:
var type=network_socket_tcp;  //replace with UDP here
var port=global.port;
max_clients=7;
server=network_create_server(type,port,max_clients);
socket=noone;
var size = 1024;
var bftype = buffer_grow;
var alignment = 1;
buff = buffer_create(size,bftype,alignment);
and my client's create event:
Code:
var ip=global.ip;
var type=network_socket_tcp;  //replace with UDP here
var port=global.port;
socket = network_create_socket(type);
connection = network_connect(socket,ip,port);
var size = 1024;
var btype = buffer_grow;
var alignment = 1;
buff = buffer_create(size,btype,alignment);
 

klawsin

Member
Can we bump this post up, still no response, and I am having the same issue. I've tried to ask Reddit but get no response either. I question if UDP sockets even work with create server command despite it being in the documentation as an option. There are virtually no tutorials or guides or posts anywhere about using this specific command with a UDP socket, only freely sending to each other with network send udp.
 

Slyddar

Member
I question if UDP sockets even work with create server command despite it being in the documentation as an option. There are virtually no tutorials or guides or posts anywhere about using this specific command with a UDP socket, only freely sending to each other with network send udp.
Well they do work. Try this post. He gives a sample project that runs UDP.
 

FrostyCat

Member
Rule #1 with UDP: UDP is a connectionless protocol. DO NOT try to connect or listen for connect/disconnect events.

network_create_server() does work on UDP, but with 3 key differences compared to TCP:
  • The client cuts straight to the chase with network_send_udp() at the target address and port, or network_send_broadcast() at the target port.
  • The server only ever listens for data events.
  • The server distinguishes clients by their return address and port, rather than socket IDs from connect events.
It's wholly inappropriate to take regular TCP code, swap network_socket_tcp for network_socket_udp, and think you're done.
 

klawsin

Member
Rule #1 with UDP: UDP is a connectionless protocol. DO NOT try to connect or listen for connect/disconnect events.

network_create_server() does work on UDP, but with 3 key differences compared to TCP:
  • The client cuts straight to the chase with network_send_udp() at the target address and port, or network_send_broadcast() at the target port.
  • The server only ever listens for data events.
  • The server distinguishes clients by their return address and port, rather than socket IDs from connect events.
It's wholly inappropriate to take regular TCP code, swap network_socket_tcp for network_socket_udp, and think you're done.
It's not that I think it's that easy, I realise that you need to change other functions and data writing to match the UDP protocols. My question is why does the documentation say I can just use the UDP socket for network_create_server and network_connect if UDP is a connectionless protocol and I can't connect then?

Thanks for the information though, I'll have to try to set up a UDP server without using connect/disconnect events then.
 

FrostyCat

Member
My question is why does the documentation say I can just use the UDP socket for network_create_server and network_connect if UDP is a connectionless protocol and I can't connect then?
The documentation says no such thing. UDP is a connectionless protocol, not a serverless protocol.

In UDP, the server simply calls network_create_server(), and sits there waiting only for data events. The client starts with network_create_socket(), then cuts straight to network_send_udp() or network_send_broadcast() right after it sets up. You don't try to connect.
 

klawsin

Member
The documentation says no such thing. UDP is a connectionless protocol, not a serverless protocol.

In UDP, the server simply calls network_create_server(), and sits there waiting only for data events. The client starts with network_create_socket(), then cuts straight to network_send_udp() or network_send_broadcast() right after it sets up. You don't try to connect.
Just rechecked the documentation, you're right. Under network_connect it says you use a socket for the argument, and when creating a socket it gives the tcp,UDP, and ws as options so I assumed I could freely use any of them without issue as long as the server and client had the same type.
Thanks.
 
Top