Qt:The bound address is already in use解决及QTcpServer如何停止监听

The bound address is already in use解决

Qt tcp服务器重复监听同个端口时会出错:The bound address is already in use

// maybe occur error:The bound address is already in use
QTcpServer tcp1, tcp2;
tcp1.setMaxPendingConnections(500);
tcp1.listen(QHostAddress::Any, 1000);
tcp2.setMaxPendingConnections(500);
tcp2.listen(QHostAddress::Any, 1000);

windows解决方法

设置端口为可复用。以下是伪代码

QTcpServer tcp1, tcp2;
int on = 1;
setsockopt(sock1, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
tcp1.setSocketDescriptor(sock1);
on = 1;
setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
tcp2.setSocketDescriptor(sock2);

完整代码

#pragma comment(lib,"ws2_32.lib")
SOCKET create_tcp_server(unsigned int lmax, unsigned short lport) {

	int r = -1;
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA wsaData;
	r = WSAStartup(sockVersion, &wsaData);
	if (r != 0) return -1;
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	int on = 1;
	r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
	if (r != 0) return -1;
	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(lport);
	sin.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(sock, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) {

		return -1;
	}
	if (listen(sock, lmax) == SOCKET_ERROR) {

		return -1;
	}
	return sock;
}

// how to use
QTcpServer tcp_;
if (tcp_.setSocketDescriptor(create_tcp_server(500, 1000))) {

	QObject::connect(&tcp_, SIGNAL(newConnection()), this, SLOT(on_new_connection()));
	QObject::connect(&tcp_, SIGNAL(acceptError(QAbstractSocket::SocketError)),
		this, SLOT(on_server_error(QAbstractSocket::SocketError)));
}

linux解决方法

同Windows,按照Linux的socket api复现create_tcp_server函数即可。

QTcpServer如何停止监听

以下代码无法停止监听

QTcpServer tcp_;
tcp_.close();

正确停止监听的开启方式

QTcpServer tcp_;
tcp_.disconnect();
tcp_.close();

 

你可能感兴趣的:(图形界面,Qt,Gtk,WxWidgets,编程语言,C,C++,Python)