0

メッセージと一緒にメッセージを受信したときに端末に出力する単純なサーバーを作成しました。私が作成したクライアントは、Telnetで送信してサーバーにメッセージを送信すると、メッセージを送信するだけで(受信しません)、正常に機能し、送信した内容を正確に出力します。ただし、その下にあるCクライアントコードを書くと、何も送信されないかのようになります。

これが-wall-wextra-pedanticを使用した以下のコードのコンパイル出力です

  ./client.c: In function ‘main’:
./client.c:17:4: warning: implicit declaration of function ‘bzero’ [-Wimplicit-function-declaration]
./client.c:19:4: warning: implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
./client.c:28:2: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
./client.c:28:41: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
./client.c:22:8: warning: unused variable ‘val’ [-Wunused-variable]
./client.c:10:15: warning: unused variable ‘n’ [-Wunused-variable]
./client.c:8:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
./client.c:8:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
soup@soup-XPS-8300:/home/c_dev/p2pcrypt_server/v0.1.0/src/tool_tests/libev

-

/* Sample TCP client */

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr;
   char * sendline;
   sendline = "hello";

   sockfd=socket(AF_INET,SOCK_STREAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
   servaddr.sin_port=htons(8018);

    int connect_status = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    printf("%d\n", connect_status);

    int send_status = send(sockfd,sendline,strlen(sendline),0);
    printf("%d\n", send_status);

    printf("END\n");
    return 0;
}

更新されたコードConnectは「0」(成功)を返し、sendは「5」を返します(送信した成功した文字の数だと思います)

4

1 に答える 1

0

これが私の側で正常に機能しているコードです。\r\ nが必要だったようで、指摘されたすべてのすばらしい提案があります。仲間に感謝します!

/* Sample TCP client */

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr;
   char * sendline;
   sendline = "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellolohellohellohellohellohellohellohellohellohellolohellohellohellohellohellohellohellohellohellolohellohellohellohellohellohellohellohellohellolo\r\n";

   sockfd=socket(AF_INET,SOCK_STREAM,0);

   memset(&servaddr, 0, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
   servaddr.sin_port=htons(8018);

    int connect_status = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    printf("%d\n", connect_status);

    int send_status = send(sockfd,sendline,strlen(sendline),0);
    printf("%d\n", send_status);

    printf("END\n");
    return 0;
}
于 2013-02-02T12:56:38.573 に答える