#include #include #include #include #include #include #include #include #include "logwriter.h" #include "status.h" LogWriter::LogWriter(QWidget *parent) : QDialog(parent) { hostLabel = new QLabel(tr("Message:")); hostLineEdit = new QLineEdit("fortune"); serverName = "/var/tmp/qt_c_socket_test"; hostLabel->setBuddy(hostLineEdit); statusLabel = new QLabel(tr("This examples requires that you run the " "LogReceiver example as well.")); connectToLogReceiver = new QPushButton(tr("Connect")); connectToLogReceiver->setDefault(true); quitButton = new QPushButton(tr("Quit")); writeButton = new QPushButton(tr("Write")); buttonBox = new QDialogButtonBox; buttonBox->addButton(connectToLogReceiver, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); buttonBox->addButton(writeButton, QDialogButtonBox::ActionRole); connect(hostLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableWriteButton())); connect(connectToLogReceiver, SIGNAL(clicked()), this, SLOT(requestNewFortune())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(writeButton, SIGNAL(clicked()), this, SLOT(writeToLogReceiver())); QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(hostLabel, 0, 0); mainLayout->addWidget(hostLineEdit, 0, 1); mainLayout->addWidget(statusLabel, 2, 0, 1, 2); mainLayout->addWidget(buttonBox, 3, 0, 1, 2); setLayout(mainLayout); setWindowTitle(tr("Fortune Client")); hostLineEdit->setFocus(); } LogWriter::~LogWriter() { } void LogWriter::requestNewFortune() { struct sockaddr_un serv_addr; connectToLogReceiver->setEnabled(false); sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd < 0) qDebug() << "ERROR opening socket" << strerror(errno); serv_addr.sun_family = AF_UNIX; strcpy(serv_addr.sun_path, "/var/tmp/qt_c_socket_test"); if (::connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) qDebug() << "ERROR connecting" << strerror(errno); } void LogWriter::readFortune() { } void LogWriter::displayError(QLocalSocket::LocalSocketError socketError) { } void LogWriter::enableWriteButton() { writeButton->setEnabled(!hostLineEdit->text().isEmpty()); } void LogWriter::writeToLogReceiver() { writeToLogReceiver(STAT_OK,DISCOVER); } void LogWriter::writeToLogReceiver(int stat, int sub_stat) { char * st = (char *) malloc(sizeof(int)*4+1); char * sst = (char *) malloc(sizeof(int)*4+1); sprintf(st,"%d", stat); sprintf(sst,"%d",sub_stat); char * msg = "msg test"; int msize = strlen(st) + strlen(sst) + strlen(msg) + 3; char * m = (char *) malloc(msize); strcpy(m, ""); strcat(m, st); strcat(m, ";"); strcat(m, sst); strcat(m, ";"); strcat(m, msg); //char buffer[256]; //char *b = "test"; qDebug() << strlen(m); qDebug() << m; int n = ::write(sockfd, m, strlen(m)); if (n < 0) qDebug() << "ERROR writing to socket"; free(st); free(sst); free(m); }