Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QT serverclient #1

Open
sean80221123 opened this issue Jul 22, 2019 · 0 comments
Open

QT serverclient #1

sean80221123 opened this issue Jul 22, 2019 · 0 comments

Comments

@sean80221123
Copy link
Owner

sean80221123 commented Jul 22, 2019

server

//new
QTcpServer* mp_TCPServer = new QTcpServer();

//listen
mp_TCPServer->listen(QHostAddress::Any, port)

//SIGNAL:newConnection() conncet signal and slot functions
newConnection()->

//>nextPendingConnection
mp_TCPSocket = mp_TCPServer->nextPendingConnection();

//SIGNAL:readyRead receive data
connect(mp_TCPSocket, SIGNAL(readyRead()), this, SLOT(ServerReadData()));

//write
int sendRe = mp_TCPSocket->write(sendMsgChar, strlen(sendMsgChar));


client

//new
QTcpSocket *mp_clientSocket = new QTcpSocket();

//connect
mp_clientSocket->connectToHost(ip, port);

//receive
SIGNAL(readyRead())->ClientRecvData()
connect(mp_clientSocket, SIGNAL(readyRead()), this, SLOT(ClientRecvData()));
ClientRecvData()
{
char recvMsg[1024] = {0};
int recvRe = mp_clientSocket->read(recvMsg, 1024);
if(recvRe == -1)
{
QMessageBox::information(this, "QTcpSocket", "connect server data fail!");
return;
}
QString showQstr = recvMsg;
ui->m_recvTextEdit_2->setText(showQstr);
}
SIGNAL(connect())

//write
->write()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant