int main() { int n,t; char x; //check connection while(create_connection(5500,"127.0.0.1")==FAIL){ printf("Ban co muon thu ket noi lai?[y]\n"); x=getchar(); while(getchar()!='\n'); if (x=='y'||x=='Y')continue; else return 0; } protocol p; while(1){ // bien t de check trang thai cua signup in_menu(); scanf("%d",&n); while(getchar()!='\n'); if(n==3) break; switch(n){ case 1: c_login(&p); break; case 2: t=c_signup(&p); break; } if(t==SIGNUP_FAIL) continue; // k trung pass send(sockfd,&p,sizeof(protocol),0); recv(sockfd,&p,sizeof(protocol),0); switch(p.flag){ case SUCCESS: printf("\nLogin success!!\n"); start(p); break; case NO_ACCOUNT: printf("\n%s",LOGIN_ERROR); break; case LOGIN_FAIL: printf("\n%s",LOGIN_ERROR); break; case SIGNUP_FAIL: printf("\n%s",SIGNUP_ERROR2); } } close(sockfd); }
int main() { create_connection(5500,"127.0.0.1"); int n,t; protocol p; while(1) { // bien t de check trang thai cua signup t=-1; in_menu(); scanf("%d",&n); while(getchar()!='\n'); if(n==3) break; switch(n) { case 1: c_login(&p); break; case 2: t=c_signup(&p); break; } if(t==SIGNUP_FAIL) continue; send(sockfd,&p,sizeof(protocol),0); recv(sockfd,&p,sizeof(protocol),0); switch(p.flag) { case SUCCESS: printf("\nLogin success!!\n"); start(p); break; case NO_ACCOUNT: printf("\n%s",LOGIN_ERROR); break; case LOGIN_FAIL: printf("\n%s",LOGIN_ERROR); break; case SIGNUP_FAIL: printf("\n%s",SIGNUP_ERROR2); } } }
/*************************************************************************************************** 功能说明:客户端分析消息(来自网络) 参数说明: 输入参数:消息的地址 输出参数:无 返回:无 ***************************************************************************************************/ int c_analyse_message(Message *ms) { char dest[7] = {0}; char src[7] = {0}; char *info; c_is_ms_exit(ms); memcpy(dest, ms->data, 6); dest[6] = 0; memcpy(src, ms->data + USERNO_LEN, 6); src[6] = 0; info = ms_get_info(ms); c_is_ms_exit(ms); if( strncmp(src, "999999", 6) == 0) { if( strncmp(info, "re-login", strlen("re-login")) == 0 ) { c_login(); is_run_local = 1; } if( strcmp(info, "re-apply") == 0 ) { //apply id stage 2 c_apply_id(); is_run_local = 1; } if( !strcmp(info, "re-modify") ) { c_modify_userinfo(); is_run_local = 1; } if( !strncmp(info, "add+", 4)) { //接收到添加好友请求 char a_userno[7]; char b_nickname[64]; char a_nickname[64]; char choice = '0'; char req_info[256]; Message *req_ms = NULL; is_run_local = 0; memcpy(a_userno, info + 4, 6); a_userno[6] = 0; memset(b_nickname, 0, 64); memcpy(b_nickname, info + 11, 64); req_ms = ms_init("999999", user->userno, NULL); do{ printf("accept one friend(%s) requeset.\tagree(Y)/disagree(n)?", a_userno); fflush(stdout); scanf("%c", &choice); if(choice == 'Y' || choice == 'y') { memset(a_nickname, 0, 64); memset(req_info, 0, 256); printf("input the nickname you want to set:\n"); scanf("%s", a_nickname); strcpy(req_info, "add_agree+"); strcpy(req_info+10, a_userno); req_info[10+6] = '+'; memcpy(req_info+17, b_nickname, 64); req_info[17+64] = '+'; memcpy(req_info+17+65, a_nickname, 64); printf("\nreq_info : %s\n", req_info); memcpy(req_ms->data+2*USERNO_LEN, req_info, 256); } if( choice == 'n' || choice == 'N') { memset(req_info, 0, 256); sprintf(req_info, "add_disagree+%s", a_userno); memcpy(req_ms->data+2*USERNO_LEN, req_info, strlen(req_info)); } }while(choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n'); ms_send_message(g_c_client_socket_fd, req_ms); ms_exit(req_ms); is_run_local = 1; } if( !strncmp(info, "add_", 4)) { //添加好友结果 //1.add_agree is_run_local = 0; if( !strncmp(info, "add_agree", 7) ) { char a_userno[7]; char a_nickname[64]; char b_nickname[64]; Friend *fr = NULL; memset(a_nickname, 0, 64); memset(b_nickname, 0, 64); sscanf(info, "add_agree+%s+%s+%s", a_userno, b_nickname, a_nickname); memcpy(a_userno, info+strlen("add_agree+"), 6); a_userno[6] = 0; memcpy(b_nickname, info+strlen("add_agree+") + 7, 64); fr = (Friend *)malloc(sizeof(Friend)); memset(fr, 0, sizeof(Friend)); memcpy(fr->userno, a_userno, 6); memcpy(fr->nickname, b_nickname, 64); if(fls) fls = friendlist_init(); friendlist_add(fls, fr); printf("\nWARNING:\n\t%s is agree your friend-request!\n\n", a_userno); } //2.add_disagree if( !strncmp(info, "add_disagree", 7) ) { char a_userno[7]; memset(a_userno, 0, 7); sscanf(info, "add_disagree+%s", a_userno); printf("\nWARNING:\n\t%s is not agree your friend-request!\n\n", a_userno); } //3.add_error if( !strncmp(info, "add_error", 7) ) { char a_userno[7]; memset(a_userno, 0, 7); sscanf(info, "add_errore+%s", a_userno); printf("\nWARNING:\n\tfriend-request %s error!\n\t%s\n\n", a_userno, info+strlen("add_error+000000")); } is_run_local = 1; } if( !strncmp(info, "Your account in another place was second loginning, you are forced to logout!", 10) ) { printf("\n\nWARNING:\n\t%s", info); printf("\n\nplease restart the program, then re-login!\n\n"); is_run_net = 0; is_run_local = 0; //client exit c_client_socket_exit(); } printf("\nreceve message from server\n\t%s\n", info); return 0; } printf("\nreceve one message\n\tsrc:%s\n\tmessage:%s\n", src, info); return 0; }