int _server_init_socket(int port) { struct sockaddr_in sa; int mastersock; bzero(&sa, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = INADDR_ANY; sa.sin_port=htons((u_short)port); pthread_mutex_lock(&mutex_protoent); mastersock = socket(PF_INET, SOCK_STREAM, resolveproto("TCP")); pthread_mutex_unlock(&mutex_protoent); if (mastersock < 0) { return ERR_SOCKET_CREATE; } if (bind(mastersock, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) <0) { return ERR_SOCKET_BIND; } if (listen(mastersock, 1) < 0) { return ERR_SOCKET_LISTEN; } return mastersock; }
int _listen(int port) { struct sockaddr_in sa; int val; int sockfd; val= 1; bzero(&sa, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = INADDR_ANY; sa.sin_port = htons((unsigned short) port); sockfd = socket(PF_INET, SOCK_STREAM, resolveproto("TCP")); if (sockfd < 0) { perror("_listen"); return ERR_SOCKET_CREATE; } setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val)); if (bind(sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { perror("_listen"); return ERR_SOCKET_BIND; } if (listen(sockfd, 1) < 0) { perror("_listen"); return ERR_SOCKET_LISTEN; } return sockfd; }
int _connect(char *address, unsigned short port) { int sockfd = 0; struct sockaddr_in sa; struct hostent *he; in_addr_t addr; bzero(&sa, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_port = htons(port); if (sa.sin_port == 0) { return -1; } /* Assume that an IP address is used. */ pthread_mutex_lock(&mutex_hostent); addr = inet_addr(address); if (addr == INADDR_NONE) { /* If it is not an IP address, assume it is a hostname. */ if ((he = gethostbyname(address)) == NULL) { close(sockfd); pthread_mutex_unlock(&mutex_hostent); return -1; } } else { /* Continue the assumption that an IP address is used. */ if ((he = gethostbyaddr((char *) (&addr), sizeof(addr), AF_INET)) == NULL) { close(sockfd); pthread_mutex_unlock(&mutex_hostent); return -1; } memcpy(&sa.sin_addr, &addr, sizeof(addr)); } memcpy(&sa.sin_addr, he->h_addr_list[0], he->h_length); pthread_mutex_unlock(&mutex_hostent); pthread_mutex_lock(&mutex_protoent); sockfd = socket(PF_INET, SOCK_STREAM, resolveproto("tcp")); pthread_mutex_unlock(&mutex_protoent); if (sockfd == -1) { return sockfd; } if (connect(sockfd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) <0) return -1; return sockfd; }
int _connect(char *address, unsigned short port) { int sockfd = -1; struct sockaddr_in sa; struct hostent *he; in_addr_t addr; bzero(&sa, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_port = htons(port); if (sa.sin_port == 0) { printf("Please specify port on which client listen for request\n"); return -1; } /* Assume that an IP address is used. */ addr = inet_addr(address); if (addr == INADDR_NONE) { /* If it is not an IP address, assume it is a hostname. */ if ((he = gethostbyname(address)) == NULL) { close(sockfd); printf("Please specify correct hostname of box where client running\n"); return -1; } } else { /* Continue the assumption that an IP address is used. */ if ((he = gethostbyaddr((char *) (&addr), sizeof(addr), AF_INET)) == NULL) { close(sockfd); printf("Please specify correct IP of box where client running\n"); return -1; } memcpy(&sa.sin_addr, &addr, sizeof(addr)); } memcpy(&sa.sin_addr, he->h_addr_list[0], he->h_length); sockfd = socket(PF_INET, SOCK_STREAM, resolveproto("tcp")); if (sockfd == -1) { printf("Can't create socket for connection to client\n"); return sockfd; } if (connect(sockfd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) == -1) { printf("Can't connect to client socket\n"); return -1; } return sockfd; }
int serverinit(const char *addr, const char *port, const char *proto) { struct sockaddr_in socketaddr; int mastersock; int trueval = 1; struct hostent *hostinfo; unsigned long ip; socketaddr_init(&socketaddr); if (NULL == addr) { socketaddr.sin_addr.s_addr = INADDR_ANY; } else { if (scanaddr(addr, &ip, NULL) != 4) { LFATAL("Invalid address : %s provided", addr); return -1; } hostinfo = gethostbyaddr((char *) &ip, 4, AF_INET); if (NULL == hostinfo) { LFATAL("gethostbyaddr : %s failed", addr); return -1; } socketaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; } socketaddr_service(&socketaddr, port, proto); mastersock = socket(PF_INET, prototype(proto), resolveproto(proto)); if (mastersock < 0) { LFATAL("couldn't create socket"); return -1; } if (bind (mastersock, (struct sockaddr *) &socketaddr, sizeof(socketaddr)) < 0) { return -1; } setsockopt(mastersock, SOL_SOCKET, SO_REUSEADDR, &trueval, sizeof(trueval)); if (prototype(proto) == SOCK_STREAM) { if (listen(mastersock, 5) < 0) { LFATAL("listen on port %d failed", socketaddr.sin_port); return -1; } } return mastersock; }
int clientconnect(const char *host, const char *port, const char *proto) { struct sockaddr_in socketaddr; int sockid; socketaddr_init(&socketaddr); socketaddr_service(&socketaddr, port, proto); socketaddr_host(&socketaddr, host); sockid = socket(PF_INET, prototype(proto), resolveproto(proto)); if (sockid < 0) { LFATAL("clientconnect socket failed, %s", strerror(errno)); return -1; } if (connect (sockid, (struct sockaddr *) &socketaddr, sizeof(socketaddr)) < 0) { LFATAL("clientconnect connect failed, %s", strerror(errno)); return -1; } return sockid; }