void parse_buf(send_info *send ,int newfd)//解析客户端传过来的字符串数据结构中的type 成员来匹配调用不同的函数。 { if(send->type == PUBLIC_CHAT)//群聊 { public_chat(send,newfd); } if(send->type == PRIVATE_CHAT)//私聊 { private_chat(send,newfd); } if(send->type == CLIENT_LOAD)//登陆验证 { server_check_login(send,newfd); } if(send->type == CLIENT_REGISTER)//注册 { register_new_client(send,newfd); } if(send->type == CLIENT_ONLINE)//获取所有在线用户 { get_all_online_clients(send,newfd); } if(send->type == CLIENT_EXIT)//客户退出 { client_exit(send,newfd); } }
/* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { int sock = *(int*)socket_desc; int read_size; char *mess; message msg; //Send some messages to the client mess = "Greetings! I am your connection handler\n"; write(sock , mess , strlen(mess)); mess = "Now type something and i shall repeat what you type \n"; write(sock , mess , strlen(mess)); //Receive a message from client while( (read_size = recv(sock , &msg , sizeof(msg) , 0)) > 0 ) { //Send the message back to client switch(msg.cmd){ case CMD_LOGIN: login(sock,msg); bzero(&msg,sizeof(msg)); break; case CMD_REGISTER: client_register(sock,msg); break; case CMD_SEND_TO: send_to(msg); break; case CMD_LIST_PEOPLE: who_is_online(sock); break; case CMD_EXIT: client_exit(sock,msg.user_name); //puts("Client disconnected"); //fflush(stdout); //free(socket_desc); break; } //write(sock , client_message , strlen(client_message)); } if(read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if(read_size == -1) { perror("recv failed"); } //Free the socket pointer free(socket_desc); return 0; }
/************************************************************************** ... **************************************************************************/ static void close_socket_callback(struct connection *pc) { close_socket_nomessage(pc); /* If we lost connection to the internal server - kill him */ client_kill_server(TRUE); freelog(LOG_ERROR, "Lost connection to server!"); output_window_append(FTC_CLIENT_INFO, NULL, _("Lost connection to server!")); if (with_ggz) { client_exit(); } }
void *pthreads(void *arg)// 线程读函数 { int newfd = *((int *)arg); char buf[BUF_SIZE] = {0}; int length = 0; send_info send, *p; p = &send; while(1) { length = read(newfd ,&send ,sizeof(send));//读取字节的长度 if(length <= 0)//读取服务器失败或者服务器为空 { printf("一个客户端退出!\n"); break; } parse_buf(p, newfd);//解析客户端传过来的字符串数据结构中的type 成员来调用函数。 memset(&send ,0 ,sizeof(send)); } client_exit(p, newfd); }
/**************************************************************************** Client connection close socket callback. It shouldn't be called directy. Use connection_close() instead. ****************************************************************************/ static void client_conn_close_callback(struct connection *pconn) { char reason[256]; if (NULL != pconn->closing_reason) { fc_strlcpy(reason, pconn->closing_reason, sizeof(reason)); } else { fc_strlcpy(reason, _("unknown reason"), sizeof(reason)); } close_socket_nomessage(pconn); /* If we lost connection to the internal server - kill it. */ client_kill_server(TRUE); log_error("Lost connection to server: %s.", reason); output_window_printf(ftc_client, _("Lost connection to server (%s)!"), reason); if (with_ggz) { client_exit(); } }
void *pthread_func(void *arg) { int newfd = *((int *)arg); char buf[BUF_SIZE] = {0}; int nread = 0; sLoginInfo send, *p_info; p_info = &send; while(1) { nread = read(newfd, &send, sizeof(send)); if(nread <= 0) { DEBUG("recv msg is empty\n"); break; } analyze_type(p_info, newfd); memset(&send, 0, sizeof(send)); } client_exit(p_info, newfd); }
/************************************************************************** ... **************************************************************************/ void disconnect_from_server(void) { const bool force = !client.conn.used; attribute_flush(); /* If it's internal server - kill him * We assume that we are always connected to the internal server */ if (!force) { client_kill_server(FALSE); } close_socket_nomessage(&client.conn); if (force) { client_kill_server(TRUE); } output_window_append(ftc_client, _("Disconnected from server.")); if (with_ggz) { client_exit(); } if (save_options_on_exit) { options_save(); } }
void startClient() { char *logfile = "/sdcard/client.out"; FILE *fp; if((fp=freopen("/sdcard/freeciv_out_client.log", "w" ,stdout))==NULL) { printf("Cannot open file.\n"); exit(1); } if((fp=freopen("/sdcard/freeciv_err_client.log", "w" ,stderr))==NULL) { printf("Cannot open file.\n"); exit(1); } setenv ("HOME", "/sdcard/FreeCiv", 0); setenv ("USER", "Sparky", 0); LOGI("Hello JNI"); log_init(logfile, LOG_DEBUG , NULL); int i, loglevel; int ui_options = 0; bool ui_separator = FALSE; char *option=NULL; bool user_tileset = FALSE; i_am_client(); /* Tell to libfreeciv that we are client */ /* Ensure that all AIs are initialized to unused state */ ai_type_iterate(ai) { init_ai(ai); } ai_type_iterate_end; init_our_capability(); (void)user_username(default_user_name, MAX_LEN_NAME); if (!is_valid_username(default_user_name)) { char buf[sizeof(default_user_name)]; my_snprintf(buf, sizeof(buf), "_%s", default_user_name); if (is_valid_username(buf)) { sz_strlcpy(default_user_name, buf); } else { my_snprintf(default_user_name, sizeof(default_user_name), "player%d", myrand(10000)); } } game.all_connections = conn_list_new(); game.est_connections = conn_list_new(); ui_init(); fc_init_network(); init_our_capability(); chatline_common_init(); init_player_dlg_common(); init_themes(); options_init(); strcpy(default_sound_plugin_name,"none"); server_port = 9999; sz_strlcpy(server_host, "localhost"); options_load(); mysrand(time(NULL)); helpdata_init(); boot_help_texts(NULL); LOGI("Reading tilespec"); tilespec_try_read("amplio2", TRUE); LOGI("Done reading tilespec"); overview_size_changed(); audio_real_init("stdsounds", "none"); //audio_play_music("music_start", NULL); auto_connect = TRUE; init_mapcanvas_and_overview(); /* run gui-specific client */ ui_main(0, NULL); /* termination */ client_exit(); return; }
/* -------------------------------------------------------------------------- * * argv[0] - prefix * * argv[1] - 'kill' * * argv[2] - name * * -------------------------------------------------------------------------- */ static void mo_kill(struct lclient *lcptr, struct client *cptr, int argc, char **argv) { int ret; struct client *acptr; if(argc > 3) { if((ret = client_relay_always(lcptr, cptr, &acptr, 2, ":%C KILL %s :%s", &argc, argv)) == -1) return; } else { if((ret = client_relay_always(lcptr, cptr, &acptr, 2, ":%C KILL %s", &argc, argv)) == -1) return; } if(argc > 3) { if(cptr->user) log(ircd_log, L_status, "%s (%s@%s) killed %s (%s@%s): %s", cptr->name, cptr->user->name, cptr->host, acptr->name, acptr->user->name, acptr->host, argv[3]); else log(ircd_log, L_status, "%s killed %s (%s@%s): %s", cptr->name, acptr->name, acptr->user->name, acptr->host, argv[3]); } else { if(cptr->user) log(ircd_log, L_status, "%s (%s@%s) killed %s (%s@%s).", cptr->name, cptr->user->name, cptr->host, acptr->name, acptr->user->name, acptr->host); else log(ircd_log, L_status, "%s killed %s (%s@%s).", cptr->name, acptr->name, acptr->user->name, acptr->host); } if(ret == 1) return; if(argc > 3) { if(cptr->user) client_exit(NULL, acptr, "killed by %s (%s@%s): %s", cptr->name, cptr->user->name, cptr->host, argv[3]); else client_exit(NULL, acptr, "killed by %s: %s", cptr->name, argv[3]); } else { if(cptr->user) client_exit(NULL, acptr, "killed by %s (%s@%s)", cptr->name, cptr->user->name, cptr->host); else client_exit(NULL, acptr, "killed by %s", cptr->name); } }
int main() { double result=0; int i=0; erlang_pid pid; CORBA_Environment *env; ei_cnode ec; /* Create and init CORBA_Environment */ env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ); /* Initiating the connection */ ei_connect_init(&ec, "c47", COOKIE, 0); /* Initiating pid*/ strcpy(pid.node, CLNODE); pid.num = 99; pid.serial = 0; pid.creation = 0; /* Fixing environment variable */ env->_fd = ei_connect(&ec, SNODE); strcpy(env->_regname,SERVER); env->_to_pid = NULL; env->_from_pid = &pid; if (env->_fd < 0) { fprintf(stderr,"Error : Cannot connect to Server\n"); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); exit(1); } /* Calling the init function */ rmod_random_init(NULL, 1, 2, 3, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ printf("Init complete !\n"); break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } /* Calling the produce function */ for(i=1; i<=10; i++) { result = rmod_random_produce(NULL, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } printf("the random number nr%d is %f\n",i,result); } /* Closing the connection */ close(env->_fd); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); return 0; }
/************************************************************************** ... **************************************************************************/ void xaw_ui_exit(void) { tileset_free_tiles(tileset); client_exit(); }
int main(){ double result=0; int i=0; int error = 0; erlang_pid pid; char host[HOSTNAMESZ]; char server_node[HOSTNAMESZ]; char client_node[HOSTNAMESZ]; CORBA_Environment *env; /* Initiate names */ #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); if ((error = WSAStartup(wVersionRequested, &wsaData))) { fprintf(stderr,"Can't initialize windows sockets: %d",error); return 0; } #endif error = gethostname(host,HOSTNAMESZ); if (error) { #ifdef __WIN32__ fprintf(stderr,"can't find own hostname (error = %ld) !\n",WSAGetLastError()); #else /* not __WIN32__ */ fprintf(stderr,"can't find own hostname !\n"); #endif } sprintf(client_node,"%s@%s",CLNODENAME,host); sprintf(server_node,"%s@%s",SNODENAME,host); /* Create and init CORBA_Environment */ env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ); /* Initiating the connection */ erl_init(NULL,0); erl_connect_init(50,COOKIE,0); /* Initiating pid*/ strcpy(pid.node,client_node); pid.num = 99; pid.serial = 0; pid.creation = 0; /* Fixing environment variable */ env->_fd=erl_connect(server_node); strcpy(env->_regname,SREGNAME); env->_to_pid = NULL; env->_from_pid = &pid; if (env->_fd < 0) { fprintf(stderr,"Error : Cannot connect to Server\n"); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); exit(1); } /* Calling the init function */ rmod_random_init(NULL, 1, 2, 3, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ printf("Init complete !\n"); break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } /* Calling the produce function */ for(i=1; i<=10; i++) { result = rmod_random_produce(NULL, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } printf("the random number nr%d is %f\n",i,result); } /* Closing the connection */ erl_close_connection(env->_fd); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); return 0; }
static void client_quit() { client_exit(conn); gtk_main_quit(); }
int main(int argc, char* argv[]) { parse_args(argc, argv); // Done differently depending on application nbody_init(num_bodies); gui_init("nbody1"); /* // Make some clumped bodies with initial velocity for a more interesting simulation for(i = 0; i < num_bodies/4; ++i) { body[i].pos.x = -50 + rand() % 20; body[i].pos.y = -50 + rand() % 20; body[i].pos.z = -50 + rand() % 20; body[i].vel.x = 0.05; } for(i = num_bodies/4; i < num_bodies/2; ++i) { body[i].pos.x = 30 + rand() % 20; body[i].pos.y = 30 + rand() % 20; body[i].pos.z = 30 + rand() % 20; body[i].vel.x = -0.05; }*/ float delta = 0.f; clock_t start = times(NULL); client_start(); int iterations = 0; while(gui_update()) { // simulate stuff delta += turbo; while(delta >= 1.f) { // random mass flip //if(allow_negative) // body[rand() % num_bodies].mass *= -1.f; calc_forces(); add_velocity(); delta -= 1.f; ++iterations; } // if there's a limit, count down and break if reached if(num_steps > 0 && (--num_steps == 0)) break; } clock_t stop = times(NULL); fputs(argv[0], stdout); int i; for(i = 1; i < argc; ++i) { fputc(' ', stdout); fputs(argv[i], stdout); } long ticks_per_sec = sysconf(_SC_CLK_TCK); printf("\n%d iterations.\n", iterations); clock_t time = stop - start; printf("elapsed: %f seconds.\navg: %f seconds per iteration.\n", ((float)(time))/ticks_per_sec, ((float)(time))/ticks_per_sec/iterations); client_exit(); gui_quit(); free(body); return 0; }