int easysocketbenchmark::createclient(){ int fd = anetTcpConnect(NULL, this->ip, this->port); while(ANET_ERR == fd){ fd = anetTcpConnect(NULL, this->ip, this->port); } /*if( ANET_ERR == fd ){ //fprintf(stderr, "connect error: %s\n", PORT, NULL); printf("connect error......"); }*/ if(anetNonBlock(NULL, fd) == ANET_ERR){ //fprintf(stderr, "set nonblock error: %d\n", fd); //printf("set nonblock error......"); } return fd; }
client::client(const string_type & host, unsigned int port) { char err[ANET_ERR_LEN]; socket_ = anetTcpConnect(err, const_cast<char*>(host.c_str()), port); if (socket_ == ANET_ERR) throw connection_error(err); anetTcpNoDelay(NULL, socket_); }
/* Connect to a Redis instance. On success NULL is returned and *fd is set * to the socket file descriptor. On error a redisReply object is returned * with reply->type set to REDIS_REPLY_ERROR and reply->string containing * the error message. This replyObject must be freed with redisFreeReply(). */ redisReply *redisConnect(int *fd, const char *ip, int port) { char err[ANET_ERR_LEN]; *fd = anetTcpConnect(err,ip,port); if (*fd == ANET_ERR) return createReplyObject(REDIS_REPLY_ERROR,sdsnew(err)); anetTcpNoDelay(NULL,*fd); return NULL; }
static int cliConnect(void) { char err[ANET_ERR_LEN]; int fd; fd = anetTcpConnect(err,config.hostip,config.hostport); if (fd == ANET_ERR) { fprintf(stderr,"Connect: %s\n",err); return -1; } anetTcpNoDelay(NULL,fd); return fd; }
static int cliConnect(void) { char err[ANET_ERR_LEN]; static int fd = ANET_ERR; if (fd == ANET_ERR) { fd = anetTcpConnect(err,config.hostip,config.hostport); if (fd == ANET_ERR) { fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err); return -1; } anetTcpNoDelay(NULL,fd); } return fd; }
void MessagingInterface::connect() { if (protocol == eCLOCKWORK || protocol == eZMQ || protocol == eCHANNEL) { if (pthread_equal(owner_thread, pthread_self())) { FileLogger fl(program_name); fl.f() << hostname<<":"<<port <<" socket connect being called from a thread that isn't the owner\n"; } socket->connect(url.c_str()); int linger = 0; socket->setsockopt (ZMQ_LINGER, &linger, sizeof (linger)); } else { char error[ANET_ERR_LEN]; connection = anetTcpConnect(error, hostname.c_str(), port); if (connection == -1) { MessageLog::instance()->add(error); std::cerr << error << "\n"; } } }
int main(){ int fd; char err[128]; char* addr="127.0.0.1"; int port = 9999; initWinSock(); fd = anetTcpConnect(err, addr, port); if(fd <= 0){ printf("%s\n",err); return 1; } PutRequestMessage* msg = dynamic_cast<PutRequestMessage*>(Message::createMessage(PUTREQUESTMSG)); msg->setKey("asd",3); msg->setValue("zxc",3); msg->serialize(); int ret = anetWrite(fd,msg->getBuffer(),msg->getBufferSize()); printf("ret : %d\n",ret); closesocket(fd); }
int syncWithMaster(void) { char buf[1024], tmpfile[256], authcmd[1024]; int fd = anetTcpConnect(NULL,server.masterhost,server.masterport); int dfd, maxtries = 5; if (fd == -1) { redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* AUTH with the master if required. */ if(server.masterauth) { snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth); if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) { close(fd); redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* Read the AUTH result. */ if (syncReadLine(fd,buf,1024,3600) == -1) { close(fd); redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s", strerror(errno)); return REDIS_ERR; } if (buf[0] != '+') { close(fd); redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?"); return REDIS_ERR; } } /* Issue the SYNC command */ if (syncWrite(fd,"SYNC \r\n",7,5) == -1) { close(fd); redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* Prepare a suitable temp file for bulk transfer */ while(maxtries--) { snprintf(tmpfile,256, "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid()); dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644); if (dfd != -1) break; sleep(1); } if (dfd == -1) { close(fd); redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno)); return REDIS_ERR; } /* Setup the non blocking download of the bulk file. */ if (aeCreateFileEvent(server.el, fd, AE_READABLE, readSyncBulkPayload, NULL) == AE_ERR) { close(fd); redisLog(REDIS_WARNING,"Can't create readable event for SYNC"); return REDIS_ERR; } server.replstate = REDIS_REPL_TRANSFER; server.repl_transfer_left = -1; server.repl_transfer_s = fd; server.repl_transfer_fd = dfd; server.repl_transfer_lastio = time(NULL); server.repl_transfer_tmpfile = zstrdup(tmpfile); return REDIS_OK; }
// //========================================================================= // int main(int argc, char **argv) { int j, fd; struct client *c; // Set sane defaults view1090InitConfig(); signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program) // Parse the command line options for (j = 1; j < argc; j++) { int more = ((j + 1) < argc); // There are more arguments if (!strcmp(argv[j],"--net-bo-port") && more) { Modes.net_input_beast_port = atoi(argv[++j]); } else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) { strcpy(View1090.net_input_beast_ipaddr, argv[++j]); } else if (!strcmp(argv[j],"--modeac")) { Modes.mode_ac = 1; } else if (!strcmp(argv[j],"--interactive-rows") && more) { Modes.interactive_rows = atoi(argv[++j]); } else if (!strcmp(argv[j],"--interactive")) { Modes.interactive = 1; } else if (!strcmp(argv[j],"--interactive-ttl") && more) { Modes.interactive_display_ttl = atoi(argv[++j]); } else if (!strcmp(argv[j],"--interactive-rtl1090")) { Modes.interactive = 1; Modes.interactive_rtl1090 = 1; } else if (!strcmp(argv[j],"--lat") && more) { Modes.fUserLat = atof(argv[++j]); } else if (!strcmp(argv[j],"--lon") && more) { Modes.fUserLon = atof(argv[++j]); } else if (!strcmp(argv[j],"--metric")) { Modes.metric = 1; } else if (!strcmp(argv[j],"--no-crc-check")) { Modes.check_crc = 0; } else if (!strcmp(argv[j],"--fix")) { Modes.nfix_crc = 1; } else if (!strcmp(argv[j],"--no-fix")) { Modes.nfix_crc = 0; } else if (!strcmp(argv[j],"--aggressive")) { Modes.nfix_crc = MODES_MAX_BITERRORS; } else if (!strcmp(argv[j],"--help")) { showHelp(); exit(0); } else { fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]); showHelp(); exit(1); } } // Initialization view1090Init(); // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. if ((fd = anetTcpConnect(Modes.aneterr, View1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) == ANET_ERR) { fprintf(stderr, "Failed to connect to %s:%d\n", View1090.net_input_beast_ipaddr, Modes.net_input_beast_port); exit(1); } // // Setup a service callback client structure for a beast binary input (from dump1090) // This is a bit dodgy under Windows. The fd parameter is a handle to the internet // socket on which we are receiving data. Under Linux, these seem to start at 0 and // count upwards. However, Windows uses "HANDLES" and these don't nececeriy start at 0. // dump1090 limits fd to values less than 1024, and then uses the fd parameter to // index into an array of clients. This is ok-ish if handles are allocated up from 0. // However, there is no gaurantee that Windows will behave like this, and if Windows // allocates a handle greater than 1024, then dump1090 won't like it. On my test machine, // the first Windows handle is usually in the 0x54 (84 decimal) region. if (fd >= MODES_NET_MAX_FD) { // Max number of clients reached fprintf(stderr, "Max number of clients exceeded : fd = 0x%X\n", fd); close(fd); exit(1); } c = (struct client *) malloc(sizeof(*c)); c->buflen = 0; c->fd = c->service = Modes.bis = fd; Modes.clients[fd] = c; if (Modes.maxfd < fd) { Modes.maxfd = fd; } // Keep going till the user does something that stops us while (!Modes.exit) { modesReadFromClient(c,"",decodeBinMessage); interactiveRemoveStaleAircrafts(); interactiveShowData(); } // The user has stopped us, so close any socket we opened if (fd != ANET_ERR) {close(fd);} pthread_exit(0); return (0); }
int syncWithMaster(void) { char buf[1024], tmpfile[256], authcmd[1024]; long dumpsize; int fd = anetTcpConnect(NULL,server.masterhost,server.masterport); int dfd, maxtries = 5; if (fd == -1) { redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* AUTH with the master if required. */ if(server.masterauth) { snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth); if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) { close(fd); redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* Read the AUTH result. */ if (syncReadLine(fd,buf,1024,3600) == -1) { close(fd); redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s", strerror(errno)); return REDIS_ERR; } if (buf[0] != '+') { close(fd); redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?"); return REDIS_ERR; } } /* Issue the SYNC command */ if (syncWrite(fd,"SYNC \r\n",7,5) == -1) { close(fd); redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s", strerror(errno)); return REDIS_ERR; } /* Read the bulk write count */ if (syncReadLine(fd,buf,1024,3600) == -1) { close(fd); redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s", strerror(errno)); return REDIS_ERR; } if (buf[0] == '-') { close(fd); redisLog(REDIS_WARNING,"MASTER aborted replication with an error: %s", buf+1); return REDIS_ERR; } else if (buf[0] != '$') { close(fd); redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?"); return REDIS_ERR; } dumpsize = strtol(buf+1,NULL,10); redisLog(REDIS_NOTICE,"Receiving %ld bytes data dump from MASTER",dumpsize); /* Read the bulk write data on a temp file */ while(maxtries--) { snprintf(tmpfile,256, "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid()); dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644); if (dfd != -1) break; sleep(1); } if (dfd == -1) { close(fd); redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno)); return REDIS_ERR; } while(dumpsize) { int nread, nwritten; nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024); if (nread <= 0) { redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s", (nread == -1) ? strerror(errno) : "connection lost"); close(fd); close(dfd); return REDIS_ERR; } nwritten = write(dfd,buf,nread); if (nwritten == -1) { redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno)); close(fd); close(dfd); return REDIS_ERR; } dumpsize -= nread; } close(dfd); if (rename(tmpfile,server.dbfilename) == -1) { redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno)); unlink(tmpfile); close(fd); return REDIS_ERR; } emptyDb(); if (rdbLoad(server.dbfilename) != REDIS_OK) { redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk"); close(fd); return REDIS_ERR; } server.master = createClient(fd); server.master->flags |= REDIS_MASTER; server.master->authenticated = 1; server.replstate = REDIS_REPL_CONNECTED; return REDIS_OK; }
// //========================================================================= // int main(int argc, char **argv) { int j, fd; struct client *c; // Set sane defaults ppup1090InitConfig(); signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program) // Parse the command line options for (j = 1; j < argc; j++) { int more = ((j + 1) < argc); // There are more arguments if (!strcmp(argv[j],"--net-bo-port") && more) { Modes.net_input_beast_port = atoi(argv[++j]); } else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) { strcpy(ppup1090.net_input_beast_ipaddr, argv[++j]); } else if (!strcmp(argv[j],"--quiet")) { ppup1090.quiet = 1; } else if (!strcmp(argv[j],"--help")) { showHelp(); exit(0); } else { fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]); showHelp(); exit(1); } } #ifdef _WIN32 // Try to comply with the Copyright license conditions for binary distribution if (!Modes.quiet) {showCopyright();} #endif // Initialization ppup1090Init(); // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. if ((fd = anetTcpConnect(Modes.aneterr, ppup1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) == ANET_ERR) { fprintf(stderr, "Failed to connect to %s:%d\n", ppup1090.net_input_beast_ipaddr, Modes.net_input_beast_port); exit(1); } // // Setup a service callback client structure for a beast binary input (from dump1090) // This is a bit dodgy under Windows. The fd parameter is a handle to the internet // socket on which we are receiving data. Under Linux, these seem to start at 0 and // count upwards. However, Windows uses "HANDLES" and these don't nececeriy start at 0. // dump1090 limits fd to values less than 1024, and then uses the fd parameter to // index into an array of clients. This is ok-ish if handles are allocated up from 0. // However, there is no gaurantee that Windows will behave like this, and if Windows // allocates a handle greater than 1024, then dump1090 won't like it. On my test machine, // the first Windows handle is usually in the 0x54 (84 decimal) region. c = (struct client *) malloc(sizeof(*c)); c->next = NULL; c->buflen = 0; c->fd = c->service = Modes.bis = fd; Modes.clients = c; // Keep going till the user does something that stops us while (!Modes.exit) { modesReadFromClient(c,"",decodeBinMessage); interactiveRemoveStaleAircrafts(); postCOAA (); } // The user has stopped us, so close any socket we opened if (fd != ANET_ERR) {close(fd);} closeCOAA (); #ifndef _WIN32 pthread_exit(0); #else return (0); #endif }