EchoClient を実装したが動かず
EchoServer ができているので、動作確認のためのクライアントも書いてみました。
#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <netinet/in.h> int error_exit(char *message) { perror(message); exit(1); } int checknumber(char *v){ int len = strlen(v); int i; int result = 1; for(i = 0; i < len && result ; i++){ result = isdigit(v[i]); } return result; } int main(int argc, char *args[]){ if( argc != 3 && argc != 4 ) { printf("Usage: %s $ListenAddress $ListenPort $EchoMessage\n", args[0]); printf(" %s $ListenAddress $EchoMessage\n", args[0]); } else { if( checknumber(args[2]) != 1 ){ printf("%s is not number, the 2nd arguments has to be a number.\n", args[2]); } else { int sock; struct sockaddr_in destination_addr; unsigned short destination_port; char *echo_buffer[256]; unsigned int message_length; int receivedbytes, totalreceivedbyte; if( argc == 3 ){ destination_port = 7; } else if( argc == 4 ){ destination_port = atoi(args[2]); } // Socket open, this is a 1st step. if( (sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ){ error_exit("socket() failed.\n"); } // Build Destination Socket Informations memset(&destination_addr, 0, sizeof(destination_addr)); destination_addr.sin_family = AF_INET; destination_addr.sin_addr.s_addr = inet_addr(args[1]); destination_addr.sin_port = destination_port; if( connect(sock, (struct sockaddr *)&destination_addr, sizeof(destination_addr)) < 0 ){ error_exit("connect() failed.\n"); } // Check echo message length message_length = strlen(args[3]); // Send message if( send(sock, args[3], message_length, 0) != message_length ){ error_exit("send() finished uncompletely...\n"); } } } return 0; }
動作確認。まずは、EchoServerV2 - Shammerismのサーバーを以下のように動かしておく。
$ ./a.out 10080 Start Listening Port : 10080...
そして、上記クライアントのビルドと実行。
$ gcc -o eclient echoclient.c $ ./eclient localhost 10080 Test connect() failed. : Permission denied $
同一マシン上の 10080 で動いているはずなのに・・・と思い、いろいろ試してみて、C での実装だと IP とホスト名の変換は別に実装してやらないといけないのでは?と気付いて、localhost でなく、127.0.0.1 で試した。
$ ./eclient 127.0.0.1 10080 Test connect() failed. : Connection refused
しかし状況は変わらず。FW が動いていないことまで確認したが・・・他にどんな要因があるのだろう。