int CoreSocket::connect(const GSockAddr &server, int iFLTag, int *fd, int nodelay) { int type = SOCK_STREAM; int ret; *fd = ::socket(server.family(), type, 0); if (*fd == -1) return LS_FAIL; if (iFLTag) ::fcntl(*fd, F_SETFL, iFLTag); if ((nodelay) && ((server.family() == AF_INET) || (server.family() == AF_INET6))) ::setsockopt(*fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(int)); ret = ::connect(*fd, server.get(), server.len()); if (ret != 0) { if (!((iFLTag & O_NONBLOCK) && (errno == EINPROGRESS))) { ::close(*fd); *fd = -1; } return LS_FAIL; } return ret; }
int CoreSocket::bind(const GSockAddr &server, int type, int *fd) { int ret; if (!server.get()) return EINVAL; *fd = ::socket(server.family(), type, 0); if (*fd == -1) return errno; int flag = 1; if (setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, (char *)(&flag), sizeof(flag)) == 0) { ret = ::bind(*fd, server.get(), server.len()); if (!ret) return ret; } ret = errno; ::close(*fd); *fd = -1; return ret; }