All pastes #11043 Raw Edit

Anonymous

public text v1 · immutable
#11043 ·published 2005-05-05 16:30 UTC
rendered paste body
/***************************************************************************
 *            cron_client.c
 *
 *  Wed May  4 20:03:01 2005
 *  Copyright  2005  tw03d024
 *  Email tw03d024@technikum-wien.at
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
#include <string.h>

#define KEY 0410257051
#define PERM 0666
#define MAX_DATA 255

typedef struct
{
long mType;
char mText[MAX_DATA];
} message_t;

int main(int argc, char *argv[])
{
    message_t msg;
    int msgid = -1;
    char command[MAX_DATA];
    char time[5];
    char *buffer;
    int i;
    
    if (argc < 2)
    {
      fprintf(stderr,"Usage: %s hh:mm program [arg1] [agr2...argn]\n",argv[0]);
      return EXIT_FAILURE;
      }
   
	for(i=2; i<argc; i++)
	{
		strcat(command, argv[i]);
		strcat(command, " ");
	}
    
    buffer = argv[1];
    msg.mType = *buffer;
    
   /* Message Queue oeffnen */
   if( (msgid = msgget(KEY,PERM))==-1 )
   {
      /* error handling */
      fprintf(stderr,"%s: Can't access message queue\n",argv[0]);
      return EXIT_FAILURE;
      }
	/*Kommandozeile ins struct schreiben*/
	strncpy(msg.mText,command,MAX_DATA);
	
	fprintf(stdout, "Message sent: %d %s\n", (int)msg.mType, msg.mText);
	
     /* Nachricht verschicken */
     if (msgsnd(msgid,&msg,sizeof(msg)-sizeof(long),0) == -1)
     {
         /* error handling */
         fprintf(stderr,"%s: Can't send message\n",argv[0]);
         return EXIT_FAILURE;
      }
   
   fprintf(stdout, "Message sent: %d %s\n", (int)msg.mType, msg.mText);
   return EXIT_SUCCESS;
}

/***************************************************************************
 *            cron_server.c
 *
 *  Wed May  4 20:03:01 2005
 *  Copyright  2005  tw03d024
 *  Email tw03d024@technikum-wien.at
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
#include <string.h>
#include <signal.h>

#define KEY 0410257051
#define PERM 0666
#define MAX_DATA 255

typedef struct
{
long mType;
char mText[MAX_DATA];
} message_t;

int main(int argc, char *argv[])
{
    message_t msg;
    int msgid = -1;
    time_t start, stop;
    double diff;
    
    start = time(NULL);
    
    if ( (msgid = msgget( KEY,PERM | IPC_CREAT | IPC_EXCL  )) == -1 )
    {
         fprintf(stderr, "%s: MessageQueue konnte nicht erzeugt werden!", argv[0]);
         return EXIT_FAILURE;
         }
    
    diff=difftime(stop=time(NULL),start);
    
    while(1)
    {
    	fprintf(stdout, "Zeit: %d\n", (int)diff);
	fflush(stdout);

   	while((diff=difftime(stop=time(NULL),start)) != 5);
         {
              if (msgrcv(msgid,&msg,sizeof(msg)-sizeof(long), msg.mType, 0) == -1) 
              {
                   fprintf(stderr, "%s: Fehler", argv[0]);
                   return EXIT_FAILURE;
              }
	      	fprintf(stdout, "Nachricht: %s %d\n", msg.mText, (int)msg.mType);
		fflush (stdout);
		
	}	
    }
    
    
  system("PAUSE");	
  return 0;
}