Windows under the QT foreground and Linux under the background program through the socket communication

Source: Internet
Author: User
Tags htons

Usually, the background program under Linux does not need the GUI to display, and the foreground program often has an interface to facilitate interaction with the user. The example shown in this article is that the QT program interacts with the background Linux process (C language), and the content that is transmitted through the socket is a struct. Because QT itself is a cross-platform framework, porting the front-end programs to other platforms can still work well.

The structure is defined as follows:

struct Test
{
int A;
Char b;
};

qt Program:Windows system, using the server classes provided by Qt Qtcpserver, qtcpsocket and related functions.

C program under Linux: connect the server and send the structure through the socket programming interface provided by Linux.

The key code for the QT program is as follows:

void Tcpserverdemo::on_listenbutton_clicked ()
{
This->tcpserver = new TCPServer (this);

QString IP;
ip = Ui->iplineedit->text ();
QString Port;
Port = Ui->portlineedit->text ();

if (! This->tcpserver->listen (qhostaddress (IP), (quint16) (Port.toint ())))
{
Qmessagebox::warning (This, TR ("TCP Server"),
TR ("Listen error:%1"). Arg (this->tcpserver->errorstring ()));
Return
}
Ui->listenbutton->setenabled (FALSE);
}

void tcpserver::incomingconnection (int socketdescriptor)
{
This->tcpsocket = new Qtcpsocket (this);
This->tcpsocket->setsocketdescriptor (Socketdescriptor);
Qobject::connect (This->tcpsocket, SIGNAL (Readyread ()),
This, SLOT (On_ready_read ()));
}

void Tcpserver::on_ready_read ()
{
struct Test t;
This->tcpsocket->read ((char *) &t,sizeof (t));
Qmessagebox::warning (NULL, TR ("TCP Server"),
TR ("a=%1 b=%2"). Arg (T.A). Arg (t.b));
}

Key code for Linux under C program:

int sockfd;
struct sockaddr_in servaddr;//, myaddr;
struct test* p;
Char Buf[n] = {0};
ssize_t N;
if (argc! = 3)
{
printf ("usage:%s serverip serverport\n", argv[0]);
return 0;
}
if ((SOCKFD = socket (pf_inet, sock_stream, 0)) = = = 1)
{
Perror ("socket");
return-1;
}
memset (&servaddr, 0, sizeof (SERVADDR));
servaddr.sin_family = pf_inet;
Servaddr.sin_port = htons (atoi (argv[2));//error htons ("6000");
SERVADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
if ( -1 = = Connect (sockfd, (struct sockaddr *) &servaddr, sizeof (SERVADDR)))
{
Perror ("Connect");
return-1;
}
p = (struct Test *) buf;
printf ("Input a B:");
scanf ("%d", &p->a);
GetChar ();
scanf ("%c", &p->b);
printf ("a=%d b=%d\n", P->a, p->b);
if ( -1 = = Send (SOCKFD, p, sizeof (BUF), 0))
{
Perror ("send");
return-1;
}
memset (buf, 0, N);
if ((n = recv (SOCKFD, buf, N, 0)) = = =-1)
{
Perror ("recv");
return-1;
}
p = (struct Test *) buf;
printf ("a=%d b=%c", P->a, p->b);
printf (">");
Close (SOCKFD);


This article is from the "Embedded Learning World" blog, please be sure to keep this source http://farsight.blog.51cto.com/1821374/1529539

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.