0

我制作了一个简单的服务器,当收到消息和消息时,它将打印到终端。我制作的客户端只是在我远程登录时发送消息(不接收)并将消息发送到服务器,它工作得很好,并且准确地打印了我发送的内容。但是,当我编写在其下方找到的 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”(成功)并发送返回“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 回答