Jump to content

TCP sockets module, am I using it right?


Recommended Posts

Hi, I'm still trying to export game data to a logging program running on a separate machine on my local network. I have written the following code which I hope is sending TCP packets to each player containing everyone's location and a serverTick. How can I tell if the packets are being sent?

I set up my receiving program to listen to the port I know the script is writing to but am not receiving any data. I think I have my port forwarded correctly, when I create a port forwarding service on my router should the service use the sender's IP or the receiver's? During debugging I am running both programs on one machine but still receive no data, Is port forwarding required for localhost communication? Do I need to set up some sort of TCP server/client handshake?!?

At the moment I create a socket from the player's IP address and assign a port number such as 54321 for player one, on my receiver at that IP address I listen to port 54321 but don't hear anything.

Anyway, as you can see I'm a little bit lost! Any tips and guidance is much appreciated. Here is my script:

  
  
--****************************************************************** 
--this function requires the ml_sockets library. Download ml_sockets 
--and place inside MTA San Andreas/server/mods/deathmatch/modules. 
--Add the following line to your mtaserver configuration file: 
--  
--****************************************************************** 
  
--****************************************************************** 
--ALL PLAYERS MUST HAVE JOINED THE SERVER BEFORE CALLING THIS SCRIPT 
--****************************************************************** 
  
-- this script sends a datagram to each player's IP 
-- address containing all player's speed and location 
function broadcast() 
  
    local players = getElementsByType("player") 
    local playerData = {} 
    local sockets = {} 
    local port = {} 
  
    --create 2D array which will contain each player's 
    --respective IP address, game map location and serverTick count 
    for i, player in ipairs(players) do 
        --assign a different port number for each player i.e. 
        --player one port = 54321, player two port = 54322, etc.... 
        port[i] = 54320 + i 
        --create array of transmission sockets 
        sockets[i] = {} 
        --create first dimension of data array 
        playerData[i] = {} 
        --create second dimension of data array containing 9 elements 
        --5 for data entries and 4 for delimiting symbols 
        --i.e. {[iPAddress][*][posX][*][posY][*][posZ][*][tick]} 
        for j=1, 9 do 
            playerData[i][j] = '*' 
        end 
    end 
             
    --get all player's IP address and open socket to that address 
    --on the player's assigned port number 
    for i, player in ipairs(players) do  
        playerData[i][1] = getPlayerIP(player) 
        sockets[i] = sockOpen(playerData[i][1],port[i]) 
    end 
  
    --broadcast each player's locations and speed to 
    --each player's respective IP address and port 
    for i, player in ipairs (players) do 
        --start datagram with symbol 
        sockWrite(sockets[i],"<") 
        --local car = getPedOccupiedVehicle(player) 
        local tick = getTickCount() 
        local posx,posy,posz = getElementPosition(player) 
        playerData[i][3] = posx 
        playerData[i][5] = posy 
        playerData[i][7] = posz 
        playerData[i][9] = tick 
        for j=1, 9 do 
            sockWrite(sockets[i], tostring(playerData[i][j])) 
            --***DEBUG*** print what was sent to console 
            print(playerData[i][j]) 
            --***END DEBUG*** 
        end 
        --end datagram with symbol 
        sockWrite(sockets[i],">") 
    end 
end 
  
setTimer(broadcast,1000,0) 
  
  

Link to comment

I'm having exactly the same problem right now.

I'm trying to use sockets to send data between resources on different servers.

I also found another module for sockets, but didn't try it out yet. https://forum.multitheftauto.com/viewtopic.php?f=108&t=46338

Maybe someone can tell me if this is correct way to do it:

I create a socket on server A with the address of server B and vice versa.

If I write data to the socket on server A now, shouldn't server B receive it?

Bonsai

Link to comment
I'm having exactly the same problem right now.

I'm trying to use sockets to send data between resources on different servers.

I also found another module for sockets, but didn't try it out yet. https://forum.multitheftauto.com/viewtopic.php?f=108&t=46338

Maybe someone can tell me if this is correct way to do it:

I create a socket on server A with the address of server B and vice versa.

If I write data to the socket on server A now, shouldn't server B receive it?

Bonsai

You first create a socket > Bind the destination address to it > Now if it is server then you listen on the TCP socket otherwise you connect to server if it is client > Accept connections on server side > establish the connection client to server > Now you can send/ receive packets. You must open the TCP port for server side and you can't send/receive on the same port as 22005 or 22003 because they are used by MTA so choose a different port like 66553 or something else.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...