QT += network
接著在程式.h檔中加入
#include < QtNetwork/QTcpSocket >
//宣告一個QTcpSocket方便在整個程式中使用
QTcpSocket *m_socket;
在程式.cpp裡
//產生QTcpSocket物件
m_socket = new QTcpSocket(this);
//Connect事件觸發的Function
//在收到Server的訊息時會觸發readRead()
connect(m_socket,SIGNAL(readyRead()),this,SLOT(slotReadyRead()));
//連線成功的事件
connect(m_socket,SIGNAL(connected()),this,SLOT(slotConnected()));
//斷線的事件
connect(m_socket,SIGNAL(disconnected()),this,SLOT(slotDisConnected()));
//連線到Server
QString ip_address="127.0.0.1";
int port = 2266
m_socket->connectToHost(ip_address,port );
//傳送訊息
QTextStream stream(m_socket);
stream << "Server: Hi, I am client";
//接收訊息
m_socket->readAll();
參考網址
Qt Network Tutorial
http://www.qtforum.org/article/9/qt-network-tutorial.html
Blocking Fortune Client Example
http://2s22.ing.ula.ve/qt/network-blockingfortuneclient.html
How to write Socket program in Qt 4.7 for Mobiles?
http://discussion.forum.nokia.com/forum/showthread.php?217630-How-to-write-Socket-program-in-Qt-4.7-for-Mobiles
Qt Network Tutorial
http://www.qtforum.org/article/9/qt-network-tutorial.html
Blocking Fortune Client Example
http://2s22.ing.ula.ve/qt/network-blockingfortuneclient.html
How to write Socket program in Qt 4.7 for Mobiles?
http://discussion.forum.nokia.com/forum/showthread.php?217630-How-to-write-Socket-program-in-Qt-4.7-for-Mobiles
沒有留言:
張貼留言