###前提・実現したいこと
「TCP/IPソケットプログラミング C言語編」という書籍でソケットAPIを勉強をしています。
ローカル環境でクライアントとサーバプログラムを使って自分自身にパケットを送信しているのですが、
以下のようなエラーが出てうまくいきません。
send()関数のところでつまずいています。
よろしくお願いします。
###発生している問題・エラーメッセージ
send() sent a different number of bytes than expected: Bad address
###TCPEchoClient.c
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define RCVBUFSIZE 32 void DieWithError(char *errorMessage); int main(int argc, char *argv[]) { int sock; struct sockaddr_in echoServAddr; unsigned short echoServPort; char *servIP; char *echoString; char echoBuffer[RCVBUFSIZE]; unsigned int echoStringLen; int bytesRcvd, totalBytesRcvd; if((argc < 3) || (argc > 4)) { fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]); exit(1); } servIP = argv[1]; echoString = argv[2]; if (argc == 4) echoServPort = atoi(argv[3]); else echoServPort = 7; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); memset(&echoServAddr, 0, sizeof(echoServAddr)); echoServAddr.sin_family = AF_INET; echoServAddr.sin_addr.s_addr = inet_addr(servIP); echoServAddr.sin_port = htons(echoServPort); if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed)"); if (send(sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("send() sent a different number of bytes than expected"); totalBytesRcvd = 0; printf("Received: "); while(totalBytesRcvd < echoStringLen) { if((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed of connection closed prematurely"); totalBytesRcvd += bytesRcvd; echoBuffer[bytesRcvd] = '\0' ; printf(echoBuffer); } printf("\n"); close(sock); exit(0); }
###DieWithError.c
#include <stdio.h> #include <stdlib.h> void DieWithError(char *errorMessage) { perror(errorMessage); exit(1); }
###補足情報(言語/FW/ツール等のバージョンなど)
- wifi環境
- クライアントの宛先アドレスをローカルIPにしている
- コネクションまではうまくいっている

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/28 01:57