저는 서버라는 하나의 프로세스와 서버에 연결하여 메시지를 보내는 클라이언트라는 몇 가지 프로세스가있는 프로그램에서 작업하고 있습니다. 그러나 어떤 이유로 클라이언트가 연결되지 않습니다. 내 서버 :mqd_t, 메시지 송수신
#include "Client.h"
using namespace std;
Client::Client()
{
attr.mq_maxmsg = 10;
attr.mq_msgsize = MSG_SIZE;
attr.mq_flags = 0;
}
void Client::init(QString name)
{
//Convert name into char*
QByteArray byteArray = name.toUtf8();
char str1[40];
const char* tempr = byteArray.constData();
strncpy(str1, tempr, sizeof(str1));
myMailboxName = str1;
//Create temp box to check if name available
string tempS = myMailboxName;
tempS += "new";
const char* tempr1 = tempS.data();
mq_unlink(tempr1);
mq_ownBox = mq_open(tempr1, O_RDONLY | O_CREAT, S_IRWXU, &attr);
mq_centralBox = mq_open(CENTRALBOX, O_RDWR);
//Tell server that you are ready
string tempS1 = str1;
string tempS2 = "started:" + tempS1;
const char* tempr2 = tempS2.data();
sprintf(buf, tempr2);
int tempI = mq_send(mq_centralBox, buf, strlen(buf), 0);
cout << tempI;
//Check for success
if(tempI){
ServerEDialog sd;
sd.setModal(true);
sd.exec();
}
else
{
//If success, wait for response fromserver
while(1)
{
int tempI2 = mq_receive(mq_ownBox, buf, MSG_SIZE, 0);
if(tempI2 != -1)
{
break;
}
}
QString tempS3 = buf;
//if invalid show error, otherwise create permanent mailbox
if(tempS3 == "invalidname")
{
BadUDialog bd;
bd.setModal(true);
bd.exec();
}
else
{
mq_unlink(myMailboxName);
mq_ownBox = mq_open(myMailboxName, O_RDONLY | O_CREAT, S_IRWXU, &attr);
}
}
}
void Client::sendMessage(QString mess)
{
}
이는 다음과 같습니다
#ifndef CLIENT_H
#define CLIENT_H
#include <QString>
#include <string>
#include <QByteArray>
#include <mqueue.h>
#include <iostream>
#include "serveredialog.h"
#include "badudialog.h"
#include "../src/messages.h"
class Client
{
public:
Client();
void init(QString name);
void sendMessage(QString mess);
private:
char *myMailboxName, buf[MSG_SIZE];
struct mq_attr attr;
mqd_t mq_ownBox, mq_centralBox;
};
#endif // CLIENT_H
이 클라이언트 CPP 파일은 다음과 같습니다
이 클라이언트 클래스의 헤더 파일입니다 내 서버 헤더 파일이 내 서버 CPP 파일이
#ifndef SERVER_H
#define SERVER_H
#include <QString>
#include <mqueue.h>
#include <QVector>
#include <QStringList>
#include <iostream>
#include "../src/messages.h"
class Server : public QObject
{
Q_OBJECT
public:
Server();
void start();
private:
void join(QString name);
char buf[MSG_SIZE], msgSend[MSG_SIZE];
QVector<mqd_t> mq_external;
QVector<QString> users;
mqd_t mq_central;
struct mq_attr attr;
signals:
void joined(QString name);
};
#endif // SERVER_H
이다는 :
#ifndef MESSAGES_H
#define MESSAGES_H
#define MSG_SIZE 150
#define CENTRALBOX "/CentralMailBox"
#include <stdio.h>
#include <stdlib.h>
#endif // MESSAGES_H
의 일부 :
#include "Server.h"
using namespace std;
Server::Server()
{
}
void Server::start(){
attr.mq_maxmsg = 100;
attr.mq_msgsize = MSG_SIZE;
attr.mq_flags = 0;
mq_unlink(CENTRALBOX);
mq_central = mq_open(CENTRALBOX, O_RDONLY | O_CREAT, S_IRWXU, &attr);
while(1)
{
//Wait to recieve message from user
int tempMsgVal = mq_receive(mq_central, buf, MSG_SIZE, 0);
if(tempMsgVal != -1)
{
cout << tempMsgVal;
}
if(tempMsgVal != -1){
QString tempS = buf;
QStringList tempSL = tempS.split(":");
if(tempSL.size() == 2 && tempSL.at(0) == "started")
{
int x = 0;
bool exists = false;
for(int i = 0; i < mq_external.size(); i++)
{
x = QString::compare(tempSL[1], users.at(i), Qt::CaseInsensitive);
if(x == 0)
{
exists = true;
break;
}
}
if(!exists)
{
sprintf(buf,"joined");
QString tempS1 = tempSL[1] + "new";
QByteArray byteArray = tempS1.toUtf8();
const char* tempr = byteArray.constData();
mqd_t tempMQ = mq_open(tempr, O_RDWR);
int tempI = mq_send(tempMQ, buf, strlen(buf), 0);
join(tempSL[1]);
}
else
{
sprintf(buf,"invalidname");
QString tempS1 = tempSL[1] + "new";
QByteArray byteArray = tempS1.toUtf8();
const char* tempr = byteArray.constData();
mqd_t tempMQ = mq_open(tempr, O_RDWR);
int tempI = mq_send(tempMQ, buf, strlen(buf), 0);
}//Endelse
}//Endif
}//Endif
}//Endwhile
}
void Server::join(QString name)
{
emit joined(name);
}
이 내가 두 클래스에 포함 messages.h 파일입니다 클라이언트 및 서버 클래스의 코드는 gui와 관련이 있지만, 해당 부분을 테스트하고 관련 메서드가 올바른 시간에 호출됩니다.
제 문제는 클라이언트 함수에서 mq_send 메서드를 호출하면 반환되고 오류가 발생하고 서버 클래스에서도 동일합니다. 송신 및 수신과 관련하여 제가 잘못하고있는 것이 있습니까? 왜냐하면 나는 내 삶에 대해 알아 내지 못하기 때문이다.
무엇이 오류입니까? mq 호출의 리턴 코드를 테스트하고 perror 또는 기타 등으로 errno의 결과를 출력해야한다. – Duck
두 메서드는 호출 될 때 -1을 반환하며 문서에 따르면 메시지를 성공적으로 보내거나받지 못했음을 의미합니다. – Amre
예. 그러나 오류 코드는 errno에 있습니다. 'perror()'를 시도하십시오. – Duck