TCP/IP connection using URBI

The following example demonstrates how both the Server and Socket object work. This simple example will establish a dialog between server and client.

Run this code in URBI server-side.

do(Global) {
 
  // Declare variables
  var mySocket = Socket.new();
  var myServer;
 
  // Server initialisation function
  function MyServerInit(var host, var port){
    try{
      Global.myServer=Server.new();
 
      at (Global.myServer.connection?(var socket)) {
        at (socket.received?(var data)){
          var myData=data.split(" ");
          if (myData[0] == "INSTR1") {
            if (myData.size < 3) return;              
            switch (myData[1]) {   
              case "PAR1":  echo("Server>> Received INSTR1 with PAR1="+myData[2].asFloat);   
              case "PAR2":  echo("Server>> Received INSTR1 with PAR2="+myData[2].asFloat);
              case "PAR3":  echo("Server>> Received INSTR1 with PAR3="+myData[2].asFloat);
            }; 
          };
          if (myData[0] == "INSTR2") echo("Server>> Received INSTR2");
        };
        Global.mySocket=myServer.sockets[myServer.sockets.size-1]; 
        echo("Server>> Client connected.");
      };
      Global.myServer.listen(host, port);
      echo("Server>> Started.");
    } catch(var e){
      echo("Server>> Error during initialisation: %s" % e.message);
      return;
    };
  }|{};
 
 
  // Function to write back sth. to client
  function SendToClient(var myData){
    try{
      if (mySocket.isConnected) Global.mySocket.write(myData);
    } catch(var e){
      echo("Server>> Error during writing to Client: %s" % e.message);
      return;
    };
 
  }|{};
};

Run this code in URBI client-side or use your own software to connect to server.

do(Global) {
 
  // Declare variables
  var mySocket = Socket.new();
 
  // Server initialisation function
  function MyConnectionInit(var host, var port){
    try{
      Global.mySocket=Socket.new();
      Global.mySocket.connect(host, port);
      at (mySocket.received?(var data)){
        echo("Client>> Received: " + data);
      };
      echo("Client>> Connected.");
    } catch(var e){
      echo("Client>> Error during connection: %s" % e.message);
      return;
    };
  }|{};
 
  // Function to write back sth. to client
  function SendToServer(var myData){
    try{
      if (mySocket.isConnected) Global.mySocket.write(myData);
    } catch(var e){
      echo("Client>> Error during writing to Server: %s" % e.message);
      return;
    }; 
  }|{};
};

Now, initialise server using function above.

MyServerInit("localhost", "10000");

Connect client or your own software.

MyConnectionInit("192.168.0.100", "10000");

To write from client to server use this

SendToServer("INSTR1 PAR1 17");

To write from server to client

SendToClient("Got it!");

More examples you can find in the URBI documentation LINK and here LINK.

 

 

EMYS and FLASH are Open Source and distributed according to the GPL v2.0 © Rev. 0.9.2, 23.11.2017

FLASH Documentation