QTcpSocket socket; socket.connectToHost("127.0.0.1", 1234); if(socket.waitForConnected()){ socket.write("Hello World!"); socket.flush(); socket.waitForBytesWritten(); }
QTcpSocket socket; socket.connectToHost("127.0.0.1", 1234); if(socket.waitForConnected()){ QFile file("myfile.txt"); if(file.open(QIODevice::ReadOnly)){ QByteArray data = file.readAll(); socket.write(data); socket.flush(); socket.waitForBytesWritten(); } }First, we create a QTcpSocket object and connect it to the host at IP address 127.0.0.1 and port 1234. Then, we check if the connection was successful using the `waitForConnected()` function. If yes, we open a file called "myfile.txt" using QFile class, read its content using `readAll()` function, and write the contents to the socket using `write()` function. We again flush the stream using `flush()` and wait for the bytes to be written using `waitForBytesWritten()` function. In summary, QTcpSocket is a useful class in the Qt framework library for implementing TCP sockets. It provides several functions for sending and receiving data over a network, making it easy to implement network communication in C++ applications.