TCP C客户端不';t发送,但telnet发送到服务器

TCP C客户端不';t发送,但telnet发送到服务器,c,tcp,C,Tcp,我制作了一个简单的服务器,当收到消息时,它会打印到终端。当我telnet并向服务器发送消息时,我制作的客户端只发送消息(不接收),它工作得很好,并准确地打印我发送的内容。然而,当我编写C客户机代码时,在它的下面找到它,就好像什么都没有发送一样 下面是我使用-wall-wextra-pedantic对下面代码的编译输出 ./client.c: In function ‘main’: ./client.c:17:4: warning: implicit declaration of functi

我制作了一个简单的服务器,当收到消息时,它会打印到终端。当我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
-

/*示例TCP客户端*/
#包括
#包括
#包括
int main(int argc,字符**argv)
{
int-sockfd,n;
servaddr中的结构sockaddr_;
字符*发送线;
sendline=“你好”;
sockfd=套接字(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.sinu port=htons(8018);
int connect_status=connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf(“%d\n”,连接状态);
int send_status=send(sockfd,sendline,strlen(sendline),0);
printf(“%d\n”,发送状态);
printf(“END\n”);
返回0;
}
更新代码
Connect返回“0”(成功)并发送返回“5”(我想这是它发送的成功字符数)

这是我这边成功运行的代码,似乎需要\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;
}
/*示例TCP客户端*/
#包括
#包括
#包括
int main(int argc,字符**argv)
{
int-sockfd,n;
servaddr中的结构sockaddr_;
字符*发送线;
发送线="HellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohelloOhellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello\r\n“;
sockfd=套接字(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.sinu port=htons(8018);
int connect_status=connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf(“%d\n”,连接状态);
int send_status=send(sockfd,sendline,strlen(sendline),0);
printf(“%d\n”,发送状态);
printf(“END\n”);
返回0;
}

检查
connect
的返回值
char sendline=“…”
?这不是字符串!也许您还需要发送一个换行符(
\r\n
)(但也请参见Joachim的注释)
sizeof(sendline)+1
将是下一个问题。假设您修复了sendline,您可能需要
strlen(sendline)
因为发送带有警告标志的终止\0文件没有意义
-Wall-Wextra-pedantic
。请始终检查哪些函数返回和/或一组错误和状态代码。然后使用例如
valgrind
(+add
-g
编译标志。)。这样做将大大有助于捕获和跟踪错误。下一步将是
-ggdb
并使用
gdb
运行。我发现您仍然没有纠正头文件问题(即,您仍然收到有关隐式声明的警告)。不要被它现在起作用的事实所愚弄,隐式声明问题总有一天会回来,给你带来困难。学会立即、系统地修复警告。你有解决方案吗?我不知道你所说的警告需要什么标题,干杯,伙计!
man strlen
man bzero
在您的系统或谷歌上。手册页的“概要”段落将始终显示您需要的
#include
。另外,正如其他人已经指出的那样(与此相关的是,bzero是一个不推荐使用的函数,新程序应该使用它的替代品。这让我想到了你:一个很好的例子,说明了你为什么应该注意警告。我建议你阅读整本书。它很短,但信息量很大,很简单。索引:感谢你想到我,并且是我第一个提供背后证据的人这是为什么,干杯!
/* 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;
}