このパッシブ TCP サーバーをループバック インターフェイスでテストする必要があります。IP とポートはコマンド ラインで指定され、getopt
. 次に、TCP ソケットを設定してバインドし、getaddrinfo(3)
ビジー待ちを開始します。
サーバ:
#include "server_utils.h"
#define MAX_CONNECTION 10
#define EXIT_ON_ERROR_(s) { fprintf(stderr, s); exit(EXIT_FAILURE); }
int access_permissions;
int session_status;
int sock_ds, acc_sock_ds;
int main(int argc, char** argv) {
/*...*/
int opt, errsv, client_addr_l;
char *p_string;
struct sockaddr_in client_addr;
struct addrinfo hints;
struct addrinfo *result, *rp;
int check = 0;
/*Parsing command line: port-number retrieving*/
while (( opt = getopt(argc, argv, "p:")) != -1){
check = 1;
switch(opt){
case 'p':
/* String to unsigned long integer with ushort cast */
p_string = optarg;
strtoul(optarg, &optarg, 0);
if(*optarg)
EXIT_ON_ERROR_("String-integer conversion error\n");
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if(!check){
fprintf(stderr, "Usage: %s -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Port number retrieved (%s), server is starting ...\n", p_string);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE|AI_NUMERICSERV;
if(getaddrinfo(NULL, p_string, &hints, &result) != 0)
/*freeaddrinfo(result); if get funct fails it should not be need*/
EXIT_ON_ERROR_("Socket creation error!\n");
for(rp = result; rp != NULL; rp = rp->ai_next){
sock_ds = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock_ds == -1) continue; //try next address
/*A TCP server should usualy set SO_REUSEADDR option on its listening socket*/
int optval = 1;
if( (setsockopt(sock_ds,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))) == -1 ){
EXIT_ON_ERROR_("Error on setsockopt call\n");
freeaddrinfo(result); //???
}
/*See TLPI Section 61.10*/
if(bind(sock_ds, rp->ai_addr, rp->ai_addrlen) == 0)
break; //success
close(sock_ds); //failure, continue
}
if(rp == NULL) {
freeaddrinfo(result); //?
EXIT_ON_ERROR_("Could not bind socket\n");
}
freeaddrinfo(result);
/*Server with passive socket*/
if(listen(sock_ds, MAX_CONNECTION == -1)){
fprintf(stderr, "Listen call error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/*Initializing client_addr_l */
client_addr_l = sizeof( struct sockaddr_in);
printf("Server is ready. Waiting for client connections.\n");
/*Busy-waiting server*/
while(1){
/*...*/
}
サーバー接続は毎回成功します。
クライアント:
#include "client_utils.h"
#define EXIT_ON_ERROR_(s) { fprintf(stderr, s); exit(EXIT_FAILURE); }
int main(int argc, char *argv[]) {
int sock_ds, opt, check_p, check_a;
char * ip, *port;
struct addrinfo hints;
struct addrinfo *result, *rp;
check_p = 0; check_a = 0;
/*port-number, ip address retrieval*/
printf("Command line parsing: port-number, ip-address retrieving...\n");
while ((opt = getopt(argc, argv, "a:p:")) != -1) {
switch (opt) {
case 'p':
/* strtoul only used for string check */
port = optarg;
strtoul(optarg, &optarg, 0);
if (*optarg)
EXIT_ON_ERROR_("Invalid port number string submitted\n");
check_p = 1;
break;
case 'a':
ip = optarg;
/*inet_aton only used for string check*/
if(strcmp(ip, "localhost") != 0 && inet_aton(ip, NULL) == 0)
EXIT_ON_ERROR_("Invalid ip address string submitted\n");
check_a = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -a ip_address ('localhost' for local server)"
" -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (!check_p || !check_a) {
fprintf(stderr, "Usage: %s -a ip_address ('localhost' for local server)"
" -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Command line parsed succesfully: <%s:%s>\n", ip, port);
/*TCP Socket binding and connection*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_next = NULL;
hints.ai_addr = NULL;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV;
if(getaddrinfo(ip, port, &hints, &result) != 0){
/*getaddrinfo(resutl) ??? (same as sever)? */
EXIT_ON_ERROR_("Socket creation error\n");
}
for(rp = result; rp != NULL; rp = rp->ai_next){
sock_ds = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock_ds == -1)
continue;
if(connect(sock_ds, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close(sock_ds);
}
if(rp == NULL){
freeaddrinfo(result); //???
EXIT_ON_ERROR_("Could not connect socket to any address\n");
}
freeaddrinfo(result);
printf("Connection achieved with:%s on port:%s\n", ip, port);
/*...*/
}
私の疑問は freeaddrinfo(3) 関数に関するものです。よく行われたヒープ管理のためにどこに配置するのが安全ですか? 私はそれをソケット作成操作の最後に置き、致命的な終了の前にいくつかのエラー状態にしました。get 操作の malloc が余分なスペースをどのように、そして何をするのか正確にはわかりません。誰かがこれについて説明してもらえますか?