qt--qtcpserver与qtcpsocket

qtcpserver

void receive_para::onStart() {
	if(!tcpserver)
		tcpserver = new QTcpServer();
	if (!tcpserver->listen(QHostAddress::Any, 5001)) {
		qDebug() << "receive_para  listen fail!\n";
		return;
	}
	connect(tcpserver, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
	qDebug() << "receive_para  start";
}
void receive_para::onStop() {
	if (tcpserver && tcpserver->isListening())
	{
		tcpserver->disconnect(SIGNAL(newConnection()));
		tcpserver->close();
	}
	qDebug() << "receive_para stop";
}
void receive_para::on_newConnection() {
	QTcpSocket *tcp = tcpserver->nextPendingConnection();
	connect(tcp, SIGNAL(readyRead()), this, SLOT(onReadMessage()));
	connect(tcp, &QAbstractSocket::disconnected, tcp, &QObject::deleteLater);
	qDebug() << "receive_para on_newConnection" ;
}
void receive_para::onRestartServer() {
	if (tcpserver)
		delete tcpserver;
	onStart();
	qDebug() << "receive_para onRestartServer" ;
}
void receive_para::onReadMessage()
{
	qDebug() << "receive";
	tcpSocket = static_cast(sender());
	qint64 len;
	while ((len = tcpSocket->read((char*)&receive_buff, 每次读取字节)>= 每次读取字节)
	{}
}
void receive_para::start_stop_signal(char* start_stop) {
	if (strcmp(start_stop, "start")==0)
	{
		if (tcpserver && tcpserver->isListening())
			qDebug() << "already start";
		else{
			onStart();
		}
	}
	if (strcmp(start_stop, "stop") == 0)
	{
		if (tcpserver && tcpserver->isListening())
		{
			onStop();
		}
		else
			qDebug() << "already stop";
	}	
}

qtcpsocet

void send_para::set_ip_Port(QString ip_, quint32 port_) {
	ip = ip_;
	port = port_;
}
void send_para::onStart() {
	if (!tcpclient)
	{
		tcpclient = new QTcpSocket;
	}
	tcpclient->abort(); //先停止
	tcpclient->connectToHost(QHostAddress(ip), port);
	if (tcpclient->waitForConnected(5000))
	{
		qDebug() << "send_para connect success!\n";
		state_start = true;
		if(!m_pTimer)
			m_pTimer = new QTimer();
		m_pTimer->setInterval(5000);//1000ms
		connect(m_pTimer, &QTimer::timeout, this, &send_para::send_interval_handle);
		m_pTimer->start();
	}
	else
		qDebug() << "send_para connect fail!\n";
}
void send_para::onStop() {
	tcpclient->abort(); //先停止
	state_start = false;
	m_pTimer->disconnect(SIGNAL(timeout()));
	m_pTimer->stop();
	qDebug() << "send_para stop";
}
void send_para::onDisconnect() {
	tcpclient->abort(); //先停止
	tcpclient->connectToHost(ip, port);
	if (tcpclient->waitForConnected(5000))
	{
		qDebug() << "send_para reconnect success!\n";
		state_start = true;
	}
	else
		qDebug() << "send_para reconnect fail!\n";
}
bool send_para::check_connect_state() {
	return tcpclient->state() == QAbstractSocket::ConnectedState;
}
void send_para::displayError(QAbstractSocket::SocketError socketError) {
	switch (socketError) {
	case QAbstractSocket::RemoteHostClosedError:
		qDebug() << "send_para: RemoteHostClosedError";
		break;
	case QAbstractSocket::HostNotFoundError:
		qDebug() << "send_para: HostNotFoundError";
		break;
	case QAbstractSocket::ConnectionRefusedError:
		qDebug() << "send_para: ConnectionRefusedError";
		break;
	default:
		qDebug() << "send_para: otherError";
	}
}
void send_para::start_stop_signal(char* start_stop,QString ip_,quint32 port_) {
	if (strcmp(start_stop, "start") == 0)
		if (tcpclient && check_connect_state())
			qDebug() << "already listening!\n";
		else
		{
			set_ip_Port(ip_, port_);
			onStart();
		}
	if (strcmp(start_stop, "stop") == 0)
		if (tcpclient &&check_connect_state())
			onStop();
		else
			qDebug() << "already close!\n";
}
void send_para::send_interval_handle() 
{
	if (state_start&&!check_connect_state())
		onDisconnect();
	if (state_start&&check_connect_state())
	{
		tcpclient->write((char*)&数据, 字节); 
		tcpclient->flush();
	}
}

你可能感兴趣的:(#,qt,qt)