c - Implementation of custom message logger for windows : reports 10049 when ntwk cable unplugged -


trying develop simple/small syslog server windows. implementation store logs in same machine 2 other processes of same machine.

wanted similar linux implementation.started use udp datagram send data between processes.

it working expected. however, has been noticed when network cable unplugged messages not reaching server client process application.

the client process reports 10049 error (when network cable unplugged) request guidance how make work between local process. since, process run in local machine.

server end listen code:

int main(int argc,char *argv[]) {     socket s;     file *fp;     struct sockaddr_in server, si_other;     int slen , recv_len;     char buf[buflen];     wsadata wsa;     struct stat sts;     char filenamewithpath[512]={'\0'};     int status;      printf("argc %d\n",argc);     char a[10];     strcpy(logbasepath,"c:\\log\\");     if(argc == 1)     {         //parsesyslogconfiguration();         if (logginglevel > 4)             logginglevel=debug_log;      }     else     {         memset(a,0,sizeof(a));         strncpy(a,argv[1],1);         int isnum=isdigit(a[0]);         printf("len argv : %d , isdigit : %d\n",strlen(argv[1]),isnum);         //parsesyslogconfiguration();         if(strlen(argv[1]) == 1 && isnum == 1)         {              logginglevel = atoi(argv[1]);             if (logginglevel > 4)                 logginglevel=debug_log;              printf("current log level initaited : %d",logginglevel);         }         else         {             logginglevel=debug_log;             printf("invalid arg (%s)for syslog server setting log level debug\n",argv[1]);             printf("values can : 0-4 \n");         }     }      if(buf[strlen(logbasepath)-1] != '\\')     {         printf("adding end slash\n");         strncat(logbasepath,"\\",1);     }     else         printf("not adding end slash\n");      //g_thread_init(null);     //write_mutex = g_mutex_new();     slen = sizeof(si_other) ;     getdatetime(&dateinfo);     strcpy(logfilename,"syslog");     memset(filenamewithpath,0,sizeof(filenamewithpath));     strcat(filenamewithpath,logbasepath);     strcat(filenamewithpath,logfilename);     //strcat(filenamewithpath,logfilename,logbasepath,"syslog");     status = stat(filenamewithpath, &sts);     if(errno == enoent)     {         fp = fopen(filenamewithpath, "a+");         logmessage(fp,dateinfo.syslogtimeformat,"logrotate","[origin software='test']",0);         fclose(fp);     }     getdatetime(&dateinfo);     setsyslogfiledate(logbasepath);      //initialise winsock     printf("\ninitialising winsock...");     if (wsastartup(makeword(2,2),&wsa) != 0)     {         printf("failed. error code : %d",wsagetlasterror());         exit(exit_failure);     }     printf("initialised.\n");      //create socket     if((s = socket(af_unix , sock_dgram , 0 )) == invalid_socket)     {         printf("could not create socket : %d" , wsagetlasterror());     }     printf("socket created.\n");      //prepare sockaddr_in structure     server.sin_family = af_unix;     server.sin_addr.s_addr = inaddr_any;     server.sin_port = htons( port );     //bind     if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == socket_error)     {         printf("bind failed error code : %d" , wsagetlasterror());         exit(exit_failure);     }     puts("bind done");     //msgqueueid = g_queue_new();     //g_queue_init(msgqueueid);   //  syslogfilewritethreadid = g_thread_create(processlogmsgfunc, null, true, &error);    // syslogrotatethreadid = g_thread_create(syslogrotatemonitor, null, true, &error);     //keep listening data     while(1)     {         fflush(stdout);         memset(buf,'\0', buflen);         if ((recv_len = recvfrom(s, buf, buflen, 0, (struct sockaddr *) &si_other, &slen)) == socket_error)         {             printf("recvfrom() failed error code : %d" , wsagetlasterror());             exit(exit_failure);         }          logstruct *qmsg = null;         memset(&message,0,sizeof(logstruct));         qmsg = malloc(sizeof(logstruct));         memset(qmsg,0,sizeof(logstruct));         memcpy(qmsg,&buf,sizeof(logstruct));         postmessageq(qmsg);      } //  g_mutex_free(write_mutex); //  g_queue_free(msgqueueid);     closesocket(s);     wsacleanup(); //    g_thread_join(syslogfilewritethreadid); //    g_thread_join(syslogrotatethreadid);     return 0; } 

clent side implementation:

    #include<stdio.h>     #include<winsock2.h>     //#include <glib.h>      #define debug_log 0     #define trace_log 1     #define warning_log 2     #define error_log 3     #define fatal_log 4      #pragma comment(lib,"ws2_32.lib") //winsock library      #define server "127.0.0.1"  //ip address of udp server     #define buflen 4096  //max length of buffer     #define port 514   //the port on listen incoming data      #define run_server 1      struct sockaddr_in si_other;     int s;       gmutex *write_mutex = null;      static char applogname[128] = {'0'};      typedef enum{         loginit,         logmessage,         logtrace,         logexit     }logcommand;      typedef struct     {         logcommand command;         int loglevel;         int pid;         char appname[128];         char loggermessage[3200];     }logstruct, *lplogstruct;        int log_init(char *infoname,int level)     {         int ret=0;         wsadata wsa;         //if(write_mutex == null)         //{             //g_thread_init(null);             //write_mutex = g_mutex_new();              //initialise winsock                 if(strlen(infoname) == 0 && strlen(applogname) == 0)                 {                     strcpy(applogname,"atm");                 }                 else                 {                     strcpy(applogname,infoname);                 }                   //create socket                 if ( (s=socket(pf_inet, sock_dgram, ipproto_udp)) == socket_error)                 {                     printf("socket() failed error code : %d" , wsagetlasterror());                     //exit(exit_failure);                     return -1;                 }                 //int nopt=1;                 //setsockopt(s, sol_socket, so_broadcast, (char*)&nopt, sizeof(int));                 //setsockopt(s, sol_socket, so_rcvtimeo, (char*)&nopt, sizeof(int));                   //setup address structure                 memset((char *) &si_other, 0, sizeof(si_other));                 si_other.sin_family = af_inet;                 si_other.sin_port = htons(port);                 si_other.sin_addr.s_un.s_addr = inaddr_any;                 //si_other.sin_addr.s_un.s_addr = inet_addr(server);                // si_other.sin_addr.s_un.s_addr = inaddr_broadcast ;                // si_other.sin_addr.s_addr = inaddr_any;           //}         return 0;     }      void log_exit()     {         run_server=0;         closesocket(s);         wsacleanup();     }      void ms_log(char *buf,int priority)     {         debug_log(buf,priority);     }      void debug_log(char *buf,int priority)     {         //g_mutex_lock(write_mutex);         int ret = 0;         logstruct log;         memset(&log,0,sizeof(logstruct));         log.command=logmessage;         log.loglevel=priority;         log.pid = getcurrentprocessid();         if(strlen(applogname))         {             strcpy(log.appname,applogname);         }         if(strlen(buf))         {             strcpy(log.loggermessage,buf);             ret=senddatapacket(&log , sizeof(logstruct));         }         //g_mutex_unlock(write_mutex);     }      int senddatapacket(logstruct *data , int datalength)     {         bool bresult;         dword cbbytes;         int slen;         slen=sizeof(si_other);         if (sendto(s, data, datalength , 0 , (struct sockaddr *) &si_other, slen) == socket_error)         {             printf("sendto() failed error code : %d" , wsagetlasterror());            return -1;         }         return 0;     }      int main(void)     {         char buf[buflen];         char message[buflen];         wsadata wsa;         logstruct log;         //start communication         printf("\ninitialising winsock...");         if (wsastartup(makeword(2,2),&wsa) != 0)         {             printf("failed. error code : %d",wsagetlasterror());             //exit(exit_failure);             return -2;         }         printf("initialised.\n");         log_init("testven",1);         while(run_server)         {             printf("enter message : ");             gets(message);             log.command = logmessage;             strcpy(log.appname,"testapp");             log.loglevel=debug_log;             log.pid=getcurrentprocessid();             strcpy(log.loggermessage,message);             senddatapacket(&log,sizeof(logstruct));             //send message         }         log_exit();         return 0;     } 

error code 10049 : wsaeaddrnotavail: cannot assign requested address.

the requested address not valid in context. results attempt bind address not valid local computer. can result connect, sendto, wsaconnect, wsajoinleaf, or wsasendto when remote address or port not valid remote computer (for example, address or port 0).

so can happen sendto well. remote address ip_addr_any not valid address more on cable plugout?

if on same machine, try 127.0.0.1 on server code well?


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -