Segmentation fault error
-
@i have a structure as follows
@
@struct image_data
@{
int msgid;
const char * fname;
}@am assigning values to this structure as msgid =6 and fname = a.bmp
then am sending the structure on udp to some other application on different machine
and also am doing the qftp of this bmp fileam able to get this file on other machine
now my motto is to display the image file
so what am doing is am displaying the fname which is filename of the image on the gui of other application.so when am displaying the structure data at the console am getting segmentation fault error
when i have tried file name with the single char am getting the filename with single char at receiving end but when am trying with char * it is showing segmentation fault erroram attaching the code here
@
@#include "GroundThread.h"struct Message
{
int message_id;
}msg;struct img_file
{
int msg_id;
const char* fname;
}imgData;GroundThread::GroundThread(QObject *parent)
:QThread(parent)
{
//restart = false;
//abort = false;
}
GroundThread::~GroundThread()
{}
void GroundThread::run()
{
process_data();}
void GroundThread::process_data()
{
std::cerr<<"am in thread"<<std::endl;
//received_ftpData();
Message msg;
printf("Hello\n");
int fd;printf("Hello2\n");
struct sockaddr_in servaddr, cliaddr;void **status;
fd = socket(AF_INET,SOCK_DGRAM,0);
printf("Hello3\n");if(fd <0)
cout<<"Socket creation Failed"<<endl;servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("10.66.48.29"); //server ip addr
servaddr.sin_port = htons(12000); //server poet no.
printf("Hello4\n");
if(bind(fd,(const struct sockaddr *)&servaddr,sizeof(servaddr))<0)
cout<<"BIND FAILED"<<endl;
unsigned char buffer[MAX_DATA_SIZE];socklen_t len=sizeof(cliaddr);
fcntl(fd,F_SETFL,O_NONBLOCK);
printf("Hello5\n");
for(;;){
sleep(3);
int n= recvfrom(fd,buffer,sizeof(buffer),0,(struct sockaddr *)&cliaddr,&len);memcpy(&msg,buffer,sizeof(msg));
printf("Hello7\n");
cout<<"Received : bytes"<<n<<" "<<msg.message_id<<endl;
if(msg.message_id ==6)
{
memcpy(&imgData,buffer,sizeof(imgData));
cout<<"img msgid:"<<imgData.msg_id<<endl;
cout<<"img filename:"<<imgData.fname<<endl;}
if(n<1) continue;
// emit received_data(buffer);
// return NULL;
}}@
@please tell me how should i solve this
thank u
-
Hi,
Please enclose your code with coding tags, otherwise it's really hard to read.
Since you are using Qt, why not use QUdpSocket and Qt's container (i.e QByteArray) ?