/* net_connect_wait_cb: * See `net_connect_wait_*'. This version calls the given * callback while waiting, and aborts if the callback returns * nonzero. */ int net_connect_wait_cb (NET_CONN *conn, const char *addr, int (*cb)(void)) { int x = 0, y; if (net_connect (conn, addr)) return -1; do { if (cb) x = cb(); y = net_poll_connect (conn); safe_sleep (1); } while ((x == 0) && (y == 0)); if (y < 0) return -1; if (y > 0) return 0; return 1; }
/* net_connect_wait_time: * See `net_connect_wait_*'. The event in this case is that * `time' seconds have passed. */ int net_connect_wait_time (NET_CONN *conn, const char *addr, int time) { int y; if (net_connect (conn, addr)) return -1; do { time--; y = net_poll_connect (conn); safe_sleep (1); } while ((time >= 0) && (y == 0)); if (y < 0) return -1; if (y > 0) return 0; return 1; }
void *pv_messclient_send(){ NET_CONN *conn = NULL; char message[MAX_MESS_LEN + 2]; int status = 0; int message_len = 0; int conn_attempts = 0; /* Send Sign in Massage*/ conn = net_openconn (DRIVER,NULL); if (!conn) { fprintf (stderr,"Error opening conn.\n"); exit(ERROR); } if (net_connect (conn,server_IP_port) != 0) { fprintf (stderr,"Error initiating connection.\n"); exit(ERROR); } /* Try to connect */ for(conn_attempts=0;conn_attempts < CONN_ATTEMPTS; conn_attempts++ ){ status = net_poll_connect(conn); sleep(1); if(status > 0){ break; }/*if*/ }/*for*/ /* if fail to coonnect */ if(conn_attempts == CONN_ATTEMPTS ){ printf("# Server unreachable !!!\n"); printf("Good bye !!!\n"); exit(0); }/*if*/ if (status < 0) { fprintf(stderr,"Error connecting."); exit(ERROR); } /* Prepare Message */ message[0]='S'; /*Sign in*/ memcpy(message+1,&user_data,sizeof(user_data)); /* Send a message */ net_send_rdm (conn,message,sizeof(user_data) + 1); /* Close the conn. */ net_closeconn (conn); printf("Enter text to send. Press Enter to quit.\n"); /* Print Uset ID */ printf("# IP : %s PID :%d :", user_data.local_IP, user_data.pid ); fflush(stdout); while(1){ /* Let user enter something. */ message_len = read(0,message+1+sizeof(user_data),MAX_MESS_LEN + 1); if( message_len == -1){ fprintf(stderr,"# Fatal Error!\n"); exit(ERROR); }/*if*/ if( message_len > MAX_MESS_LEN ){ printf("# Too long message ! Message is discarded !\n"); continue; }/*if*/ conn = net_openconn (DRIVER, NULL); if (!conn) { fprintf (stderr,"Error opening conn.\n"); exit(ERROR); } if (net_connect (conn,server_IP_port) != 0) { fprintf (stderr,"Error initiating connection.\n"); exit(ERROR); } /* Try to connect */ for(conn_attempts=0;conn_attempts < CONN_ATTEMPTS; conn_attempts++ ){ status = net_poll_connect(conn); sleep(1); if(status > 0){ break; }/*if*/ }/*for*/ /* if fail to coonnect */ if(conn_attempts == CONN_ATTEMPTS ){ printf("# Server unreachable !!!\n"); printf("Good bye !!!\n"); exit(0); }/*if*/ /* do status = net_poll_connect (conn); while (status == 0); */ if (status < 0) { fprintf (stderr,"Error connecting."); exit(ERROR); } /* Put the Client ID in the message */ memcpy(message+1,&user_data,sizeof(user_data)); /* Logout */ if( message_len == 1){ message[0]='L'; /* Logout */ /* Send a message */ net_send_rdm (conn,message, sizeof(user_data) + 1); printf("# Good Bye !!!\n"); exit(0); } else{ message[0]='M'; /* Message */ /* Send a message */ message[message_len + 1 + sizeof(user_data)] = '\0'; net_send_rdm (conn, message, message_len + 1 + sizeof(user_data) + 1); } /* Close the conn. */ net_closeconn (conn); } /* while */ return OK; }