gpt4 book ai didi

linux - (TCP)Echo客户端: Automatic disconnect after message sent

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:37 24 4
gpt4 key购买 nike

这里是 TCP 回显客户端的代码。当我让 echo 服务器监听端口 5000 时,客户端会连接,但一旦我键入消息并按 Enter 键,它就会自动断开连接。这里有什么问题吗?

源代码(tcp_ec.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>



#define MAX_BUFFER 1024

void die(char *s)
{
perror(s);
exit(1);
}


int main()

{

int connector,flags,r;
int port;
struct hostent* host;
struct in_addr in;
struct sockaddr_in rmaddr;
bool connected = false;
char sndbuffer[MAX_BUFFER];
char rcvbuffer[MAX_BUFFER];
char hostname[INET_ADDRSTRLEN];
char* exit = "quit";



if((connector = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
perror("socket");
return -1;
}


printf("\n");
printf("Enter the remote hostname(URL/IP4 address): ");
scanf("%s", hostname);
printf("\n");



printf("Enter the port number you wish to connect(on): ");
scanf("%u", &port);
printf("\n");

if(port==0){
printf("ERR0R: Port number must be between 1 & 65,535\n");
printf("\n");
printf("Enter the port number you wish to connect(on): ");
scanf("%u", &port);
printf("\n");
}

host = gethostbyname(hostname);

if(host==NULL){
perror("hostname");
return -1;
}


bzero(&rmaddr,sizeof(rmaddr));
rmaddr.sin_family = AF_INET;
rmaddr.sin_port = htons(port);
bcopy((char*)host->h_addr, (char*)&rmaddr.sin_addr.s_addr, host->h_length);


if(connect(connector,(struct sockaddr*)&rmaddr,sizeof(rmaddr))<0){
perror("connect");
return -1;
}else{
connected=true;
printf("\n");
printf("Connected to host: %s",hostname,"on port %u",port);
printf(" type 'quit' to disconnect\n");
printf("\n");
}

while(connected==true){

printf(">");
scanf("%s",sndbuffer);
printf("\n");

if(sndbuffer==exit){
close(connector);
connected = false;
return 0;
}

if(send(connector,(void*)sndbuffer,sizeof(sndbuffer),MSG_EOR||MSG_NOSIGNAL)<0){
perror("send");
close(connector);
return -1;
}

if(recv(connector,(void*)rcvbuffer,sizeof(rcvbuffer),MSG_EOR||MSG_NOSIGNAL)<0){
perror("recv");
close(connector);
return -1;
}else{
printf(">>");
printf("%s\n",rcvbuffer);
printf("\n");
}




}




}

控制台输出(程序输出文件为“tc”):

zermacr0yd@DALEK /usr/lib/gcc/x86_64-linux-gnu/4.7.3/include $ ./tec

Enter the remote hostname(URL/IP4 address): 127.0.0.1

Enter the port number you wish to connect(on): 5000


Connected to host: 127.0.0.1 type 'quit' to disconnect

>asssr

sendmsg: Broken pipe
zermacr0yd@DALEK /usr/lib/gcc/x86_64-linux-gnu/4.7.3/include $

这是在 echo_server 在端口 5000 上运行时完成的。

最佳答案

替换:

if(send(connector,(void*)sndbuffer,sizeof(sndbuffer),MSG_EOR||MSG_NOSIGNAL)<0){
... and
if(recv(connector,(void*)rcvbuffer,sizeof(rcvbuffer),MSG_EOR||MSG_NOSIGNAL)<0){

作者:

if(send(connector,(void*)sndbuffer,sizeof(sndbuffer),MSG_EOR|MSG_NOSIGNAL)<0){
... and
if(recv(connector,(void*)rcvbuffer,sizeof(rcvbuffer),MSG_EOR|MSG_NOSIGNAL)<0){

...加上一些小错误。

编辑:完整更正源

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>

#define MAX_BUFFER 1024

void die(char *s)
{
perror(s);
exit(1);
}

int main() {
int connector,flags,r;
int port;
struct hostent* host;
struct in_addr in;
struct sockaddr_in rmaddr;
bool connected = false;
char sndbuffer[MAX_BUFFER];
char rcvbuffer[MAX_BUFFER];
char hostname[INET_ADDRSTRLEN];
char* exit = "quit";


if((connector = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
perror("socket");
return -1;
}

printf("\n");
printf("Enter the remote hostname(URL/IP4 address): ");
scanf("%s", hostname);
printf("\n");

printf("Enter the port number you wish to connect(on): ");
scanf("%u", &port);
printf("\n");

if(port==0){
printf("ERR0R: Port number must be between 1 & 65,535\n");
printf("\n");
printf("Enter the port number you wish to connect(on): ");
scanf("%u", &port);
printf("\n");
}
host = gethostbyname(hostname);

if(host==NULL){
perror("hostname");
return -1;
}


bzero(&rmaddr,sizeof(rmaddr));
rmaddr.sin_family = AF_INET;
rmaddr.sin_port = htons(port);
bcopy((char*)host->h_addr, (char*)&rmaddr.sin_addr.s_addr, host->h_length);


if(connect(connector,(struct sockaddr*)&rmaddr,sizeof(rmaddr))<0){
perror("connect");
return -1;
}else{
// connected=true;
printf("\n");
printf("Connected to host: %son port %u", hostname, port);
printf(" type 'quit' to disconnect\n");
printf("\n");
}

// while(connected==true){
while(1){

printf(">");
scanf("%s",sndbuffer);
printf("\n");

if(sndbuffer==exit){
close(connector);
// connected = false;
return 0;
}

if(send(connector,(void*)sndbuffer,sizeof(sndbuffer),MSG_EOR|MSG_NOSIGNAL)<0){
perror("send");
close(connector);
return -1;
}

if(recv(connector,(void*)rcvbuffer,sizeof(rcvbuffer),MSG_EOR|MSG_NOSIGNAL)<0){
perror("recv");
close(connector);
return -1;
}else{
printf(">>");
printf("%s\n",rcvbuffer);
printf("\n");
}
}

return 0;
}

输出:

~/src/usenet$ ./a.out

Enter the remote hostname(URL/IP4 address): 127.0.0.1

Enter the port number you wish to connect(on): 7


Connected to host: 127.0.0.1on port 7 type 'quit' to disconnect

>aap

>>aap

>noot

>>noot

>mies

>>mies

>quit

>>quit

>^C

[对“quit”没有反应是由于缺少strncmp()//memcmp(),加上“quit”实际上是“quit\n”(而不是nul终止)]

关于linux - (TCP)Echo客户端: Automatic disconnect after message sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com