QT Realization LAN Chat tool

Source: Internet
Author: User
Tags get ip

Three years ago just learned to write a QT LAN chat tool small project.

Because it is a beginner qt writing, the code is relatively simple, there is no time to tidy up the project, only for everyone to refer to the relevant TCP and UDP connection and transmission functions and related controls basic use method.

Need to learn the source of e-mail ~


1. Client program Function description

       Connection server:        tcpsocket = new Qtcpsocket (this);        Tcpsocket->connecttohost (serverip,8000);      Disconnect server:        tcpsocket->disconnectfromhost ();



2. Server-Side Program description

            if (Tcpserver->listen (Qhostaddress::any, i)) {              ui->textedit->append ("Listen ok!");             } else{              ui->textedit->append ("Listen error!");             }



3. File Transfer Module
     Send side: Udpsocket = new Qudpsocket (this);   Udpsocket->bind (Qhostaddress::any, 7758);    Connect (Udpsocket, SIGNAL (Readyread ()), This,slot (Readpendingdatagrams ()));    Receive side: Connect (Udpsocket, SIGNAL (Readyread ()), this, SLOT (Readpendingdatagrams ()));    File.setfilename (Filee);    if (!file.open (qiodevice::writeonly | Qiodevice::append | qiodevice::unbuffered))          return;



4 Program Interface








5. Main functions


5.1 Get the native IP address
  QString Feiq_3::getip ()  //Get IP Address {    qlist<qhostaddress> list =  qnetworkinterface::alladdresses ();    foreach (qhostaddress address, list) {       if (address.protocol () = = Qabstractsocket::ipv4protocol) {           if ( Address.tostring (). Contains ("127.0.")) {               continue;}           return address.tostring ();       }    }    return 0;}

5.2 Getting a list of online users
void feiq_3::on_pushbutton_in_clicked ()//connection server key {    QString ServerIP = Ui->lineedit_server->text ();    Connecttoserver (ServerIP);    Ipname.append ("%");    Ipname.append (IP);    Ipname.append ("%");    Ipname.append (Ui->lineedit_name->text ());    Tcpsocket->write (ipname);    Connect (tcpsocket,signal (Readyread ()), this, SLOT (Readmessage ()));} if (Tmp.contains ("   @")) {        ui->listwidget->clear ();        TM = Tmp.split ("   @");        for (int i = 0; i < tm.count (); i + +) {                Ui->listwidget->additem (tm.at (i));        } }


5.3 Private Chat Module
else if (Tmp.startswith ("&&")) {        sm = tmp.split ("&&");        if (sm.at (3) = = IP) {            bbb->chuanmessage ("<font color=blue>[self]</font>" + sm.at (3) + ":<br>" + SM . at (2));            if (sm.at (1) = = IP) {                bbb->chuanmessage ("<font color=red>[each other]</font>" + sm.at (3) + ":<br>" + sm.at (2));            }        }        if (sm.at (1) = = IP) {            if (sm.at (3)! = IP) {                bbb->show ();                Bbb->chuanip (sm.at (3), Ui->lineedit_server->text ());                Bbb->chuanmessage ("<font color=red>[]</font>" + sm.at (3) + ":<br>" + sm.at (2));}            }    


5.4 Select File Features
Fdialog = new Qfiledialog (this);    Fdialog->setfilemode (qfiledialog::existingfiles);    Connect (fdialog,signal (fileselected (const QString &)), This,slot (FS (const QString &));    Fdialog->hide (); void Dialog::fs (const QString & FS) {    //Select File Click Open to start the signal until a single file is opened    Qdebug () < < "FS" <<fs;    Files.clear ();    Files.append (fs);}

5.5 File transfer send-side
void Dialog::senddata () {if (!file.atend ()) {Qbytearray line = File.read (8000);        Udpsocket->writedatagram (Line,qhostaddress (Iprec), 7755);        i++; Qdebug () << "Send over!"        << i << line.size ();            if (Line.size () <8000) {Qbytearray yes; Yes.append ("*^* File receive complete!            ");            Tcpsocket->write (yes); Qmessagebox::warning (THIS,TR ("Notification"), TR ("Send success!")        "), Qmessagebox::yes);        }}}void Dialog::readpendingdatagrams () {while (Udpsocket->haspendingdatagrams ()) {Qbytearray datagram;        Datagram.resize (Udpsocket->pendingdatagramsize ());        Qhostaddress Sender;        Quint16 Senderport;        Udpsocket->readdatagram (Datagram.data (), Datagram.size (), &sender, &senderport);        Qdebug () << datagram;            if (datagram = = "1") {Qdebug () << "Send ok!";        SendData (); }    }}


5.6 File Transfer Receive end
void Dialogrec::initsocket () {    connect (udpsocket, SIGNAL (Readyread ()), this            , SLOT (Readpendingdatagrams ())) ;    File.setfilename (Filee);    if (!file.open (qiodevice::writeonly | Qiodevice::append | qiodevice::unbuffered))        return;    Filee.clear ();} void Dialogrec::readpendingdatagrams () {    int i = 0;        Receive    while (Udpsocket->haspendingdatagrams ()) {        Qbytearray datagram;        Datagram.resize (Udpsocket->pendingdatagramsize ());        Qhostaddress Sender;        Quint16 Senderport;        Udpsocket->readdatagram (Datagram.data (), Datagram.size (), &sender, &senderport);        File.write (Datagram.data (), datagram.size ());        i++;        Udpsocket->writedatagram ("1", 1, sender, 7758);        Qdebug () << i << datagram.size ();    }}

5.7 Window beautification (in the Private Chat window, add "Spring", "Summer", "Autumn", "winter" four kinds of skin.)
void dialog::on_combobox_currentindexchanged (int index) {    if (index==0) {        map.load ("./icon/1.jpg");        Map = map.scaled (qsize (390,330));        Ui->label_back->setpixmap (map);}    if (index==1) {        map.load ("./icon/2.jpg");        Map = map.scaled (qsize (390,330));        Ui->label_back->setpixmap (map);}    if (index==2) {        map.load ("./icon/3.jpg");        Map = map.scaled (qsize (390,330));        Ui->label_back->setpixmap (map);}    if (index==3) {        map.load ("./icon/4.jpg");        Map = map.scaled (qsize (390,330));        Ui->label_back->setpixmap (map);}}


QT Realization LAN Chat tool

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.