gpt4 book ai didi

c++ - socket编程发送数据时出现Broken Pipe错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:18 27 4
gpt4 key购买 nike

<分区>

我有一个通过互联网连接到服务器的客户端源代码。
当我跟踪我的代码时,它会在编译器运行发送代码时向我显示 SIGPIPE 错误。我的问题在哪里?
我按照评论所说更改了发送方法并使用了 strcpy。
但是我又出错了。

#include <QCoreApplication>
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <string> /*To use string type*/
#include <iostream>
#include <string>
#include <QChar>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <typeinfo>//for print the type typeid(a).name --> int
#include <string>
#include <arpa/inet.h>
#include <signal.h>
//#include <pthread.h> //make thread
using namespace std ;

//________________
#define bufsize 100
int sock;
struct sockaddr_in server;
string str;
char* message = new char[bufsize];
char* server_reply = new char[bufsize];
void RECV()
{
memset(&server_reply,'\0',bufsize);
int a=recv(sock , server_reply ,bufsize , 0);
cout<<a;
if(server_reply[0]!='\0')
cout<<server_reply<<endl;

}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
sock = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);//0
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("example.com");//
server.sin_family = AF_UNSPEC;//AF_INET;
server.sin_port = htons( 3490 );


//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

puts("Connected\n");
puts("Bienvenido al Chatroom, puedes empezar a escribir en la sala!");

strcpy(message,"Hi");
signal(SIGPIPE, SIG_IGN);
if((send(sock , message , strlen(message)+1 , 0))<0)
{
perror("send");
exit(1);
}
RECV();
close(sock);
return a.exec();
}

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