当前位置: 首页 > news >正文

建立音乐网站网络媒体设计是什么

建立音乐网站,网络媒体设计是什么,阿里云域名注册入口官网,进入兔展网站做PPt最近要在QT下开发Tcp通讯#xff0c;发送序列化数据以便于接收。 这里涉及到几个问题#xff1a; 1.QTcpSocket、QTcpServer的通讯 2.QDataStream序列化数据 多的不说#xff0c;直接上干货#xff01;#xff01;#xff01; 客户端#xff1a; tcpclient.h 1 #ifndef …最近要在QT下开发Tcp通讯发送序列化数据以便于接收。 这里涉及到几个问题 1.QTcpSocket、QTcpServer的通讯 2.QDataStream序列化数据   多的不说直接上干货     客户端 tcpclient.h 1 #ifndef TCPCLIENT_H2 #define TCPCLIENT_H3 4 #include QMainWindow5 #include qt4/Qt/qtcpsocket.h6 #include Qt/qhostinfo.h7 #include QDataStream8 #include QtNetwork9 10 11 12 struct Control_Motor 13 { 14 int length; 15 int command; 16 QString data; 17 }; 18 19 20 class Motor 21 { 22 public: 23 Motor(){} 24 Motor(int speed,int accele_speed,int p_some) 25 { 26 m_speed speed; 27 m_accele_speed accele_speed; 28 m_p_some p_some; 29 } 30 31 32 33 public: 34 int getV(){return m_speed;} 35 int getA(){return m_accele_speed;} 36 int getP(){return m_p_some;} 37 38 void setV(const int v){m_speed v;} 39 void setA(const int a){m_accele_speed a;} 40 void setP(const int p){m_p_some p;} 41 42 43 44 public: 45 //friend QDataStream operator (QDataStream out,const Motor motor); 46 //friend QDataStream operator (QDataStream in,Motor motor); 47 /* 48 friend QDataStream operator (QDataStream out,Motor motor) 49 { 50 out motor.m_speedmotor.m_p_somemotor.m_accele_speed; 51 qDebug() motor.m_speedmotor.m_p_somemotor.m_accele_speedEnter in this; 52 return out; 53 } 54 */ 55 public: 56 int m_speed; 57 int m_accele_speed; 58 int m_p_some; 59 60 }; 61 62 63 namespace Ui { 64 class TcpClient; 65 } 66 67 class TcpClient : public QMainWindow 68 { 69 Q_OBJECT 70 71 public: 72 explicit TcpClient(QWidget *parent 0); 73 ~TcpClient(); 74 75 private: 76 Ui::TcpClient *ui; 77 QTcpSocket *tcpClient; 78 79 80 private slots: 81 void slotConnect(); 82 void readMessage(); 83 void displayError(QAbstractSocket::SocketError); 84 void sendMessage(); 85 86 87 88 public: 89 Control_Motor control_Motor; 90 91 Motor *m_motor; 92 93 }; 94 95 96 #endif // TCPCLIENT_H tcpclient.cpp 1 #include tcpclient.h2 #include ui_tcpclient.h3 #include QFile4 #include QDataStream5 6 7 QDataStream operator(QDataStream out,const Motor motor)8 {9 //qDebug() motor.m_speedmotor.m_p_somemotor.m_accele_speedEnter in;10 out motor.m_speedmotor.m_p_somemotor.m_accele_speed;11 //qDebug() motor.m_speedmotor.m_p_somemotor.m_accele_speedEnter in this;12 return out;13 }14 15 QDataStream operator (QDataStream in,Motor motor)16 {17 int speed 0;18 int accele_speed 0;19 int p_some 0;20 21 in speed p_some accele_speed;22 23 motor.setV(speed);24 motor.setP(p_some);25 motor.setA(accele_speed);26 27 return in;28 }29 Q_DECLARE_METATYPE(Motor)30 31 32 TcpClient::TcpClient(QWidget *parent) :33 QMainWindow(parent),34 ui(new Ui::TcpClient)35 {36 ui-setupUi(this);37 38 qRegisterMetaTypeMotor(Motor);39 40 tcpClient new QTcpSocket(this);41 connect(ui-Connect_Btn,SIGNAL(clicked()),this,SLOT(slotConnect()));42 connect(ui-Send_Btn,SIGNAL(clicked()),this,SLOT(sendMessage()));43 connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readMessage()));44 connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this,45 SLOT(displayError(QAbstractSocket::SocketError)));46 }47 48 TcpClient::~TcpClient()49 {50 delete ui;51 }52 53 void TcpClient::slotConnect()54 {55 //QString stringAddress ui-ldt_IP-text();56 //QString stringPort ui-ldt_Port-text();57 QString stringAddress 192.168.154.128;58 QString stringPort 8080;59 int port stringPort.toInt();60 QHostAddress address;61 address.setAddress(stringAddress);62 tcpClient-abort();63 tcpClient-connectToHost(address,port);64 ui-Connect_Btn-setEnabled(false);65 }66 67 void TcpClient::readMessage()68 {69 70 }71 72 void TcpClient::displayError(QAbstractSocket::SocketError)73 {74 qDebug() tcpClient-errorString();75 }76 77 void TcpClient::sendMessage()78 {79 QString stringMotor ui-ldt_Motor-text();80 QString stringData ui-ldt_data-text();81 control_Motor.command stringMotor.toInt();82 int dataLength stringData.length();83 control_Motor.length 8 dataLength;84 control_Motor.data stringData;85 QString data stringMotorstringData;86 87 m_motor new Motor(20,40,60);88 89 //Motor m_motor(20,40,60);90 91 //用于暂存要发送的数据92 QByteArray block;93 //使用数据流写入数据94 QDataStream out(block,QIODevice::WriteOnly);95 //设置数据流的版本客户端和服务器端使用的版本要相同96 out.setVersion(QDataStream::Qt_4_6);97 out(quint32) 0;98 //设置发送长度初始值为099 //out control_Motor.lengthcontrol_Motor.commandcontrol_Motor.data; 100 101 //qDebug() control_Motor.lengthcontrol_Motor.commandcontrol_Motor.data; 102 //out 103 out control_Motor.command; 104 qDebug()Start outendl; 105 out *m_motor; 106 qDebug()End outendl; 107 qDebug() control_Motor.command m_motor-getA()m_motor-getP()m_motor-getV(); 108 //回到字节流起始位置 109 out.device()-seek(0); 110 //重置字节流长度 111 //out (quint16) (block.size()-sizeof(quint16)); 112 out (quint32)(block.size()- sizeof(quint32)); 113 qDebug() block.size()block.size(); 114 115 //往套接字缓存中写入数据并发送 116 tcpClient-write(block,block.size()); 117 tcpClient-disconnectFromHost(); 118 tcpClient-waitForDisconnected(); 119 120 block.resize(0); 121 this-close(); 122 //tcpClient-write(data.toLatin1(),data.size()); 123 }   服务器端 tcpserver.h 1 ifndef TCPSERVER_H2 #define TCPSERVER_H3 4 #include QMainWindow5 #include qt4/Qt/qhostinfo.h6 #include server.h7 8 9 namespace Ui { 10 class TcpServer; 11 } 12 13 class TcpServer : public QMainWindow 14 { 15 Q_OBJECT 16 17 public: 18 explicit TcpServer(QWidget *parent 0); 19 ~TcpServer(); 20 21 private: 22 Ui::TcpServer *ui; 23 int port; 24 Server *server; 25 26 protected slots: 27 void slotCreateServer(); 28 void updateServer(QString,int); 29 30 }; 31 32 #endif // TCPSERVER_H   tcpserver.cpp 1 #include tcpserver.h2 #include ui_tcpserver.h3 #include QtNetwork/QNetworkInterface4 5 TcpServer::TcpServer(QWidget *parent) :6 QMainWindow(parent),7 ui(new Ui::TcpServer)8 {9 ui-setupUi(this); 10 port 8080; 11 QString address QNetworkInterface::allAddresses().first().toString(); 12 QListQHostAddress list2 QNetworkInterface::allAddresses(); 13 foreach (QHostAddress address, list2) 14 { 15 if(address.protocol() QAbstractSocket::IPv4Protocol) 16 ui-ldt_IP-setText(address.toString()); 17 } 18 ui-ldt_Port-setText(QString::number(port)); 19 connect(ui-Connect_Btn,SIGNAL(clicked()),this,SLOT(slotCreateServer())); 20 } 21 22 TcpServer::~TcpServer() 23 { 24 delete ui; 25 } 26 27 void TcpServer::slotCreateServer() 28 { 29 server new Server(this,port); 30 connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int))); 31 ui-Connect_Btn-setEnabled(false); 32 } 33 34 void TcpServer::updateServer(QString msg, int length) 35 { 36 ui-lwt_Text-addItem(msg.left(length)); 37 }   server.h 1 #ifndef SERVER_H2 #define SERVER_H3 4 #include qt4/Qt/qtcpserver.h5 #include qt4/Qt/qtcpsocket.h6 7 struct Control_Motor8 {9 int length; 10 int command; 11 QString data; 12 }; 13 14 15 class Motor 16 { 17 public: 18 Motor(int speed,int accele_speed,int p_some) 19 { 20 m_speed speed; 21 m_accele_speed accele_speed; 22 m_p_some p_some; 23 } 24 25 Motor(){m_speed 0;} 26 27 28 29 public: 30 int getV(){return m_speed;} 31 int getA(){return m_accele_speed;} 32 int getP(){return m_p_some;} 33 34 void setV(const int v){m_speed v;} 35 void setA(const int a){m_accele_speed a;} 36 void setP(const int p){m_p_some p;} 37 38 39 40 private: 41 int m_speed; 42 int m_accele_speed; 43 int m_p_some; 44 45 }; 46 47 48 49 class Server : public QTcpServer 50 { 51 Q_OBJECT 52 53 public: 54 Server(QObject *parents0,int port0); 55 QListQTcpSocket*TcpClientSocketList; 56 QTcpSocket *tcpClientSocket; 57 signals: 58 void updateServer(QString,int); 59 60 public slots: 61 void slotUpdateClient(QString,int); 62 void slotDisconnect(int); 63 // void slotnewconnection(); 64 protected: 65 void incomingConnection(int socketDescriptor); 66 67 signals: 68 void updateClients(QString,int); 69 void disconnected(int); 70 71 protected slots: 72 void dataReceive(); 73 void slotDisconnected(); 74 75 public: 76 Control_Motor control_motor; 77 Motor m_mtor; 78 }; 79 80 #endif // SERVER_H   server.cpp 1 #include server.h2 3 QDataStream operator (QDataStream out,Motor motor)4 {5 out motor.getV()motor.getP()motor.getA();6 return out;7 }8 9 10 QDataStream operator (QDataStream in,Motor motor)11 {12 int speed motor.getV();13 int accele_speed motor.getA();14 int p_some motor.getP();15 16 in speed p_some accele_speed;17 18 motor.setV(speed);19 motor.setP(p_some);20 motor.setA(accele_speed);21 22 return in;23 }24 25 26 27 28 Server::Server(QObject *parent,int port) :29 QTcpServer(parent)30 {31 this-listen(QHostAddress::Any,port);32 }33 34 void Server::incomingConnection(int socketDescriptor)35 {36 tcpClientSocket new QTcpSocket(this);37 //tcpClientSocket this-nextPendingConnection();38 tcpClientSocket-setSocketDescriptor(socketDescriptor);39 TcpClientSocketList.append(tcpClientSocket);40 //connect(this,SIGNAL(updateClients(QString,int)),this,SLOT(slotUpdateClient(QString,int)));41 //connect(this,SIGNAL(updateClients(QString,int)),this,SLOT(slotUpdateClient(QString,int)));42 connect(this,SIGNAL(disconnected(int)),this,SLOT(slotDisconnect(int)));43 connect(tcpClientSocket,SIGNAL(readyRead()),this,SLOT(dataReceive()));44 connect(tcpClientSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));45 //connect(tcpClientSocket,SIGNAL(disconnected()),tcpClientSocket,SLOT(deleteLater()));46 47 }48 void Server::slotUpdateClient(QString msg,int length)49 {50 emit updateServer(msg,length);51 for (int i0;iTcpClientSocketList.count();i)52 {53 QTcpSocket *itemTcpClientSocketList.at(i);54 if (item-write(msg.toLatin1(),length)!length)55 continue;56 }57 }58 void Server::slotDisconnect(int socketDescriptor)59 {60 qDebug()__FILE____FUNCTION____LINE__endl;61 for (int i0;iTcpClientSocketList.count();i)62 if (TcpClientSocketList.at(i)-socketDescriptor()socketDescriptor)63 {64 TcpClientSocketList.removeAt(i);65 return;66 }67 }68 69 void Server::dataReceive()70 {71 qDebug()QQWQW11111111111111;72 //quint32 size 0;73 quint32 nextBlockSize 0;74 qDebug()TcpClientSocketList.count()TcpClientSocketList.count();75 //for(int i 0; i TcpClientSocketList.count();i)76 //{77 QDataStream in(tcpClientSocket);78 in.setVersion(QDataStream::Qt_4_6);79 if(nextBlockSize 0)80 {81 if(tcpClientSocket-bytesAvailable()sizeof(quint32))82 {83 //break;84 return;85 }86 innextBlockSize;87 qDebug()nextBlockSize;88 }89 if(nextBlockSize0xFFFF)90 {91 //break;92 return;93 }94 if(tcpClientSocket-bytesAvailable()nextBlockSize)95 {96 //break;97 return;98 }99 100 //incontrol_motor.lengthcontrol_motor.commandcontrol_motor.data; 101 102 //qDebug()control_motor.lengthcontrol_motor.commandcontrol_motor.data; 103 in control_motor.command m_mtor; 104 qDebug()control_motor.command m_mtor.getA()m_mtor.getP()m_mtor.getV(); 105 106 //ui-SN_lineEdit_2-setText(QString(%1).arg(message_rev.SN)); 107 //ui-IP_lineEdit-setText(message_rev.IP); 108 //ui-STATE_lineEdit_3-setText(message_rev.Condition); 109 //} 110 } 111 112 void Server::slotDisconnected() 113 { 114 qDebug()__FILE____FUNCTION____LINE__endl; 115 emit disconnected(tcpClientSocket-socketDescriptor()); 116 }     在这里要特别说明一下在此遇到的几个问题希望能帮到大家也提醒一下自己。 1.在TcpClient.proTcpServer.pro里一定要注意加上QT network不然编译的时候QNetworkInterface、QHostAddress这些地方会报错误。 2.在QDataStream重载自定义的类的时候一开始把重载写在.h文件里编译的时候总是报Tcp_DataStream/TcpServer/server.h:49: error: multiple definition of operator(QDataStream, Motor)这个错误说实话这个问题真的困扰了我很久然后把重载函数放到.cpp文件里,编译通过简直是要命 3.QDataStream的nextBlock读取到的长度为发送的数据的长度而不是整个包的长度out (quint32)(block.size()- sizeof(quint32));如果直接写out (quint32)(block.size()读的时候数据将会不正常。 4.重载的时候一定要正确。  转载于:https://www.cnblogs.com/wanzaiyimeng/p/4572074.html
http://www.pierceye.com/news/462570/

相关文章:

  • html网站怎么进入后台网站建设完成之后要索取哪些
  • 做炭化料的网站国外可以做非法网站吗
  • 厦门 网站建设 网站开发 未来网络做百科专用参考链接的网站
  • 手机网站友情链接怎么做网站轮播图
  • 网站做支付宝花呗分期设计师联盟网是谁创建的
  • 辽宁手机版建站系统开发高平市规建设局网站
  • 免费电子商务网站建设个人网站心得
  • 2003 iis网站发布网站c2g的代表性电商平台
  • 用asp做网站的可行性分析哪个网站做美食视频
  • 瓷砖网站模板建设网站虚拟主机
  • 陇西哪里能学做网站百度识图网页版在线使用
  • 如果自己弄网站书签制作 小学生 一等奖
  • 连江网站建设wordpress页面文章列表
  • 国外jquery特效网站网站建设的英语
  • 建立网站最好的模板夜蝶直播app下载安装
  • 重庆学校网站建设wordpress 模版安装
  • 公司要招个做网站的人商标设计网站猪八戒
  • 古建设工程造价管理协会网站小程序引流推广平台
  • 网站域名缴费十年外贸建设企业网站服务
  • 游戏网站外链建设网站开发项目的需求分析
  • 确定网站的主题与风格东营市建设项目工伤保险是哪个网站
  • 如何做一张网站平面效果图装宽带需要多少钱
  • 什么网站能让小孩做算术题厦门住房和城乡建设局网站
  • 网站上线过程建设网站什么费用
  • 企业网站域名在哪申请成都广告公司
  • 手机页面网站模板怎么卖宁波网站建设优化找哪家
  • 深圳网站外包公司注册公司流程和费用找哪家
  • 专业新站整站快速排名公司商业网站和企业网站的区别
  • 网站做等保备案河北建设厅网站登陆怎么找附件
  • 网站前台显示数据库指定分类怎么做php优化大师官方免费