Qlocalserver and Qlocalsocket Process communication

Source: Internet
Author: User
Tags readline

In Qt, a variety of IPC methods are available, and the authors use Qlocalserver and Qlocalsocket. It looks like you're on the edge with the socket, but the bottom is Windows name pipe. This should be supported for duplex communication. A qlocalserver #ifndef vxmainwindow_h
#define Vxmainwindow_h#include
#include
#includeclass Qpushbutton;
Class Qtextedit;class Cvxmainwindow:public Qwidget
{
Q_objectpublic:
Cvxmainwindow (Qwidget *parent=null);
~cvxmainwindow ();
Protected
void Resizeevent (Qresizeevent *);
Private Slots:
void Btn_listenclickedslot ();
void Btn_stoplistenclickedslot ();
void Newconnectionslot ();
void DataReceived ();
Private
Qlocalserver *m_pserver;
Qlocalsocket *m_psocket; Qpushbutton *m_pbtn_listen;
Qpushbutton *m_pbtn_stoplisten;
Qtextedit *m_pedt_info;
}; #endif//Vxmainwindow_h#include "VxMainWindow.h" #includeCVxMainWindow:: Cvxmainwindow (Qwidget *parent)
: Qwidget (parent)
{
M_pbtn_listen = new Qpushbutton (QOBJECT::TR ("Start listening"), this);
M_pbtn_stoplisten = new Qpushbutton (QOBJECT::TR ("Stop Listening"), this);
M_pedt_info = new Qtextedit (this);
M_pserver = new Qlocalserver (this); Connect (M_pbtn_listen, SIGNAL (clicked ()), this, SLOT (Btn_listenclickedslot ()));
Connect (M_pbtn_stoplisten, SIGNAL (clicked ()), this, SLOT (Btn_stoplistenclickedslot ()));
Connect (M_pserver, SIGNAL (Newconnection ()), this, SLOT (Newconnectionslot ()));
}cvxmainwindow::~cvxmainwindow ()
{}void cvxmainwindow::resizeevent (qresizeevent *)
{
M_pbtn_listen->setgeometry (10, 5, 80, 20);
M_pbtn_stoplisten->setgeometry (100, 5, 80, 20);
M_pedt_info->setgeometry (0, Max, width (), height ()-30);
}void Cvxmainwindow::btn_listenclickedslot ()
{
if (!m_pserver->islistening ())
{
if (M_pserver->listen (Qobject::tr ("AAA")))
{
M_pedt_info->append (QOBJECT::TR ("Open listening port succeeded! "));
}
Else
{
M_pedt_info->append (QOBJECT::TR ("Open listening port failed! "));
}
}
Else
{
M_pedt_info->append (Qobject::tr ("Listening in ...! "));
}
}void Cvxmainwindow::btn_stoplistenclickedslot ()
{
if (m_pserver->islistening ())
{
M_pserver->close ();
M_pedt_info->append (QOBJECT::TR ("Stop listening! "));
}
}void Cvxmainwindow::newconnectionslot ()
{
M_pedt_info->append (Qobject::tr ("There are new clients connected to the server");
M_psocket = M_pserver->nextpendingconnection ();
Connect (M_psocket, SIGNAL (Disconnected ()), M_psocket, SLOT (Deletelater ()));
Connect (M_psocket, SIGNAL (Readyread ()), this, SLOT (datareceived ())); int length = 0;
QString vmsgstr = qobject::tr ("Welcome");
if ((Length=m_psocket->write (Vmsgstr.tolatin1 (), Vmsgstr.length ()))!=vmsgstr.length ())
{ }
}void Cvxmainwindow::d atareceived ()
{
while (M_psocket->bytesavailable ())
{
QString vtemp;
Vtemp = M_psocket->readline ();
M_pedt_info->append (vtemp); int length = 0;
QString vmsgstr = Qobject::tr ("Reply:") + vtemp;
if ((Length=m_psocket->write (Vmsgstr.tolatin1 (), Vmsgstr.length ()))!=vmsgstr.length ())
{  }
}
} Two Qlocalsocket #ifndef vxmainwindow_h
#define Vxmainwindow_h#include
#includeclass Qpushbutton;
Class Qtextedit;
Class Qlineedit;class Cvxmainwindow:public Qwidget
{
Q_objectpublic:
Cvxmainwindow (Qwidget *parent=null);
~cvxmainwindow ();
Protected
void Resizeevent (Qresizeevent *);
Private Slots:
void Btn_connectclickedslot ();
void Btn_disconnectclickedslot ();
void Btn_sendclickedslot ();
void Connectedslot ();
void Disconnectedslot ();
void DataReceived ();
void DisplayError (Qabstractsocket::socketerror);
Private
Qlocalsocket *m_psocket; Qpushbutton *m_pbtn_connect;
Qpushbutton *m_pbtn_disconnect;
Qtextedit *m_pedt_info;
Qlineedit *m_pedt_send;
Qpushbutton *m_pbtn_send;
}; #endif//Vxmainwindow_h#include "VxMainWindow.h" #includeCVxMainWindow:: Cvxmainwindow (Qwidget *parent)
: Qwidget (parent)
{
M_pbtn_connect = new Qpushbutton (QOBJECT::TR ("Connection Server"), this);
M_pbtn_disconnect = new Qpushbutton (QOBJECT::TR ("Disconnected"), this);
M_pedt_send = new Qlineedit (this);
M_pbtn_send = new Qpushbutton (QOBJECT::TR ("send"), this);
M_pedt_info = new Qtextedit (this);
M_psocket = new Qlocalsocket (this); Connect (M_pbtn_connect, SIGNAL (clicked ()), this, SLOT (Btn_connectclickedslot ()));
Connect (M_pbtn_disconnect, SIGNAL (clicked ()), this, SLOT (Btn_disconnectclickedslot ()));
Connect (M_pbtn_send, SIGNAL (clicked ()), this, SLOT (Btn_sendclickedslot ()));
Connect (M_psocket, SIGNAL (connected ()), this, SLOT (Connectedslot ()));
Connect (M_psocket, SIGNAL (Disconnected ()), this, SLOT (Disconnectedslot ()));
Connect (M_psocket, SIGNAL (Readyread ()), this, SLOT (datareceived ()));
Connect (M_psocket, SIGNAL (Error (QABSTRACTSOCKET::SOCKETERROR)), this, SLOT (DisplayError (qabstractsocket:: (SocketError)));
}cvxmainwindow::~cvxmainwindow ()
{}void cvxmainwindow::resizeevent (qresizeevent *)
{
M_pbtn_connect->setgeometry (10, 5, 80, 20);
M_pbtn_disconnect->setgeometry (100, 5, 80, 20);
M_pedt_send->setgeometry (10, 30, 150, 20);
M_pbtn_send->setgeometry (170, 30, 80, 20);
M_pedt_info->setgeometry (0, Max, width (), height ()-60);
}void Cvxmainwindow::btn_connectclickedslot ()
{
M_psocket->connecttoserver (Qobject::tr ("AAA"));
}void Cvxmainwindow::btn_disconnectclickedslot ()
{
M_psocket->disconnectfromserver ();
}void Cvxmainwindow::btn_sendclickedslot ()
{
int length = 0;
QString vmsgstr = M_pedt_send->text ();
if ((Length=m_psocket->write (Vmsgstr.tolatin1 (), Vmsgstr.length ()))!=vmsgstr.length ())
{
M_pedt_info->append (QOBJECT::TR ("Send Message failed:") + vmsgstr);
}
}void Cvxmainwindow::connectedslot ()
{
M_pedt_info->append (Qobject::tr ("Successfully connected to the server! "));
}void Cvxmainwindow::d isconnectedslot ()
{
M_pedt_info->append (Qobject::tr ("Disconnect from the server! "));
}void Cvxmainwindow::d atareceived ()
{
while (M_psocket->bytesavailable ())
{
QString vtemp;
Vtemp = M_psocket->readline ();
M_pedt_info->append (vtemp);
}
}void Cvxmainwindow::d isplayerror (Qabstractsocket::socketerror)
{}http://blog.chinaunix.net/uid-20718335-id-1993073.html

Qlocalserver and Qlocalsocket Process communication

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.