Example #1
0
void init (void)
{
	char nick[1024], addr[1024];
	NET_DRIVERLIST drv;
	int x;

	drv = net_driverlist_create();
	net_driverlist_clear (drv);
	net_driverlist_add (drv, netdriver);

	if (!net_initdrivers (drv)) {
		printf("Error initialising driver.\n");
		exit (1);
	}

	printf ("Enter target address: ");
	fgets (addr, 1024, stdin);
	while (strchr(addr,'\n')) *strchr(addr,'\n')=0;

	printf ("Enter nickname: ");
	fgets (nick, 10, stdin);
	while (strchr(nick,'\n')) *strchr(nick,'\n')=0;

	if (!(conn = net_openconn (netdriver, NULL))) {
		printf ("Unable to open conn.\n");
		exit (2);
	}

	printf ("Connecting to %s ", addr);
	fflush (stdout);
	
	x = net_connect_wait_cb (conn, addr, callback);
	if (x) {
		if (x > 0)
			puts (" -- user aborted.");
		else
			puts (" -- error occured.");
		net_closeconn (conn);
		exit (3);
	}
	puts (" -- connected.");
	
	{
		char buffer[100];
		sprintf (buffer, "/nick %s", nick);
		net_send_rdm (conn, buffer, strlen (buffer));
	}
}
Example #2
0
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;
}
Example #3
0
/* __libnet_internal__conns_exit:  (internal)
 *  Internal shutdown routine.
 */
void __libnet_internal__conns_exit() {
	while (__libnet_internal__openconns->next) net_closeconn(__libnet_internal__openconns->next->conn);
	free (__libnet_internal__openconns);
	__libnet_internal__openconns = NULL;
	MUTEX_DESTROY(__libnet_internal__openconns);
}