MOEA_Status MOEA_Init_socket(const int objectives, const int constraints, const char* service) { int gai_errno; int listenfd; int readfd; int writefd; int yes = 1; struct addrinfo hints; struct addrinfo *servinfo = NULL; struct addrinfo *sp = NULL; struct sockaddr_storage their_addr; socklen_t addr_size = sizeof(their_addr); MOEA_Init(objectives, constraints); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (service == NULL) { service = MOEA_DEFAULT_PORT; } if ((gai_errno = getaddrinfo(NULL, service, &hints, &servinfo)) != 0) { MOEA_Debug("getaddrinfo: %s\n", gai_strerror(gai_errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } for (sp = servinfo; sp != NULL; sp = sp->ai_next) { if ((listenfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) { MOEA_Debug("socket: %s\n", strerror(errno)); continue; } /* enable socket reuse to avoid socket already in use errors */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { MOEA_Debug("setsockopt: %s\n", strerror(errno)); } if (bind(listenfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { MOEA_Debug("bind: %s\n", strerror(errno)); close(listenfd); continue; } break; } freeaddrinfo(servinfo); if (sp == NULL) { return MOEA_Error(MOEA_SOCKET_ERROR); } if (listen(listenfd, 1) == -1) { MOEA_Debug("listen: %s\n", strerror(errno)); close(listenfd); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((readfd = accept(listenfd, (struct sockaddr*)&their_addr, &addr_size)) == -1) { MOEA_Debug("accept: %s\n", strerror(errno)); close(listenfd); return MOEA_Error(MOEA_SOCKET_ERROR); } close(listenfd); if ((writefd = dup(readfd)) == -1) { MOEA_Debug("dup: %s\n", strerror(errno)); close(readfd); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((MOEA_Stream_input = fdopen(readfd, "r")) == NULL) { MOEA_Debug("fdopen: %s\n", strerror(errno)); close(readfd); close(writefd); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((MOEA_Stream_output = fdopen(writefd, "w")) == NULL) { MOEA_Debug("fdopen: %s\n", strerror(errno)); fclose(MOEA_Stream_input); close(readfd); close(writefd); return MOEA_Error(MOEA_SOCKET_ERROR); } return MOEA_SUCCESS; }
void MOEA_Error_callback_default(const MOEA_Status status) { MOEA_Debug("%s\n", MOEA_Status_message(status)); MOEA_Terminate(); exit(EXIT_FAILURE); }
MOEA_Status MOEA_Init_socket(const int objectives, const int constraints, const char* service) { int status; int listenfd; int acceptfd; struct addrinfo hints; struct addrinfo *servinfo; struct sockaddr_storage their_addr; socklen_t addr_size = sizeof(their_addr); MOEA_Init(objectives, constraints); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (service == NULL) { service = MOEA_DEFAULT_PORT; } if ((status = getaddrinfo(NULL, service, &hints, &servinfo)) != 0) { MOEA_Debug("getaddrinfo: %s\n", gai_strerror(status)); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((listenfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) { MOEA_Debug("socket: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if (bind(listenfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { MOEA_Debug("bind: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if (listen(listenfd, 1) == -1) { MOEA_Debug("listen: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((acceptfd = accept(listenfd, (struct sockaddr*)&their_addr, &addr_size)) == -1) { MOEA_Debug("accept: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if (close(listenfd) == -1) { MOEA_Debug("close: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((MOEA_Stream_input = fdopen(acceptfd, "r")) == NULL) { MOEA_Debug("fdopen: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } if ((MOEA_Stream_output = fdopen(dup(acceptfd), "w")) == NULL) { MOEA_Debug("fdopen: %s\n", strerror(errno)); return MOEA_Error(MOEA_SOCKET_ERROR); } return MOEA_SUCCESS; }