I have a client side source code that connects to server over internet.
When I tracing my code it shows me the SIGPIPE error when the compiler run send code. where is my problem?
I changed send method as the comment say and used strcpy.
But I have error again.
#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();
}