Thursday, November 28, 2013

NTP Client

Linux version

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h >

int main(void)
{
    int sockfd = 0, n = 0, ii = 1;
    char recvBuff[1024];
    struct sockaddr_in serv_addr; 
    setvbuf(stdout, NULL, _IONBF, 0);
printf("Retrieving time, please wait ");
    memset(recvBuff, '0',sizeof(recvBuff));
    n=-1;

    while(n<=0)
    {
//    printf("====== attempt %d \n\n", ii);
    printf(".");
    
    if (ii == 50)
    {
   printf("\n");
   ii =0;
   }
    
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    } 

    serv_addr.sin_addr.s_addr = inet_addr("64.90.182.55");
//     serv_addr.sin_addr.s_addr = inet_addr("96.47.67.105");
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(13); 

       if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
       {
          printf("\n Error : Connect Failed \n");
          return 1;
       } 

       n = read(sockfd, recvBuff, sizeof(recvBuff)-1);
       close(sockfd);
       usleep(10000);
       ii++;
    }
      recvBuff[n] = 0;
      printf("%s\n",recvBuff);

    return 0;
}

Windows version

//============================================================================
// Name        : windows_time_client.cpp
// Author      : Norma Hermawan
// Version     :
// Copyright   : Your copyright notice
// Description : CONNECT TO REMOTE HOST (CLIENT APPLICATION)
//               Include the needed header files.
//               Don’t forget to link libws2_32.a to your program as well
//               2. To set the library
//               Bring up the properties box for the project.
//               Select all configurations.
//               In the linker section on the left handside - select Input and in the right hand side add
//               ws2_32.lib in the Additional Dependencies box

//============================================================================

#include "stdafx.h"
#include <winsock.h>
#include <iostream>

using namespace std;
#define TIMESERV_PORT  13
//#define TIMESERV_IP    "96.47.67.105" // this is time server IP
#define TIMESERV_IP    "64.90.182.55" // this is time server IP
//#define TIMESERV_IP    "128.138.188.172" // this is time server IP
//#define TIMESERV_IP    "211.233.40.78" // this is time server IP
//#define TIMESERV_IP    "203.160.128.178" // this is time server IP
//#define TIMESERV_IP    "113.20.31.30" // this is time server IP

int main() {
SOCKET sfd; //Socket handle
char buffer[80];
int n, ii =1;
time_t prev_stamp;

printf("Retrieving time, please wait ",ii);
do{
//    system ("ping 64.90.182.55");
//Start up Winsock…
// printf("Starting winsock ============================= attempt %d\n\n",ii);
// printf("Retrieving time, attempt %d\n\n",ii);
printf(".");
if (ii==50){
      printf("\n");
      ii = 0;
            }
WSADATA wsadata;

int error = WSAStartup(0x0202, &wsadata);

//Fill out the information needed to initialize a socket…
SOCKADDR_IN addr; //Socket address information

addr.sin_family = AF_INET; // address family Internet
addr.sin_port = htons (TIMESERV_PORT); //Port to connect on
addr.sin_addr.s_addr = inet_addr (TIMESERV_IP); //Target IP

// printf("Create socket\n");
sfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
// printf("Establishing Connection \n");
    int res = connect(sfd, (sockaddr*) &addr, sizeof(addr));
//    printf("Connectition is established \n");
memset(buffer, 0, sizeof(buffer)); //Clear the buffer
// printf("Receiving signal \n\n");
prev_stamp = time(NULL);
//Put the incoming text into our buffer
n = recv (sfd, buffer, sizeof(buffer)-1, 0);//) <= 0) &&((time(NULL) - prev_stamp) < 5))
    
closesocket(sfd);
WSACleanup(); //Clean up Winsock
// Sleep(10);
ii++;
}
while(n<=0);
printf("\nTime: ", n);
buffer[n] = 0;
printf("%s\n",buffer);

system("Pause");
return 0;
}

Tuesday, November 26, 2013

Time Slice Multitasking

This is a code example of time slice multitasking in C.

#include
#define KERNEL_SLEEP_US 1000
#define msleep(x) sleepc++; if(sleepc != x){return;}else{sleepc=0;}
int program_end = 0;

void task1(void) {
static int pc=0,sleepc=0;
switch (pc){
case 0: goto label0;
case 1: goto label1;
case 2: goto label2;
case 3: goto label3;
case 4: goto label4;
case 5: goto label5;
case 6: goto label6;
case 7: goto label7;
default: return;
}                                   // task code section
                                       // ========================================= 
label0:               /* a task code -> */ while(1){
pc=1; return; label1: /* a task code -> */   
pc=2; return; label2: /* a task code -> */   
pc=3; return; label3: /* a task code -> */   
pc=4; return; label4: /* a task code -> */   
pc=5; return; label5: /* a task code -> */   printf("Hello from task 1\n");
pc=6; return; label6: /* a task code -> */   msleep(250);  }
pc=7; return; label7: /* a task code -> */   
pc=8; return;                           // ========================================      
}

void task2(void) {
static int pc=0,sleepc=0,ii;
switch (pc){
case 0: goto label0;
case 1: goto label1;
case 2: goto label2;
case 3: goto label3;
case 4: goto label4;
case 5: goto label5;
case 6: goto label6;
case 7: goto label7;
default: return;
}                                   // task code section
                                       // ========================================= 
label0:               /* a task code -> */   for(ii=0;ii<5 font="" ii="">
pc=1; return; label1: /* a task code -> */     printf("<<-- --="" 2="" d="" font="" ii="" n="" task="">
pc=2; return; label2: /* a task code -> */     msleep(1000); };
pc=3; return; label3: /* a task code -> */   
pc=4; return; label4: /* a task code -> */   program_end = 1;
pc=5; return; label5: /* a task code -> */   
pc=6; return; label6: /* a task code -> */   
pc=7; return; label7: /* a task code -> */   
pc=8; return;                           // =========================================        
}

my_kernel(){
  usleep(KERNEL_SLEEP_US); // sleep 100 ms
}

int main(void)
{
  while(!program_end)
  {
  task1();
  task2();
  my_kernel();
  }
    return 0;
}