void send_play_req(char *nick, int sid) { char sendbuf[8096]; int s, d; char dnick[255]; int did; int dsd; /* find nick of "sid" */ for (s=0; s<=MAXCLIENTS; s++) { if(clients[s].sd != 0 && clients[s].id == sid) { break; } } //printf("nick del retador: %s id: %d\n", clients[s].nick, clients[s].id); /* find the destination */ for (d=0; d<=MAXCLIENTS; d++) { if (clients[d].sd != 0 && strcmp(clients[d].nick, nick) == 0) { sprintf(dnick, "%s", clients[d].nick); did = clients[d].id; dsd = clients[d].sd; clients[s].otherside = did; sprintf(sendbuf, "'%s' WANTS TO PLAY WITH YOU, TYPE '.ACCEPT' TO CONNECT WITH HIM.", clients[s].nick); sendtext(dsd, sendbuf); clients[d].challenger = sid; break; } } }
void direct_connect(int local, int remote) { int len; void *buf; buf = malloc(255); if (clients[remote].challenger == local) { sendtext(clients[local].sd, "ERROR: remote challenger != local"); return; } sendtext(clients[local].sd, "CONNECTED!"); sendtext(clients[remote].sd, "CONNECTED!"); clients[remote].connected = 1; for (;;) { /* receive from local side and redirect it to remote side */ bzero(buf, sizeof(buf)); len=recv(clients[local].sd, buf, 255, 0); if (len == 0) { /* broken socket! */ printf("broken socket!\n"); break; } if (len > 0) { send(clients[remote].sd, buf, len, 0); } /* vice versa */ bzero(buf, sizeof(buf)); len=recv(clients[remote].sd, buf, 255, 0); if (len == 0) { /* broken socket! */ printf("broken socket!\n"); break; } if (len > 0) { send(clients[local].sd, buf, len, 0); } fast_nanosleep(0,1000); } printf("end_dc!\n"); clients[remote].connected = 0; free(buf); close(clients[remote].sd); close(clients[local].sd); }
void typetext(HWND hwnd, int msngrwindow, LPCTSTR Sploitmsg) { if(msngrwindow != 0) { ShowWindow(hwnd, SW_SHOW); activatefocus(hwnd); cleartext(); Sleep(10); for(int x = 0; x < strlen(Sploitmsg); x++) { keybd_event(VkKeyScan((char)Sploitmsg[x]),0,0 , 0); keybd_event(VkKeyScan((char)Sploitmsg[x]),0, KEYEVENTF_KEYUP,0); } activatefocus(hwnd); sendtext(msngrwindow); } return; }
/*============================================================================== main 主函数 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 参数: 无 返回码: 无 ==============================================================================*/ main(int argc,char *argv[]) { if(argc < 2) { printf("testfile 8 批量扣款信息查询 9 批量扣款信息返回\n"); } int sd,j; /* socket descriptor */ char ydport[10]; char ydhost[40]; char sendbuf[1024]; char buf[1024]; char recvbuf[1024]; char filelen[18+1]; int len; TCPPARMS tcpp; int retCode; memset(sendbuf,0,sizeof(sendbuf)); memset(recvbuf,0,sizeof(recvbuf)); memset(buf,0,sizeof(buf)); char recvfile[] = { "7008recv" }; char sendfile[] = { "7009send" }; int i = 0; switch(atoi(argv[1])) { case 8: sprintf(sendbuf,"%-4.4s%-4.4s%-4.4s%-18.18s","0026","7008","",""); break; case 9: i = productext(recvfile, sendfile); sprintf(sendbuf,"%-4.4s%-4.4s%-4.4s%-18.18d","0026","7009","",i); break; default: exit(0); } runlog(__FILE__,__LINE__,"sendbuf=[%s]\n",sendbuf); //设置服务端ip地址、端口号 //strcpy(ydhost,"172.20.8.206"); //strcpy(ydport,"443"); strcpy(ydhost,"192.168.8.205"); strcpy(ydport,"9005"); if(tcp_init(atoi(ydport),ydhost,&tcpp) < 0){ printf("TCP初始化失败!\n"); return -1; }; if((sd = tcp_connect(&tcpp)) < 0){ printf("TCP连接失败!\n"); closesocket(sd); return -1; } if(writen(sd,sendbuf,strlen(sendbuf)) < strlen(sendbuf)){ printf("TCP发送报文失败!\n"); closesocket(sd); return -1; } //接受文件 switch(atoi(argv[1])) case 8: { if(readn(sd,recvbuf,30)<0){/*失败*/ printf("TCP接收报文失败![%d]\n",len); closesocket(sd); return -1; } sprintf(filelen,"%18.18s",recvbuf+12); recvtext(sd,"7008recv",filelen); break; case 9: sendtext(sd, sendfile, i); break; } closesocket(sd); runlog(__FILE__,__LINE__,"recvbuf=[%s]\n",recvbuf); return 0; } /*main*/
int send_http_response_header_to_client(int sd, char* header){ return sendtext(sd, header); }
void *server_connection(void *args) { char buf[8096]; char sendbuf[8096]; int len; int sd; int i, ii; int protoid=0; int init=0; char *chattext; char nick[255]; int myid = -1; char *temp; struct client_data_t *client_data; client_data=(struct client_data_t*)args; sd = client_data->sd; printf("Accept!\n"); for(;;) { bzero(buf, sizeof(buf)); /* set socket as non blocking */ fcntl(sd, F_SETFL, fcntl(sd, F_GETFL) | O_NONBLOCK | MSG_WAITALL); len=recv(sd, buf, 255, 0); if (len == 0) break; if (len > 0) { /* manage protocol */ if (strlen(buf) > 3 && buf[0] == 'I' && buf[1] == 'D') { printf("ID request\n"); send(sd, buf, strlen(buf), 0); /* reply it with the same ID */ } else if (strlen(buf) == 1 && buf[0] == 1) { printf("(1) REQUEST\n"); bzero(sendbuf, sizeof(sendbuf)); sprintf(sendbuf, "\x01"); send(sd, sendbuf, strlen(sendbuf), 0); protoid=1; } /* text begins with \x02 */ else if (buf[0] == 2) { printf("(chat) %s\n", buf); chattext = strstr(buf, ">"); if (strcmp(chattext, ">.INIT") == 0) { /* initialize and register the client */ /* try to figure out what is our nick... */ if (init == 1) { sendtext(sd, "CAN'T INIT TWICE!"); goto endinit; } for (i=0; i<=strlen(buf); i++) { if (buf[i] == '>') break; } if (i==strlen(buf)) { sendtext(sd, "UNABLE TO FIND YOUR NICK!"); break; } else { bzero(nick, sizeof(nick)); for (ii=0; ii<i ;ii++) { nick[ii-1] = buf[ii]; } //nick[ii]=0; } /* check if the nick is already registered */ for (i=0; i<=MAXCLIENTS; i++) { if (strcmp(clients[i].nick, nick) == 0) { sendtext(sd, "YOUR NICK IS ALREADY ONLINE"); sendtext(sd, "PLEASE CHOOSE ANOTHER."); fast_nanosleep(3,0); goto endthread; } } // register client for (i=0; i<=MAXCLIENTS; i++) { if (clients[i].sd == 0) { /* find an empty slot */ myid = i; //clients[myid].nick = (char *)malloc(sizeof(char *) * 64); strcpy(clients[myid].nick, ""); sprintf(clients[myid].nick, "%s", nick); clients[myid].id = myid; sprintf(sendbuf, "REGISTERED AS '%s' ON SLOT %d.", clients[myid].nick, myid); sprintf(clients[myid].nick, "%s", strupr(clients[myid].nick)); sendtext(sd, sendbuf); sendtext(sd, "!! IMPORTANT !!"); sendtext(sd, "PLEASE UNCHECK ALL THE 'PLAYER SELECT' CHECK BOXES."); sendtext(sd, "---"); clients[myid].sd = sd; clients[myid].challenger=-1; break; } } if (i == MAXCLIENTS) { /* can't find an empty slot! */ sendtext(sd, "UNABLE TO FIND AN EMPTY SLOT!!"); sendtext(sd, "PLEASE TRY AGAIN LATER"); fast_nanosleep(3,0); goto endthread2; } init = 1; sendtext(sd, "DONE!"); endinit: init = init; /* this is stupid, but gcc complains if nothing is to do after the label */ } /* .WHO request */ if (strcmp(chattext, ">.WHO") == 0 && init == 1) { for (i = 0 ; i <= MAXCLIENTS ; i++) { if (clients[i].sd != 0) { sprintf(sendbuf, "%d: %s", clients[i].id, clients[i].nick); sendtext(sd, sendbuf); } } sendtext(sd, "--"); } /* play request */ if (strncmp(chattext, ">.PLAY ", 7) == 0 && strlen(chattext) > 7 && init == 1) { temp = (char *)malloc(sizeof(char *) * 255); sscanf(chattext, ">.PLAY %s", temp); sprintf(sendbuf, "SENDING REQUEST TO %s", temp); sendtext(sd, sendbuf); send_play_req(temp, myid); free(temp); } /* .ACCEPT request */ if (strcmp(chattext, ">.ACCEPT") == 0 && init == 1) { if (clients[myid].challenger == -1) { sendtext(sd, "YOU HAVE NO CHALLENGER!"); } else { direct_connect(myid, clients[myid].challenger); goto endthread; } } if (init == 0) { sendtext(sd, "TYPE '.INIT' TO INITIALIZE YOUR CLIENT."); } else { printf("chattext: %s\n", chattext); if (strcmp(chattext, ">.HELP") == 0) { sendtext(sd, "HELP IS NOT IMPLEMENTED YET!"); sendtext(sd, "SORRY."); } } } else { printf("len: %d - '%s'\n", len, buf); printf("ASCII decode: "); for (i=0;i<=strlen(buf);i++) { printf("%c=%i ", buf[i], buf[i]); } printf("\n"); } } if (protoid == 2) { sendtext(sd, "YOU ARE UNINITIALIZED!"); sendtext(sd, "TYPE '.INIT' TO INITIALIZE YOUR CLIENT."); protoid = 3; } if (protoid == 1) { protoid=2; sendtext(sd, "WELCOME TO ZSNES SERVER!"); send(sd, "\x03", 1, 0); /* uncheck player 1 slot */ } /* direct connection */ if (myid > -1) { if (clients[myid].connected == 1) { direct_connect2(myid); goto endthread; } } fast_nanosleep(0,9999); } endthread: /* unregister the client */ for (i=0; i<=MAXCLIENTS; i++) { if (clients[i].id == myid) { bzero(clients[i].nick, sizeof(clients[i].nick)); strcpy(clients[i].nick, ""); clients[i].id = 0; clients[i].sd = 0; break; } } endthread2: close(sd); printf("Closing connection\n"); pthread_exit(0); }