Implement qt udp Communication on Windows
Today, we use QT to Implement UDP communication,The simulator sends a message to the host and the host is displayed.. Very simple.
1.
We will first complete the host part and use vs2005 to complete itThe following code does not explain much:
# Include <stdio. h> # include <winsock2.h> # pragma comment (Lib, "ws2_32.lib") # define server_port 45454int main () {/* init Winsock */wsadata; if (wsastartup (makeword (2, 1), & wsadata) {printf ("Winsock init failed! \ N "); return 1;} struct sockaddr_in local; struct sockaddr_in from; int Len = sizeof (struct sockaddr_in); Local. sin_family = af_inet; local. sin_port = htons (server_port);/* listener port */local. sin_addr.s_addr = inaddr_any;/* local * // * Create UDP socket */socket sock_fd = socket (af_inet, sock_dgram, 0); BIND (sock_fd, (struct sockaddr *) & local, sizeof (local); char buffer [1024]; int size; while (1) {Buf Fer [0] = '\ 0'; printf ("waiting... \ n "); size = recvfrom (sock_fd, buffer, sizeof (buffer), 0, (struct sockaddr *) & from, & Len); If (size! = Socket_error) {buffer [size] = '\ 0'; printf ("from % s: \ n % s \ n", inet_ntoa (from. sin_addr), buffer) ;}} closesocket (sock_fd); wsacleanup (); Return 0 ;}
2.
Complete the QT section. Create a new "qt gui application ":
Enter "udpsender ":
Select one of the three platforms:
Select qwidget:
Created!
3.
Modify widget. UI and add a text edit with the ID textsend. Add a Pushbutton with the ID btnsend:
4.
Modify the widget. h as follows and add qudpsocket:
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QtNetwork/QUdpSocket>namespace Ui { class Widget;}const QString SERVER_ADDR = "127.0.0.1";const int SERVER_PORT = 45454;class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private: Ui::Widget *ui; QUdpSocket *udpSocket;private slots: void sendText();};#endif // WIDGET_H
Note: The above server_addr is the IP address of the local machine. You can replace it with the IP address of another host in the LAN. In addition, server_port must be the same as the port of the first Windows host.
5.
Modify widget. cpp as follows:
#include "widget.h"#include "ui_widget.h"#include <QByteArray>Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); udpSocket = new QUdpSocket(this); QObject::connect(ui->btnSend, SIGNAL(clicked()), this, SLOT(sendText()));}Widget::~Widget(){ delete ui;}void Widget::sendText(){ QByteArray datagram = ui->textSend->toPlainText().toAscii(); udpSocket->writeDatagram(datagram, datagram.size(), QHostAddress(SERVER_ADDR), SERVER_PORT);}
6.
Finally, modify udpsender. Pro as follows:
#-------------------------------------------------## Project created by QtCreator 2012-06-07T12:54:34##-------------------------------------------------QT += core guiTARGET = udpsenderTEMPLATE = appSOURCES += main.cpp\ widget.cppHEADERS += widget.hFORMS += widget.uiQT += networkCONFIG += mobilityMOBILITY = symbian { TARGET.UID3 = 0xe7254336 # TARGET.CAPABILITY += TARGET.EPOCSTACKSIZE = 0x14000 TARGET.EPOCHEAPSIZE = 0x020000 0x800000}
Note: QT + = network is very important! In addition, target. uid3 does not need to be modified.
7.
Finally, select the target QT simulator. The running result is as follows:
8.
Run it on the mobile phone: Because my Nokia 5230 does not support WiFi, I have to change to another mobile phone. Here I leave it blank:
Finally, I uploaded the Code:
Done!