一起看代码来玩玩QT之13 IO(two TCPServer TcpClient TcpScoket)
来源:互联网 发布:暴雪防沉迷算法 编辑:程序博客网 时间:2024/06/02 19:28
TcpClient.h
#ifndef TCPCLIENT_H#define TCPCLIENT_H#include <QWidget>#include <QTcpSocket>#include <QLineEdit>class TcpClient : public QWidget{ Q_OBJECTpublic: explicit TcpClient(QWidget *parent = 0); QTcpSocket* _socket; QLineEdit* _lineEdit;signals:public slots: void slotButtonClick();};#endif // TCPCLIENT_HTcpClient.cpp
#include "TcpClient.h"#include <QHBoxLayout>#include <QPushButton>TcpClient::TcpClient(QWidget *parent) : QWidget(parent){ _socket = new QTcpSocket(this); _socket->connectToHost("127.0.0.1", 9988); _lineEdit = new QLineEdit; QHBoxLayout* lay = new QHBoxLayout(this); lay->addWidget(_lineEdit); QPushButton* button = new QPushButton("Send"); lay->addWidget(button); connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick())); connect(_lineEdit, SIGNAL(returnPressed()), this, SLOT(slotButtonClick()));}void TcpClient::slotButtonClick(){ QString strText = _lineEdit->text(); if(strText.isEmpty()) return; //net 中不能发QString 类型 _socket->write(strText.toUtf8()); _lineEdit->clear();}
TcpServer.h
#ifndef TCPSERVER_H#define TCPSERVER_H#include <QWidget>#include <QTcpServer>#include <QTcpSocket>#include <QTextBrowser>class TcpServer : public QWidget{ Q_OBJECTpublic: explicit TcpServer(QWidget *parent = 0); QTcpServer* _server; QTcpSocket* _socket; QTextBrowser* _show;signals:public slots: void slotNetConnection(); void slotReadyRead();};#endif // TCPSERVER_H
TcpServer.cpp
#include "TcpServer.h"#include <QHBoxLayout>#include <QNetworkInterface>#include <QMessageBox>#include "ChooseInterface.h"TcpServer::TcpServer(QWidget *parent) : QWidget(parent){ // 创建服务器并监听 _server = new QTcpServer; ChooseInterface dlg; dlg.exec(); QMessageBox::information(NULL,"you select the ip:", dlg._strSelect);// _server->listen(QHostAddress::Any,9988); 全部地址 _server->listen(QHostAddress(dlg._strSelect), 9988); // 当有客户端来连接时,调用slotNetConnection方法 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNetConnection())); _show = new QTextBrowser; QHBoxLayout* lay = new QHBoxLayout(this); lay->addWidget(_show);}void TcpServer::slotNetConnection(){ // 判断是否有未处理的连接 while(_server->hasPendingConnections()) { // 调用nextPeddingConnection去获得连接的socket _socket = _server->nextPendingConnection(); _show->append("New connection ...."); // 为新的socket提供槽函数,来接收数据 connect(_socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); }}void TcpServer::slotReadyRead(){ // 接收数据,判断是否有数据,如果有,通过readAll函数获取所有数据 while(_socket->bytesAvailable() > 0) { _show->append("Data arrived ..... "); QByteArray buf = _socket->readAll(); _show->append(buf); }}
//选择地址类
ChooseInterface.h
#ifndef CHOOSEINTERFACE_H#define CHOOSEINTERFACE_H#include <QDialog>#include <QComboBox>class ChooseInterface : public QDialog{ Q_OBJECTpublic: explicit ChooseInterface(QWidget *parent = 0); QComboBox* _comboBox; QString _strSelect;signals:public slots: void slotComboBoxChange(QString);};#endif // CHOOSEINTERFACE_H
ChooseInterface.cpp
#include "ChooseInterface.h"#include <QNetworkInterface>#include <QVBoxLayout>ChooseInterface::ChooseInterface(QWidget *parent) : QDialog(parent){ /* get all interface 从 <span style="font-family: Arial, Helvetica, sans-serif;">QNetworkInterface 中</span><span style="font-family: Arial, Helvetica, sans-serif;">*/</span> QList<QHostAddress> addrList = QNetworkInterface::allAddresses();#if 0 QList<QNetworkInterface> infList = QNetworkInterface::allInterfaces(); QList<QNetworkAddressEntry> entryList = infList.at(0).addressEntries(); entryList.at(0).broadcast()#endif _comboBox = new QComboBox; QVBoxLayout* lay = new QVBoxLayout(this); lay->addWidget(_comboBox); foreach(QHostAddress addr, addrList) { quint32 ipaddr = addr.toIPv4Address(); //如果不toIPv4Address ,toString后有 IPv4,和 IPv6 if(ipaddr == 0) continue; _comboBox->addItem(QHostAddress(ipaddr).toString()); } connect(_comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotComboBoxChange(QString)));}void ChooseInterface::slotComboBoxChange(QString str){ this->_strSelect = str;}
0 0
- 一起看代码来玩玩QT之13 IO(two TCPServer TcpClient TcpScoket)
- 一起看代码来玩玩QT之13 IO(one 基本)
- 一起看代码来玩玩QT之13 IO(three UDP)
- 一起看代码来玩玩QT之01Hello world
- 一起看代码来玩玩QT之02LineEdit
- 一起看代码来玩玩QT之03coordinate
- 一起看代码来玩玩QT之04 Layout
- 一起看代码来玩玩QT之05 Controls
- 一起看代码来玩玩QT之06Event
- 一起看代码来玩玩QT之07 Painter
- 一起看代码来玩玩QT之10 Graphics
- 一起看代码来玩玩QT之11 Dialog
- 一起看代码来玩玩QT之17 json
- 一起看代码来玩玩QT之19 Process(进程)
- 一起看代码来玩玩QT之21 HttpClient
- 一起看代码来玩玩QT之23 audioRecord
- 一起看代码来玩玩QT之24 AudioPlayer
- 一起看代码来玩玩QT之18QSqlDatabase(two 用QSqlTableModel 来操作database this way very nice)
- Android 消息机制 - Handler, Looper, Message, MessageQueue 的源码分析
- tcp与udp传输对比
- python 异常处理
- 一次完整的HTTP请求所经历的7个步骤
- spring 配置内存溢出监听器、文件上传、ie下载json问题
- 一起看代码来玩玩QT之13 IO(two TCPServer TcpClient TcpScoket)
- 利用Javascript实现隔行变色
- Web开发(一)--Servlet
- poj 2127 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- 6.Sense编辑器(Sense Editor)
- JavaSE学习笔记_5:Java多态
- Servlet的一些笔记
- 产品经理必备软件——Axure使用详解(1)
- day05—html之JSP&Servlet进阶