I am in bit of a problematic situation here:
I have a simple C server (can't use Qt TcpServer, limitations of the embedded board) which sends double data (generated by GPIO ports) over tcpsocket using send(). If I have a simple C client (in my Linux PC) then I am able to get the real time data as is generated with the help of recv(). Here is my server code:
int main (void){
int s,b,l,fd,sa,bytes,on=1;
char buf[BUF_SIZE],fname[255];
struct sockaddr_in channel;
long long sum=0;
double average=0;
int i,j;
int srno=0;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s<0)
{
printf("socket creation failure");
exit(0);
}
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
memset(&channel,0,sizeof(channel));
channel.sin_family=AF_INET;
channel.sin_addr.s_addr=htonl(INADDR_ANY);
channel.sin_port=htons(SERVER_PORT);
b=bind(s, (struct sockaddr *)&channel,sizeof(channel));
if(b<0)
{
printf("bind error");
exit(0);
}
listen(s,5);
while(1)
{
printf("waiting for request\n");
sa=accept(s,0,0);
if(sa<0)
{
printf("accept failure");
}
for (i=1; i<=56; i++) //for 10 sec >> 56
{
for(j=1;j<=20;j++)
{
//more than 100 lines of code here
}
average=(sum/20)*1.22;
send(sa, &average, sizeof(average), 0);
//int tmp = htonl((uint32_t)average); //tried this also
//send(sa, &tmp, sizeof(tmp), 0);
// int n = write(sa, &average, sizeof(average)) //and tried this also
// if(n<0){
// printf("error");}
printf("%lld \n", average);
srno++;
sum=0;
average=0;
}
}
remove_gpio();
close(s);
return 0;
}
But I am unable to get to display the data in Qt Client, it is driving me crazy. In my QtextEdit I get non-ASCII characters, whatever I try. inside my startRead() I did
...
QByteArray Data = socket->readAll();
ui->textEdit->append(QString(Data));
...
I also tried using other methods bytesavailable() canreadLine(), and also went through other links but was unsuccessful.
Non-ASCII crazy characters is what I get everytime. Sorry not able to give a pic due to low rep. I need help regarding sending double data through socket and be able to work with the data or atleast display them in a QTextEdit/QPlainTextEdit.
Thanks.