1 Reply Latest reply on Apr 20, 2009 5:43 PM by Wilco_Ludikhuize

    Sockets, flash lite, C

      Ok, here is the deal:

       

      I'm writing a Flash Lite application for an embedded linux system where flash acts as the GUI.

      Since Flash hasn't got the ability to use shell commands I wrote a simple C server application which executes commands and sends back the results.

       

      The server application in C is working and sends back the results to the C client application.

      The problem is I cannot get the actionscript client to work.

       

      I think the majority of the problem lies with the fact that I'm an absolute noob when it comes to action script programming, have experience in a multitude of other languages.

       

      I'd like to know if it's possible to use sockets to connect to C.

      And if so how.

      Also if there are other solutions to this problem, I'd like to know this.

      I have done some research on how to execute commands from flash using fscommand but this seems to have a lot of restrictions.

       

      actionscript code for test in first frame:

       

      var socket:XMLSocket = new XMLSocket();

      socket.connect("127.0.0.1",1800);

      socket.send("ls");

       

      usage c server:

      ./server 1800

       

      code for c:


      /* A simple server in the internet domain using TCP
         The port number is passed as an argument */
      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/socket.h>
      #include <netinet/in.h>


      void error(char *msg);
      void initSocket(int port);


      void error(char *msg)
      {
          perror(msg);
          exit(1);
      }


      void initSocket(int port)
      {
           int sockfd, newsockfd, portno, clilen;
           char buffer[256];
           struct sockaddr_in serv_addr, cli_addr;
           int n;
          FILE *in;
          extern FILE *popen();
          char buff[512];



           sockfd = socket(AF_INET, SOCK_STREAM, 0);
           if (sockfd < 0)
              error("ERROR opening socket");
           bzero((char *) &serv_addr, sizeof(serv_addr));
           portno = port;
           serv_addr.sin_family = AF_INET;
           serv_addr.sin_addr.s_addr = INADDR_ANY;
           serv_addr.sin_port = htons(portno);
           if (bind(sockfd, (struct sockaddr *) &serv_addr,
                    sizeof(serv_addr)) < 0)
                    error("ERROR on binding");
           listen(sockfd,5);
           clilen = sizeof(cli_addr);
           //endless loop to keep listening
           while(1)
           {
                newsockfd = accept(sockfd,
                            (struct sockaddr *) &cli_addr,
                            &clilen);
                if (newsockfd < 0)
                     error("ERROR on accept");
                bzero(buffer,256);
                n = read(newsockfd,buffer,255);
                if (n < 0) error("ERROR reading from socket");
                printf("Here is the command: %s\n",buffer);
              
                /* popen creates a pipe so we can read the output
                of the program we are invoking */
                if (!(in = popen(buffer, "r"))) {
                    exit(1);
                }
                /* read the output of netstat, one line at a time */
                while (fgets(buff, sizeof(buff), in) != NULL ) {
                    n = write(newsockfd,buff,sizeof(buff));
                    if (n < 0) error("ERROR writing to socket");
                }
                /* close the pipe */
                    pclose(in);


            close(newsockfd);
           }
      }


      int main(int argc, char *argv[])
      {
           if (argc < 2) {
               fprintf(stderr,"ERROR, no port provided\n");
               exit(1);
           }
           //initialize socket and listen on port
           initSocket(atoi(argv[1]));
           return 0;
      }