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)); } }
int main (int argc, char **argv) { NET_CONN *listen, *conn = NULL; NET_CHANNEL *chan; char remote[NET_MAX_ADDRESS_LENGTH], buf[NET_MAX_ADDRESS_LENGTH]; char *p, c; int server = -1; if (argc > 1) { if (!strcmp (argv[1], "server")) server = 1; else if (!strcmp (argv[1], "client")) server = 0; } if (server == -1) { puts ("Pass `server' or `client' on the command line."); return 1; } net_init(); net_detectdrivers(net_drivers_all); net_initdrivers(net_drivers_all); if (server) { listen = net_openconn(DRIVER, ""); net_listen(listen); while (!conn) conn = net_poll_listen(listen); } else { conn = net_openconn(DRIVER, NULL); if (net_connect_wait_time(conn, ADDRESS, 5) != 0) { printf("Error connecting\n"); return 1; } } chan = net_openchannel(DRIVER, NULL); p = net_getlocaladdress(chan); net_send_rdm(conn, p, strlen(p) + 1); while (!net_query_rdm(conn)) ; net_receive_rdm(conn, remote, sizeof remote); printf ("Address before fixing: %s\n", remote); if (net_fixupaddress_conn(conn, remote, buf) != 0) { printf("Didn't work\n"); return 1; } printf ("Address after fixing: %s\n", buf); printf("assigning target: %s\n", buf); net_assigntarget(chan, buf); do { c = fgetc(stdin); if (c == '1') net_send(chan, "** Channel", 11); else if (c == '2') net_send_rdm(conn, "** Conn", 8); while (net_query(chan)) { net_receive(chan, buf, sizeof buf, NULL); printf("%s\n", buf); } while (net_query_rdm(conn)) { net_receive_rdm(conn, buf, sizeof buf); printf("%s\n", buf); } } while (c != 'q'); return 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; }