Skip to content

openUDP

Jason Watkins edited this page Jul 6, 2016 · 13 revisions

Sets up a new connection to an X-Plane Connect plugin running in X-Plane. In object oriented languages (currently Java and Python), the constructor for the client object takes the place of this function.

In C, the openUDP function drops both port parameters and automatically selects the ports instead. It is otherwise identical to the aopenUDP function.

In Java, Several overloads of the constructor are available. Only the most detailed version is represented here.

Syntax

Language Signature
C XPCSocket aopenUDP(const char *xpIP, unsigned short xpPort, unsigned short port)
MATLAB [socket] = openUDP(xpHost, xpPort, port, timeout)
Java XPlaneConnect(String xpHost, int xpPort, int port, int timeout)
Python XPlaneConnect(xpHost = 'localhost', xpPort = 49009, port = 0, timeout = 100)
Parameters

xpIP (C): The IPV4 address of the machine running X-Plane in dotted quad notation.

xpHost: The hostname of the machine running X-Plane. In MATLAB and Python this parameter is optional, and will default to the local machine.

xpPort: The port on which the XPC plugin is listening. Usually 49007. In MATLAB and Python this parameter is optional and will default to 49007.

port: The port which will be used to send and receive data. This should usually be ommited or set to 0 to allow the operating system to automatically choose a port. In MATLAB and Python this parameter is optional and will default to 0.

timeout (Java & Python): The period (in milliseconds) after which read attempts will fail.

Return value

C & MATLAB: A structure describing an opened socket.

Java & Python: An initialized instance of the client.

Exceptions

C Error Code Java Exception Python Error Description
  •       |SocketException     |OSError     |The incoming port could not be bound
    
  •       |UnknownHostException|ValueError  |The specified X-Plane host could not be resolved
    

In the C client, any error encountered at this point is considered to be fatal. The client will print an error message to stdout and exit.

Example

C
#include "xplaneConnect.h"

// Open a connection to X-Plane
XPCSocket sock = openUDP("127.0.0.1", 49007, 49008);
// Or, let the client port be chosen automatically
//XPCSocket sock = aopenUDP("127.0.0.1", 49007);

// Use the client here

// Close the connection when finished.
closeUDP(sock);
MATLAB
import XPlaneConnect.*;

% Open the client
socket = openUDP('127.0.0.1', 49007, 49008);
% Or, open a client using the default parameters
%socket = openUDP();

% Use the client here

% Close the client when finished.
closeUDP(socket);
Java
import gov.nasa.xpc.XPlaneConnect;

// Open a connection to X-Plane by creating a new client.
try(XPlaneConnect xpc = new XPlaneConnect())
{
    // Use the client here
} // Java automatically closes the connection at the end of the try block
Python
import xpc

# Open a connection to X-Plane by creating a new client.
with xpc.XPlaneConnect() as client:
    # Use the client here
# Python automatically closes the connection at the end of the with block.