Sockets

Top  Previous  Next

TCP/IP Sockets provide a mechanism for data exchange between programs running on computers connected by a LAN or WAN. The Socket Object allows you to send and receive data to and from an external program via TCP/IP. The external program can be another instance of FreeFlyer, or any other program capable of communicating via socket. Sockets in FreeFlyer can run in server or client mode. To begin data exchange, the program acting as the server (either FreeFlyer or an external program) starts and listens for a connection on a designated port. One or more clients (these can also be FreeFlyer or other external programs) may then connect to the server port. Once a connection is established, data exchange is bi-directional; the client and server distinction only applies to the original connection mode. FreeFlyer may be executed in either client or server mode and two instances of FreeFlyer may exchange data.

 

The following Sample Mission Plans (included with your FreeFlyer installation) demonstrate the use of the Socket object:

 

Interfacing with External Resources Samples

Client Socket

Download TLEs

Server Socket

 

 

FreeFlyer as Server


A FreeFlyer server will begin listening on the user-specified port number for connections from remote client programs when the Open command is executed. The client program(s) could be additional instances of FreeFlyer, or other external programs. FreeFlyer will wait for a specified number of seconds for a remote client to connect. If none connects by the timeout time, an error will result. However, if a remote client connects, a second TCP/IP socket is automatically created, which binds to the remote client’s TCP/IP socket. Now there are two sockets – one listening for new connections, and one bound to the remote client. At this point the Open command returns, and the Mission Plan begins executing the next command. The listening TCP/IP socket still exists and is able to accept additional connections from other clients while the current FreeFlyer Mission Plan is running. Each time a remote client connects to the server, a new connected TCP/IP socket is created, one per remote client. FreeFlyer may be used to broadcast data to multiple clients by using the Send command with a server socket having multiple remote client connections. A Socket object configuration can also be imported from a FreeFlyer formatted object file.

 

 

FreeFlyer as Client


A FreeFlyer client will create a socket and attempt to connect to the server (specified by the remote machine name or IP address on which the server program resides, and the port number on which the server is listening) when the Open command is executed. A failure to find the appropriate server will result in an error. Once a connection has been established, bi-directional data exchange may begin. The TCP/IP socket is disconnected when the Close command is executed.

 

 

Send and Receive Commands


Data exchange in FreeFlyer is accomplished using the Send and Receive commands. FreeFlyer allows the user to exchange Variables, Strings, Arrays and StringArrays with the Send and Receive commands.

 

 

What is Endian?


Endian refers to the byte ordering of a computer. Exchanging binary data across multiple computer platforms (e.g. PC, Sun, etc.) may be complicated by different byte order (endian) for the binary representation of floating point and integer numbers. Consult the specific computer platform documentation for information on byte ordering.

 

 

Sending and Receiving ASCII


Avoid separation or termination characters in data when sending and receiving ASCII. For example, don't put spaces in strings when the separation character is a space - the Receive won't know where the string ends and the next field begins. When receiving in ASCII mode, the model used is as follows:

 

1.For each data element, except for the last, data is read from the stream up to the next occurrence of the separation character(s), which are then also read.

2.The read data is converted and assigned to the data element.

3.For the last data element, data is read from the stream up to the next occurrence of the termination character(s), which are then also read.

 

Any occurrences of separation characters within the data are ignored. This means that you can get away with having separation characters in the data if you are receiving only a single data element and the termination character(s) are different than the separation character(s).

 

 

Example: Two FreeFlyer instances communicating with each other


In this example, ServerSocket.MissionPlan and ClientSocket.MissionPlan are each opened in a separate instance of FreeFlyer. ServerSocket is run prior to running the ClientSocket.

 

The ServerSocket has a Socket that is awaiting data from the Client. Once the Server receives the data, it performs data manipulation and sends it back to the Client. The ClientSocket has a Socket that will be used to send and receive data from the Server. The Mission Plan propagates a Spacecraft, and sends the SMA, Eccentricity, and Inclination to the Server through the Socket, and waits to receive the manipulated data from the Server through the Socket. Once the Client receives the manipulated data from the Server, it reports the data to the screen and steps the Spacecraft to the next propagation step.

 

For more information on the example Mission Plans discussed here, see the Interfacing with External Resources section of the Sample Mission Plans guide. The ClientSocket and ServerSocket Mission Plans are discussed there as well.

 

ServerSocket (run first)

 

Open ServerSocket;

 

SocketFlag = ServerSocket.IsOpen;

 

While (SocketFlag == 1);

 

  Receive Variable1, Variable2, Variable3, SocketFlag from ServerSocket;

  Update ReceivedData;

 

  // Edit the data

  Variable1 = Variable1 + 10;

  Variable2 = Variable2 + 0.01;

  Variable3 = Variable3 + 1;

 

  Send Variable1, Variable2, Variable3 to ServerSocket;

 

End;

 

Close ServerSocket;

 

Server Socket Object Editor

Server Socket Object Editor

 

ClientSocket (run second)

 

Open ClientSocket;

 

While (LEO.ElapsedTime < TIMESPAN(3 days));

 

  // Send and Receive Data

 

  SMA = LEO.A;

  Ecc = LEO.E;

  Inc = LEO.I;

 

  Send SMA, Ecc, Inc, SocketFlag to ClientSocket;

  Receive SMA, Ecc, Inc from ClientSocket;

 

  Update ReceivedData;

  Step LEO;

 

End;

 

SocketFlag = 0;

 

// Send final message to socket

Send SMA, Ecc, Inc, SocketFlag to ClientSocket;

 

Close ClientSocket;

 

Client Socket Object Editor

Client Socket Object Editor

 

See Also


Socket Properties and Methods

Open Command

Close Command

Send Command

Receive Command