User Tools

Site Tools

한국어

moacon:network_sample_program:index

Network sample program

The following program implements a simple web server on the MOACON. It will listen on port 8080 and respond to any connection with “Hello from the MOACON.”

#include "moacon500.h"
#include <string.h>
void cmain(void)
{
    //Configure the network settings
    u8 GatewayIP[]={192,168,0,1};
    u8 SubnetMask[]={255,255,255,0};
    u8 MacAdr[]={0,0,34,53,12,0};
    u8 DeviceIp[]={192,168,0,12};
    netBegin(GatewayIP, SubnetMask, MacAdr, DeviceIp);
    //Use socket 0
    u8 socket = 0;
    //Ensure socket is disconnected and closed
    disConnect(socket);
    close(socket);
    //Keep track of the connection status
    u8 currentStatus = 0xFF;
    u8 lastStatus = 0xFF;
    while (1) //Run forever
    {
         currentStatus = netStatus(socket);
         if (currentStatus != lastStatus) //If connection status changes
         {
             lastStatus = currentStatus;
             switch(currentStatus)
             {
             case SOCK_INIT: //If not listening, start listening
                 printf("Init\r\n");
                 socketOpen(socket,8080);
                 listen(socket);
                 break;
             case SOCK_CLOSED: //If closed, start listening
                 printf("Closed\r\n");
                 socketOpen(socket,8080);
                 listen(socket);
                 break;
             case SOCK_ESTABLISHED: //If connection established, respond
                 printf("Established\r\n");
                 netPrint(socket, "HTTP/1.0 200 OK\r\n");
                 netPrint(socket, "Content-Type: text\r\n");
                 char* hello = "Hello from the MOACON";
                 netPrint(socket, "Content-Length: %d\r\n\r\n", strlen(hello));
                 netPrint(socket, "%s", hello);
                 break;
            case SOCK_LISTEN: //If listening, just wait
                 printf("Listening\r\n");
                 break;
            case SOCK_CLOSE_WAIT: //If client disconnects, disconnect
                 printf("Closing\r\n");
                 disConnect(socket);
                 break;
            default:
                 break;
            }
            delay(10); //Wait for 10 milliseconds
         }
     }
}

The MOACON will begin listening for connections:

Enter the URL to the MOACON socket listening on port 8080

The MOACON responds and begins listening for a new connection.

MOACON home

moacon/network_sample_program/index.txt · Last modified: 2021/09/09 11:01 by COMFILE Technology