Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <getopt.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <signal.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/un.h>
- int endOfDesc;
- char myPath[20] = {0};
- char socketPath[30] = {0};
- struct sockaddr_un sockAddrConnection;
- int socketPrivate;
- int myNumber;
- typedef struct{
- char c;
- struct timespec stempel;
- }MessageToArch;
- void readOption(int argc, char* argv[]){
- char c;
- char* end;
- while((c=getopt(argc, argv, "z:f:")) != -1){
- switch(c){
- case 'z':
- endOfDesc = strtol(optarg, &end, 10);
- break;
- case 'f':
- memcpy(myPath, optarg, 20);
- // printf("%s\n", myPath);
- break;
- }
- }
- }
- void function(int sigNum, siginfo_t* s, void* p){
- MessageToArch secretMessage;
- // memset(secretMessage, 0, sizeof(MessageToArch));
- read(endOfDesc, &(secretMessage.c), 1 );
- printf("%c\n", secretMessage.c);
- clock_gettime(CLOCK_REALTIME, &secretMessage.stempel);
- int wasSendCorrect = sendto(socketPrivate, &secretMessage, sizeof(MessageToArch), 0,
- (struct sockaddr*)& sockAddrConnection, sizeof(struct sockaddr_un));
- if(wasSendCorrect == -1){
- perror("/Robotnik/:Message wasn't send becouse:");
- }
- }
- void setPrivateSocket(){
- memset(&sockAddrConnection, 0, sizeof(struct sockaddr_un));
- memset(socketPath, 0, sizeof(char)*20);
- sockAddrConnection.sun_family = AF_UNIX;
- sprintf(socketPath, "/tmp/private/%s", myPath);
- memcpy(sockAddrConnection.sun_path+1, socketPath, 25);//ile
- printf("sciazkoa: %s\n", sockAddrConnection.sun_path+1);
- socketPrivate = socket(AF_UNIX,SOCK_DGRAM ,0);
- sleep(2);
- if( connect(socketPrivate, (struct sockaddr*)& sockAddrConnection, sizeof(sockAddrConnection))){
- perror("/Robotnik/:Something wrong with connect: ");
- close(socketPrivate);
- // exit(0);
- }
- }
- int main(int argc, char* argv[]){
- readOption(argc, argv);
- struct sigaction sigAction;
- memset(&sigAction, 0, sizeof(struct sigaction));
- sigAction.sa_sigaction = &function;
- sigAction.sa_flags = SA_SIGINFO;
- sigaction(SIGALRM, &sigAction, NULL);
- setPrivateSocket();
- while(1){};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement