I have my client with Qt4 C++ and my server is in C. I am trying o send a text from server to client and client should print it out, but It does not work. Is there any way to make that happened?
Client:
void Client::readFortune()
{
//in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
in >> nextFortune;
if (nextFortune == currentFortune) {
QTimer::singleShot(0,
this,
SLOT(requestNewFortune
()));
return;
}
currentFortune = nextFortune;
// It should set it here
statusLabel->setText( currentFortune );
getFortuneButton->setEnabled(true);
}
void Client::readFortune()
{
QDataStream in(tcpSocket);
//in.setVersion(QDataStream::Qt_4_0);
QTextStream out ( stdout) ;
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
QString nextFortune;
in >> nextFortune;
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}
currentFortune = nextFortune;
// It should set it here
statusLabel->setText( currentFortune );
getFortuneButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Server:
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
To copy to clipboard, switch view to plain text mode
Bookmarks