TCP C客户端不发送,但telnet到服务器
我已经做了一个简单的服务器,将消息一起收到消息时,将打印到终端。当我telnet并发送消息到服务器时,我发送的客户端只是发送消息(不会收到),它工作的很好,并且打印出我发送的内容。但是,当我编写在其下面找到的C客户端代码时,就好像没有发送任何内容。TCP C客户端不发送,但telnet到服务器
这里是我的编译输出下面的代码-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]
[email protected]:/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;
}
更新代码 连接返回 “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;
}
我看到你还没有纠正头文件问题(即你仍然收到有关隐式声明的警告)。不要被它现在可以工作的事实所愚弄,隐含的声明问题会在某一天回来并给你一个难题。学会立即系统地解决警告。 – fvu
你有解决方案我不知道你说的警告是什么标题,干杯队友! – Xenland
'man strlen'和'man bzero'无论是在您的系统或谷歌。联机帮助页面的“简介”段落将始终显示您需要的#include。另外,正如其他人已经指出的(以及相关的[manpage](http://linux.die.net/man/3/bzero)),bzero是一个不推荐的函数,新程序应该使用它的替换[memset](http ://linux.die.net/man/3/memset) – fvu
检查connect'的'的返回值。 – cnicutar
'char sendline =“...”'?这不是字符串! –
也许你还需要发送换行符('\ r \ n')? (但也看Joachim的评论) –