void send_imgFileName(char *file_name){ char uri[1024]; char client_addr[128]; char method_name[] = "/addimg"; strncpy(uri, SIMGLINK_URI_HEAD, 1024); strncat(uri, method_name, 1024); getClientAddr(CLIENT_NETWORK_DEVICE, client_addr, 128); strncat(uri, "/", 1024); strncat(uri, client_addr, 128); strncat(uri, "/", 1024); strncat(uri, file_name, 1024); send_httpr(SIMGLINK_HOST, uri, SIMGLINK_PORTNO, stdout); }
BSONObj ChangeLogType::toBSON() const { BSONObjBuilder builder; if (_changeId) builder.append(changeId.name(), getChangeId()); if (_server) builder.append(server.name(), getServer()); if (_shard) builder.append(shard.name(), getShard()); if (_clientAddr) builder.append(clientAddr.name(), getClientAddr()); if (_time) builder.append(time.name(), getTime()); if (_what) builder.append(what.name(), getWhat()); if (_ns) builder.append(ns.name(), getNS()); if (_details) builder.append(details.name(), getDetails()); return builder.obj(); }
void *accepted(void *csd){ PEER *p = (PEER *) csd; DATA *data = malloc(sizeof(DATA)); DATA *reply = malloc(sizeof(DATA)); int client_sd = p->client_sd; int len, status; int id = p->id; while(1){ printf("B4 recv data\n"); data = recv_data(client_sd,(unsigned int *)&len,&status); printf("AFTER recv data\n"); switch(status){ case -1: case -2: printf("Status = error(%d)\n", status); close(client_sd); offline(id); printf("[Disconnected] Client %s:%d\n", getClientAddr(&p->client_addr), ntohs(p->client_addr.sin_port)); return 0; default: break; } printf("DATA command: %d\n", data->command); switch(data->command){ case LOGIN: printf("Client %s:%d: [LOGIN] Username="******"\n"); data->arg->ip = ntohl(p->client_addr.sin_addr.s_addr); status = online(data->arg, id); if(status >= 0 && status < 10){ reply = newHeader(); reply->command = LOGIN_OK; reply->length = 1; if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0; }else{ printf("[ERROR] Client %s:%d ", getClientAddr(&p->client_addr), ntohs(p->client_addr.sin_port)); reply = newHeader(); reply->command = ERROR; switch(status){ case -1: printf("maximum connection exceed!"); reply->error = TOO_MUCH_CONN; break; case -2: printf("duplication of name!"); reply->error = SAME_NAME; break; case -3: printf("duplication of port and IP address!"); reply->error = SAME_CONN; } printf("\n"); reply->length = 7; if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0; close(client_sd); printf("[Disconnected] Client %s:%d\n", getClientAddr(&p->client_addr), ntohs(p->client_addr.sin_port)); return 0; } break; case GET_LIST: printf("Client %s:%d: GET_LIST\n", getClientAddr(&p->client_addr), ntohs(p->client_addr.sin_port)); reply = newHeader(); reply->command = GET_LIST_OK; getOnlineList(reply); reply->length += 7; if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0; break; default: printf("Client %s:%d: Unknown command\n", getClientAddr(&p->client_addr), ntohs(p->client_addr.sin_port)); break; } } if(client_sd) close(client_sd); offline(id); printOnlineList(); printf("[Disconnected] Client %s:%d\n", getClientAddr(&p->client_addr), p->client_addr.sin_port); }
int main(int argc, char **argv){ int sd = socket(AF_INET, SOCK_STREAM, 0); int client_sd; int addr_len; int len; int i; pthread_t pth[10]; struct sockaddr_in server_addr; struct sockaddr_in client_addr; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(PORT); for(i=0;i<10;i++){ peers[i] = NULL; } if(bind(sd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0){ printf("Socket cannot bind to a port. %s \nError number: %d\n", strerror(errno), errno); } if(listen(sd,3) < 0){ printf("Cannot listen to a port. %s \nError number: %d\n", strerror(errno), errno); } addr_len = sizeof(client_addr); while(1){ if((client_sd = (accept(sd, (struct sockaddr *) &client_addr, (unsigned int*)&addr_len))) < 0){ printf("Connection error. Accepting client failed. %s \nError number: %d\n", strerror(errno), errno); exit(0); } printf("[Connected] Client: %s:%d\n", getClientAddr(&client_addr), htons(client_addr.sin_port)); for(i=0;i<10;i++){ if(peers[i]==NULL){ passing[i].id = i; passing[i].client_sd = client_sd; passing[i].sd = sd; passing[i].client_addr = client_addr; pthread_create(&pth[i], NULL, accepted, (void *) &passing[i]); break; } } if(i==10){ DATA *reply = newHeader(); printf("[ERROR] Client %s:%d ", getClientAddr(&client_addr), ntohs(client_addr.sin_port)); reply = newHeader(); reply->command = ERROR; printf("maximum connection exceed!"); reply->error = TOO_MUCH_CONN; printf("\n"); reply->length = 7; send_data(client_sd,reply,(unsigned int*)&len); close(client_sd); printf("[Disconnected] Client %s:%d\n", getClientAddr(&client_addr), ntohs(client_addr.sin_port)); } } for(i=0;i<10;i++) pthread_join(pth[i],NULL); close(sd); return 0; }