现在做网站开发吗,一个网站是怎么建立的,企业网站seo贵不贵,西安推广公司无网不胜客户端使用lua、服务端使用QT做为服务器。 步骤#xff1a; 客户端 -----------Post 用户名和密码 服务端接受Post请求#xff0c;读取数据#xff0c;返回response一、客户端代码 loadingImg requireapp.scenes.LoadingLayerlocal LoginScene class( 客户端 -----------Post 用户名和密码 服务端接受Post请求读取数据返回response 一、客户端代码 loadingImg requireapp.scenes.LoadingLayerlocal LoginScene class(LoginScene, function()return display.newScene(LoginScene)
end)function LoginScene:ctor()print(LoginScene)self.loading loadingImg:new()self.loading:addTo(self)self:removeChild(self.loading)local function onRequestCallback(event)local request event.request--dump(event)if event.name completed thenprint(request:getResponseHeadersString())local code request:getResponseStatusCode()if code ~ 200 then-- 请求结束但没有返回 200 响应代码print(code)returnendprint(---------------callback--------)-- 请求成功显示服务端返回的内容print(response length .. request:getResponseDataLength())local response request:getResponseString()print(response)elseif event.name progress thenprint(progress .. event.dltotal)else-- 请求失败显示错误代码和错误消息print(event.name)print(request:getErrorCode(), request:getErrorMessage())returnendendlocal request network.createHTTPRequest(onRequestCallback, 127.0.0.1:19999, POST)--request:addPOSTValue(name, laoliu)request:setPOSTData(user:123456,password:123456)request:start()
endfunction LoginScene:onEnter()
endfunction LoginScene:onExit()endreturn LoginScene 二、客户端lua代码核心介绍 network.createHTTPRequest(onRequestCallback, 127.0.0.1:19999, POST) --request:addPOSTValue(name, laoliu) request:setPOSTData(user:123456,password:123456) 三、服务端QT代码 在.pro文件中追加 QT core gui network .h文件 #ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include qsctpserver.h
#include qtcpsocket.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();
public slots:void onNewConnection();void acceptConnection();void readMessage();void disconnected();void deleteLater();
private:Ui::MainWindow *ui;QTcpServer * serverListen;QTcpSocket *serverConnect;
};#endif // MAINWINDOW_H .cpp #include mainwindow.h
#include ui_mainwindow.h#include QLocalSocketMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);serverListen new QTcpServer;serverConnect new QTcpSocket;serverListen-listen(QHostAddress::Any,19999);connect(serverListen,SIGNAL(newConnection()),this,SLOT(acceptConnection()));}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::onNewConnection()
{int temp1;
}void MainWindow::acceptConnection()
{serverConnect serverListen-nextPendingConnection(); //得到每个连进来的socketconnect(serverConnect,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息触发读函数
}void MainWindow::readMessage() //读取信息
{
// ui-textEdit_rec-teQByteArray qba serverConnect-readAll(); //读取qDebug()qba;QString ssQVariant(qba).toString();QString info(ss);QStringList tokens(info.split( QRegExp([ /r/n][ /r/n]*)));qDebug()tokens;if ( tokens[0] GET )//getDeal(serverConnect);{ qDebug()get;}if( tokens[0] POST)//postDeal(serverConnect);{ qDebug()POST;}serverConnect-write(HTTP/1.1 200 OK\r\n);serverConnect-close();
}void MainWindow::disconnected()
{qDebug()disconnected;
}void MainWindow::deleteLater()
{qDebug()deleteLater;
} 核心基于这两个类进行tcp/ip 操作 QTcpServer * serverListen;QTcpSocket *serverConnect; QTcpServer的基本操作: 1、调用listen监听端口。 2、连接信号newConnection在槽函数里调用nextPendingConnection获取连接进来的socket。 QTcpSocket的基本能操作 1、调用connectToHost连接服务器。 2、调用waitForConnected判断是否连接成功。 3、连接信号readyRead槽函数异步读取数据。 4、调用waitForReadyRead阻塞读取数据。 四、tcp、ip实现http的过程 HTTP通信机制是在一次完整的HTTP通信过程中Web浏览器与Web服务器之间将完成下列7个步骤 1. 建立TCP连接 在HTTP工作开始之前Web浏览器首先要通过网络与Web服务器建立连接该连接是通过TCP来完成的该协议 与IP协议共同构建Internet即著名的TCP/IP协议族因此Internet又被称作是TCP/IP网络。HTTP是比TCP更 高层次的应用层协议根据规则只有低层协议建立之后才能进行更高层协议的连接因此首先要建立TCP 连接一般TCP连接的端口号是80。 2. Web浏览器向Web服务器发送请求命令 一旦建立了TCP连接Web浏览器就会向Web服务器发送请求命令。例如GET/sample/hello.jsp HTTP/1.1。 3. Web浏览器发送请求头信息 浏览器发送其请求命令之后还要以头信息的形式向Web服务器发送一些别的信息之后浏览器发送了一空白 行来通知服务器它已经结束了该头信息的发送。 4. Web服务器应答 客户机向服务器发出请求后服务器会客户机回送应答 HTTP/1.1 200 OK 应答的第一部分是协议的版本 号和应答状态码。 5. Web服务器发送应答头信息 正如客户端会随同请求发送关于自身的信息一样服务器也会随同应答向用户发送关于它自己的数据及被请求 的文档。 6. Web服务器向浏览器发送数据 Web服务器向浏览器发送头信息后它会发送一个空白行来表示头信息的发送到此为结束接着它就以 Content-Type应答头信息所描述的格式发送用户所请求的实际数据。 7. Web服务器关闭TCP连接 一般情况下一旦Web服务器向浏览器发送了请求数据它就要关闭TCP连接然后如果浏览器或者服务器在其 头信息加入了这行代码Connection:keep-alive TCP连接在发送后将仍然保持打开状态于是浏览器可以继续通过相同的连接发送请求。保持连接节省了为 每个请求建立新连接所需的时间还节约了网络带宽。 转载于:https://www.cnblogs.com/hiwoshixiaoyu/p/10034949.html