int encodePxdDevices_except_one(char *buffer, char *except_device_ip,int isProxied) { //IoT_Device_Info *temp = all_registed_device; int offset = 0, len = 0, i = 0; for (i = 0; i < MAXDEVICECOUNT; i++) { if (registed_devices[i] != NULL && (strcmp(registed_devices[i]->ip.ip, except_device_ip) != 0) && registed_devices[i]->proxied== isProxied) { if (strcmp(registed_devices[i]->funGroup, "UI") == 0) continue; len = strlen(registed_devices[i]->ip.ip); charcat(buffer, registed_devices[i]->ip.ip, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; len = strlen(registed_devices[i]->devDesc); charcat(buffer, registed_devices[i]->devDesc, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; } } return offset; }
/* int encodeAllDevices(char *buffer) { IoT_Device_Info *temp = all_registed_device; int idx = 0,len=0; while (temp != NULL) { len = strlen(temp->ip.ip); charcat(buffer, temp->ip.ip, idx, len); idx += len; buffer[idx] = SplitSymble; idx++; len = strlen(temp->devDesc); charcat(buffer, temp->devDesc, idx, len); idx += len; buffer[idx] = SplitSymble; idx++; if (temp->nextDevice != NULL) { temp = (IoT_Device_Info*)temp->nextDevice; } else { temp = NULL; } } return idx; } */ int encodeAllDevices_except_one(char *buffer,char *except_device_ip) { //IoT_Device_Info *temp = all_registed_device; int offset = 0, len = 0,i=0; for (i = 0; i < MAXDEVICECOUNT; i++) { if (registed_devices[i] != NULL && (strcmp(registed_devices[i]->ip.ip, except_device_ip)!=0)) { if (strcmp(registed_devices[i]->funGroup, "UI") == 0) continue; len = strlen(registed_devices[i]->ip.ip); charcat(buffer, registed_devices[i]->ip.ip, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; len = strlen(registed_devices[i]->devDesc); charcat(buffer, registed_devices[i]->devDesc, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; } } /* while (temp != NULL) { if(strcmp(temp->ip.ip, except_device_ip)!=0) { len = strlen(temp->ip.ip); charcat(buffer, temp->ip.ip, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; len = strlen(temp->devDesc); charcat(buffer, temp->devDesc, offset, len); offset += len; buffer[offset] = SplitSymble; offset++; } if (temp->nextDevice != NULL) { temp = (IoT_Device_Info*)temp->nextDevice; } else { temp = NULL; } } */ return offset; }
void encode(int li, int ri) { if (li == ri) return; int i, isp; float p, phalf; if (ri - li == 1) { charcat(codes[ptable[li].ch], '0'); charcat(codes[ptable[ri].ch], '1'); } else { phalf = 0; for(i = li; i <= ri; ++i) phalf += ptable[i].p; p = 0; isp = -1; phalf *= 0.5f; for(i = li; i <= ri; ++i) { if(p <= phalf) charcat(codes[ptable[i].ch], '0'); else { charcat(codes[ptable[i].ch], '1'); if(isp < 0) isp = i; } p += ptable[i].p; } if (isp < 0) isp = li + 1; encode(li, isp - 1); encode(isp, ri); } }
void handle_ip_request_package(IoT_Package *package_info) { char suffix[3]; char newIp[20]; int offset = 0; sprintf(suffix, "%d", ip_count); int prefix_len = strlen(prefix); //int suffix_len = strlen(suffix); ip_count++; charcat(newIp, prefix, offset, prefix_len); offset += prefix_len; charcat(newIp, suffix, offset, prefix_len); char buffer[MAXRECV]; IoT_Package new_package_info = generate_iot_package(); create_package(&new_package_info, ServerIP, newIp, "0", 1); int packageLength = encode_package(buffer, &new_package_info); send(client_socket[package_info->belongSocketIdx], buffer, packageLength, 0); printf("Give ip:%s\n", new_package_info.des_ip.ip); free_package(&new_package_info); }
int doread() { struct obj *scroll; boolean confused = (Confusion != 0); boolean known = FALSE; scroll = getobj("?", "read"); if(!scroll) return(0); if(!scroll->dknown && Blind) { pline("Being blind, you cannot read the formula on the scroll."); return(0); } if(Blind) pline("As you pronounce the formula on it, the scroll disappears."); else pline("As you read the scroll, it disappears."); if(confused) pline("Being confused, you mispronounce the magic words ... "); switch(scroll->otyp) { #ifdef MAIL case SCR_MAIL: readmail(/* scroll */); break; #endif /* MAIL */ case SCR_ENCHANT_ARMOR: { struct obj *otmp = some_armor(); if(!otmp) { strange_feeling(scroll,"Your skin glows then fades."); return(1); } if(confused) { pline("Your %s glows silver for a moment.", objects[otmp->otyp].oc_name); otmp->rustfree = 1; break; } if(otmp->spe > 3 && rn2(otmp->spe)) { pline("Your %s glows violently green for a while, then evaporates.", objects[otmp->otyp].oc_name); useup(otmp); break; } pline("Your %s glows green for a moment.", objects[otmp->otyp].oc_name); otmp->cursed = 0; otmp->spe++; break; } case SCR_DESTROY_ARMOR: if(confused) { struct obj *otmp = some_armor(); if(!otmp) { strange_feeling(scroll,"Your bones itch."); return(1); } pline("Your %s glows purple for a moment.", objects[otmp->otyp].oc_name); otmp->rustfree = 0; break; } if(uarm) { pline("Your armor turns to dust and falls to the floor!"); useup(uarm); } else if(uarmh) { pline("Your helmet turns to dust and is blown away!"); useup(uarmh); } else if(uarmg) { pline("Your gloves vanish!"); useup(uarmg); selftouch("You"); } else { strange_feeling(scroll,"Your skin itches."); return(1); } break; case SCR_CONFUSE_MONSTER: if(confused) { pline("Your hands begin to glow purple."); Confusion += rnd(100); } else { pline("Your hands begin to glow blue."); u.umconf = 1; } break; case SCR_SCARE_MONSTER: { int ct = 0; struct monst *mtmp; for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) if(cansee(mtmp->mx,mtmp->my)) { if(confused) mtmp->mflee = mtmp->mfroz = mtmp->msleep = 0; else mtmp->mflee = 1; ct++; } if(!ct) { if(confused) pline("You hear sad wailing in the distance."); else pline("You hear maniacal laughter in the distance."); } break; } case SCR_BLANK_PAPER: if(confused) pline("You see strange patterns on this scroll."); else pline("This scroll seems to be blank."); break; case SCR_REMOVE_CURSE: { struct obj *obj; if(confused) pline("You feel like you need some help."); else pline("You feel like someone is helping you."); for(obj = invent; obj ; obj = obj->nobj) if(obj->owornmask) obj->cursed = confused; if(Punished && !confused) { Punished = 0; freeobj(uchain); unpobj(uchain); free(uchain); uball->spe = 0; uball->owornmask &= ~W_BALL; uchain = uball = (struct obj *) 0; } break; } case SCR_CREATE_MONSTER: { int cnt = 1; if(!rn2(73)) cnt += rnd(4); if(confused) cnt += 12; while(cnt--) (void) makemon(confused ? PM_ACID_BLOB : (struct permonst *) 0, u.ux, u.uy); break; } case SCR_ENCHANT_WEAPON: if(uwep && confused) { pline("Your %s glows silver for a moment.", objects[uwep->otyp].oc_name); uwep->rustfree = 1; } else if(!chwepon(scroll, 1)) /* tests for !uwep */ return(1); break; case SCR_DAMAGE_WEAPON: if(uwep && confused) { pline("Your %s glows purple for a moment.", objects[uwep->otyp].oc_name); uwep->rustfree = 0; } else if(!chwepon(scroll, -1)) /* tests for !uwep */ return(1); break; case SCR_TAMING: { int i,j; int bd = confused ? 5 : 1; struct monst *mtmp; for(i = -bd; i <= bd; i++) for(j = -bd; j <= bd; j++) if ((mtmp = m_at(u.ux+i, u.uy+j))) (void) tamedog(mtmp, NULL); break; } case SCR_GENOCIDE: { extern char genocided[], fut_geno[]; char buf[BUFSZ]; struct monst *mtmp, *mtmp2; pline("You have found a scroll of genocide!"); known = TRUE; if(confused) *buf = u.usym; else do { pline("What monster do you want to genocide (Type the letter)? "); getlin(buf); } while(strlen(buf) != 1 || !monstersym(*buf)); if(!strchr(fut_geno, *buf)) charcat(fut_geno, *buf); if(!strchr(genocided, *buf)) charcat(genocided, *buf); else { pline("Such monsters do not exist in this world."); break; } for(mtmp = fmon; mtmp; mtmp = mtmp2){ mtmp2 = mtmp->nmon; if(mtmp->data->mlet == *buf) mondead(mtmp); } pline("Wiped out all %c's.", *buf); if(*buf == u.usym) { killer = "scroll of genocide"; u.uhp = -1; } break; } case SCR_LIGHT: if(!Blind) known = TRUE; litroom(!confused); break; case SCR_TELEPORTATION: if(confused) level_tele(); else { #ifdef QUEST int oux = u.ux, ouy = u.uy; tele(); if(dist(oux, ouy) > 100) known = TRUE; #else /* QUEST */ int uroom = inroom(u.ux, u.uy); tele(); if(uroom != inroom(u.ux, u.uy)) known = TRUE; #endif /* QUEST */ } break; case SCR_GOLD_DETECTION: /* Unfortunately this code has become slightly less elegant, now that gold and traps no longer are of the same type. */ if(confused) { struct trap *ttmp; if(!ftrap) { strange_feeling(scroll, "Your toes stop itching."); return(1); } else { for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) if(ttmp->tx != u.ux || ttmp->ty != u.uy) goto outtrapmap; /* only under me - no separate display required */ pline("Your toes itch!"); break; outtrapmap: cls(); for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) at(ttmp->tx, ttmp->ty, '$'); prme(); pline("You feel very greedy!"); } } else { struct gold *gtmp; if(!fgold) { strange_feeling(scroll, "You feel materially poor."); return(1); } else { known = TRUE; for(gtmp = fgold; gtmp; gtmp = gtmp->ngold) if(gtmp->gx != u.ux || gtmp->gy != u.uy) goto outgoldmap; /* only under me - no separate display required */ pline("You notice some gold between your feet."); break; outgoldmap: cls(); for(gtmp = fgold; gtmp; gtmp = gtmp->ngold) at(gtmp->gx, gtmp->gy, '$'); prme(); pline("You feel very greedy, and sense gold!"); } } /* common sequel */ more(); docrt(); break; case SCR_FOOD_DETECTION: { int ct = 0, ctu = 0; struct obj *obj; char foodsym = confused ? POTION_SYM : FOOD_SYM; for(obj = fobj; obj; obj = obj->nobj) if(obj->olet == FOOD_SYM) { if(obj->ox == u.ux && obj->oy == u.uy) ctu++; else ct++; } if(!ct && !ctu) { strange_feeling(scroll,"Your nose twitches."); return(1); } else if(!ct) { known = TRUE; pline("You smell %s close nearby.", confused ? "something" : "food"); } else { known = TRUE; cls(); for(obj = fobj; obj; obj = obj->nobj) if(obj->olet == foodsym) at(obj->ox, obj->oy, FOOD_SYM); prme(); pline("Your nose tingles and you smell %s!", confused ? "something" : "food"); more(); docrt(); } break; } case SCR_IDENTIFY: /* known = TRUE; */ if(confused) pline("You identify this as an identify scroll."); else pline("This is an identify scroll."); useup(scroll); objects[SCR_IDENTIFY].oc_name_known = 1; if(!confused) while( !ggetobj("identify", identify, rn2(5) ? 1 : rn2(5)) && invent ); return(1); case SCR_MAGIC_MAPPING: { struct rm *lev; int num, zx, zy; known = TRUE; pline("On this scroll %s a map!", confused ? "was" : "is"); for(zy = 0; zy < ROWNO; zy++) for(zx = 0; zx < COLNO; zx++) { if(confused && rn2(7)) continue; lev = &(levl[zx][zy]); if((num = lev->typ) == 0) continue; if(num == SCORR) { lev->typ = CORR; lev->scrsym = CORR_SYM; } else if(num == SDOOR) { lev->typ = DOOR; lev->scrsym = '+'; /* do sth in doors ? */ } else if(lev->seen) continue; #ifndef QUEST if(num != ROOM) #endif /* QUEST */ { lev->seen = lev->new = 1; if(lev->scrsym == ' ' || !lev->scrsym) newsym(zx,zy); else on_scr(zx,zy); } } break; } case SCR_AMNESIA: { int zx, zy; known = TRUE; for(zx = 0; zx < COLNO; zx++) for(zy = 0; zy < ROWNO; zy++) if(!confused || rn2(7)) if(!cansee(zx,zy)) levl[zx][zy].seen = 0; docrt(); pline("Thinking of Maud you forget everything else."); break; } case SCR_FIRE: { int num; struct monst *mtmp; known = TRUE; if(confused) { pline("The scroll catches fire and you burn your hands."); losehp(1, "scroll of fire"); } else { pline("The scroll erupts in a tower of flame!"); if(Fire_resistance) pline("You are uninjured."); else { num = rnd(6); u.uhpmax -= num; losehp(num, "scroll of fire"); } } num = (2*num + 1)/3; for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) { if(dist(mtmp->mx,mtmp->my) < 3) { mtmp->mhp -= num; if(strchr("FY", mtmp->data->mlet)) mtmp->mhp -= 3*num; /* this might well kill 'F's */ if(mtmp->mhp < 1) { killed(mtmp); break; /* primitive */ } } } break; } case SCR_PUNISHMENT: known = TRUE; if(confused) { pline("You feel guilty."); break; } pline("You are being punished for your misbehaviour!"); if(Punished){ pline("Your iron ball gets heavier."); uball->owt += 15; break; } Punished = INTRINSIC; setworn(mkobj_at(CHAIN_SYM, u.ux, u.uy), W_CHAIN); setworn(mkobj_at(BALL_SYM, u.ux, u.uy), W_BALL); uball->spe = 1; /* special ball (see save) */ break; default: impossible("What weird language is this written in? (%u)", scroll->otyp); }
void killed(struct monst *mtmp) { #define NEW_SCORING int tmp; int tmp2; int nk; int x; int y; struct permonst *mdat = mtmp->data; if(mtmp->cham != 0) { mdat = PM_CHAM; } if(Blind != 0) { pline("You destroy it!"); } else { if(mtmp->mtame != 0) { pline("You destroy %s!", amonnam(mtmp, "poor")); } else { pline("You destroy %s!", monnam(mtmp)); } } if(u.umconf != 0) { if(Blind == 0) { pline("Your hands stop clowing blue."); u.umconf = 0; } } /* Count killed monsters */ #define MAXMONNO 100 /* In case we cannot find it in mons */ nk = 1; /* Index in mons array (if not 'd', '@', ...) */ tmp = mdat - mons; if((tmp >= 0) && (tmp < (CMNUM + 2))) { extern char fut_geno[]; ++u.nr_killed[tmp]; nk = u.nr_killed[tmp]; if((nk > MAXMONNO) && (index(fut_geno, mdat->mlet) == 0)) { charcat(fut_geno, mdat->mlet); } } /* Punish bad behaviour */ if(mdat->mlet == '@') { Telepat = 0; u.uluck -= 2; } if((mtmp->mpeaceful != 0) || (mtmp->mtame != 0)) { --u.uluck; } if(mdat->mlet == 'u') { u.uluck -= 5; } /* Give experience points */ tmp = 1 + (mdat->mlevel * mdat->mlevel); if(mdat->ac < 3) { tmp += (2 * (7 - mdat->ac)); } if(index("AcsSDXaeRTVWU&In:P", mdat->mlet) != 0) { tmp += (2 * mdat->mlevel); } if(index("DeV&P", mdat->mlet) != 0) { tmp += (7 * mdat->mlevel); } if(mdat->mlevel > 6) { tmp += 50; } #ifdef NEW_SCORING /* * ------- Recent addition: make number of points decrease * when this is not the first of this kind */ int ul = u.ulevel; int ml = mdat->mlevel; /* Points are given based on present and future level */ if(ul < 14) { for(tmp2 = 0; (tmp2 == 0) || ((ul + tmp2) <= ml); ++tmp2) { if(tmp <= 0) { if(((u.uexp + 1) + ((tmp + (0)) / nk)) >= (10 * pow(2, (unsigned)(ul - 1)))) { ++ul; if(ul == 14) { break; } } } else { if(((u.uexp + 1) + ((tmp + (4 << (tmp2 - 1))) / nk)) >= (10 * pow(2, (unsigned)(ul - 1)))) { ++ul; if(ul == 14) { break; } } } } } tmp2 = (ml - ul) - 1; if(tmp2 < 0) { tmp = (tmp + (0)) / nk; } else { tmp = (tmp + (4 << tmp2)) / nk; } if(tmp == 0) { tmp = 1; } /* * Note: ul is not necessarily the future value of u.ulevel * ------- End of recent valuation change ------- */ #endif u.uexp += tmp; u.urexp += (4 * tmp); flags.botl = 1; while((u.ulevel < 14) && (u.uexp >= (10 * pow(2, u.ulevel - 1)))) { ++u.ulevel; pline("Welcome to level %d.", u.ulevel); tmp = rnd(30); if(tmp < 3) { tmp = rnd(10); } u.uhpmax += tmp; u.uhp += tmp; flags.botl = 1; } /* Dispose of monster and make cadaver */ x = mtmp->mx; y = mtmp->my; mondead(mtmp); tmp = mdat->mlet; if(tmp == 'm') { /* He killed a minotaur, give him a wand of digging */ /* Note: The dead minotaur will be on top of it! */ mksobj_at(WAND_SYM, WAN_DIGGING, x, y); /* * if(cansee(x, y) != 0) { * atl(x, y, fobj->olet); * } */ stackobj(fobj); } #ifndef NOWORM else if(tmp == 'w') { mksobj_at(WEAPON_SYM, WORM_TOOTH, x, y); stackobj(fobj); } #endif else { if((letter(tmp) == 0) || (rn2(3) == 0)) { tmp = 0; } if(levl[x][y].typ >= DOOR) { /* Might be a mimic in wall */ if((x != u.ux) || (y != u.uy)) { /* Might be here after swallowed */ if((index("NTVm&", mdat->mlet) != NULL) || (rn2(5) != 0)) { mkobj_at(tmp, x, y); if(cansee(x, y) != 0) { atl(x, y, fobj->olet); } stackobj(fobj); } } } } }
void handle_managent_package(IoT_Package *package_info) { //printAllChar(package_info->data, package_info->data_length); const int temp_str_len = 50; int deviceIndex = -1; char str_temp[temp_str_len] = { '\0' }; JsonData jsonData; charcat(jsonData.content, package_info->data, 0, package_info->data_length); init_token(&jsonData); range_t range; range.start = jsonData.tokens[1].start; range.end = jsonData.tokens[1].end; getPatteren(range, jsonData.content, str_temp); //Register if (strcmp("IOTDEV", str_temp) == 0) { puts("A Register package include device info"); add_new_device(package_info->sor_ip, jsonData, package_info->belongSocketIdx); } //Command else if (strcmp("IOTCMD", str_temp) == 0) { puts("A Command package"); IoT_Command cmd = generate_iot_command(); decode_iot_cmd(package_info->data, &cmd); if (cmd.cmd_type != command_t_Management) { puts("IOTCMD has wrong command type"); return; } //if (strcmp(cmd.ID,"Dis_All") == 0 && cmd.cmd_type==command_t_Management) if (memcmp(cmd.ID, "Dis_",4) == 0) { discover_package_replier(package_info,&cmd); } else if (strcmp(cmd.ID, "Del_Dev") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: has bean deleted\n", deviceIndex); free_device_info(registed_devices[deviceIndex]); registed_devices[deviceIndex] = NULL; } } else if (strcmp(cmd.ID, "Prx_Add") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: has bean proxied\n", deviceIndex); registed_devices[deviceIndex]->proxied = 1; } } else if (strcmp(cmd.ID, "Prx_Rmv") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: proxy released\n", deviceIndex); registed_devices[deviceIndex]->proxied = 0; } } else if (strcmp(cmd.ID, "Re_Conf") == 0) { printf("Reload config"); } else { printf("unknow manage command:%s", cmd.ID); } } }
int main_server_loop(Server_Object *server_obj) { IoTSocket new_socket; struct sockaddr_in address; int activity, i, valread,fdmax=65535; //first received buffer char buffer[MAXRECV]; TimeSpan timeout; timeout.tv_sec = 100; timeout.tv_usec = 0; //set of socket descriptors fd_set master, readfds; //Bind if (bind(server_obj->listener, (struct sockaddr *)&server_obj->address, sizeof(server_obj->address)) == -1) { printf("Main server Bind failed\n"); PAUSE; exit(EXIT_FAILURE); } puts("Main server Bind done"); //Listen to incoming connections listen(server_obj->listener, MAXCLIENTS); //Accept and incoming connection puts("Main server Waiting for incoming connections..."); #if defined(WIN32) int addrlen = sizeof(struct sockaddr_in); #elif defined(__linux__) || defined(__FreeBSD__) socklen_t addrlen = sizeof(struct sockaddr_in); #endif //clear the socket fd set FD_ZERO(&master); FD_ZERO(&readfds); //add listener socket to fd set FD_SET(server_obj->listener, &master); //fdmax = server_obj->listener + 1; while (1) { readfds = master; activity = select(MAXCLIENTS, &readfds, NULL, NULL, &timeout); if (activity == -1) { printf("Main server select call failed"); PAUSE; exit(EXIT_FAILURE); } //Add new connection into set if (FD_ISSET(server_obj->listener, &readfds)) { if ((new_socket = accept(server_obj->listener, (struct sockaddr *)&address, &addrlen))<0) { perror("accept"); PAUSE; exit(EXIT_FAILURE); } //inform user of socket number - used in send and receive commands printf("Main server has new connection , socket fd is %d , ip is : %s , port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //add new socket to array of sockets for (i = 0; i < MAXCLIENTS; i++) { if (client_socket[i] == 0) { client_socket[i] = new_socket; FD_SET(new_socket, &master); printf("Main server adding a new item to list of sockets at index %d \n", i); packageBuffer[i] = generate_package_buffer(); break; } } } //handle connected conndetion,find which connection active for (i = 0; i < MAXCLIENTS; i++) { /* if (FD_ISSET(client_socket[i], &readfds)) { valread = recv(client_socket[i], buffer, MAXRECV, 0); if (valread <= 0) { //Somebody disconnected , get his details and print printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //Close the socket and mark as 0 in list for reuse FD_CLR(client_socket[i], &master); CloseIoTSocket(client_socket[i]); client_socket[i] = 0; free_package_buffer(packageBuffer[i]); packageBuffer[i] = NULL; } //Echo back the message that came in else if (valread>0) { //add null character, if you want to use with printf/puts or other string handling functions //buffer[valread] = '\0'; printf("FD_Index:%d\n", i); printf("%s:%d-", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); printAllChar(buffer, valread); char buf[10]; strcpy(buf, "Hello"); //ms_sleep(4000); while (1) { send(client_socket[i], buf, 5, 0); ms_sleep(1000); printf("Sended\n"); } } } */ if (FD_ISSET(client_socket[i], &readfds)) { //get details of the client getpeername(client_socket[i], (struct sockaddr*)&address, &addrlen); //Check if it was for closing , and also read the incoming message //recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one). memset(buffer, '\0', MAXRECV); valread = recv(client_socket[i], buffer, MAXRECV, 0); if (valread <= 0) { //Somebody disconnected , get his details and print printf("Main server got host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //Close the socket and mark as 0 in list for reuse FD_CLR(client_socket[i], &master); CloseIoTSocket(client_socket[i]); client_socket[i] = 0; free_package_buffer(packageBuffer[i]); packageBuffer[i] = NULL; } //Echo back the message that came in else if (valread>0) { /* printf("Received data[%d]", valread); printAllChar(buffer, valread); */ if (packageBuffer[i]->receiveCount+ valread > MAXRECV) { puts("Buffer over flow clear All buffer"); packageBuffer[i]->receiveCount = 0; memset(packageBuffer[i]->tempBuffer,'\0', MAXRECV); } else { charcat(packageBuffer[i]->tempBuffer, buffer, packageBuffer[i]->receiveCount, valread); packageBuffer[i]->receiveCount += valread; } //try to get a completed IoT Package recv_result result; do { IoT_Package package_info = generate_iot_package(); result = getCompletedPackage(packageBuffer[i]->tempBuffer, &packageBuffer[i]->receiveCount, &package_info); if (result== recv_result_COMPLETED) { package_info.belongSocketIdx = i; handle_package(&package_info); } free_package(&package_info); } while (result == recv_result_COMPLETED); if (packageBuffer[i]->receiveCount == 0) { printf("Buffer[%d] cleared2\n",i); } } } } } }