Вобщем пытаюсь передать структуру вида:
struct tg
{
__u8 sender;
__u32 seqno;
__u8 type;
__u32 query_data;
__u32 ip[4];
__u32 sin_port;
__u8 protocol;
__u8 family;
};
server = new QLocalServer(this);
if (!server->listen("/tmp/tg")) {}
connect(server, SIGNAL(newConnection()), this, SLOT(read_socket()));
void Window::read_socket()
{
QLocalSocket *clientConnection = server->nextPendingConnection();
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
clientConnection->waitForReadyRead(10000); // без него одни нули
in >> query-> sender;
in >> query->seqno;
in >> query->query_type;
in >> query->query_data;
in >> query->ip1;
in >> query->ip2;
in >> query->ip3;
in >> query->ip4;
in >> query->sin_port;
in >> query->protocol;
in >> query->family;
...
}
struct tg_query
{
quint8 sender;
quint32 seqno;
quint8 query_type;
quint32 query_data;
quint32 ip[4];
quint32 sin_port;
quint8 protocol;
quint8 family;
};
Привел бы код записи в сокет.
Демон отправляет так:
int
forward_query_to_frontend (struct tg_query query)
{
int retval;
int sockfd;
struct sockaddr_un address;
struct tg_query answer;
sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sun_family = AF_UNIX;
strcpy(address.sun_path, PATH_FRONTEND);
/* Now connect our socket to the server's socket. */
retval = connect (sockfd, (struct sockaddr *)&address, sizeof(address));
if(retval == -1)
{
close(sockfd);
return -1;
}
// forwards the module query to the frontend
query.sender = TG_DAEMON;
retval = send(sockfd, &query, sizeof(struct tg_query), 0);
retval = recv(sockfd, &answer, sizeof(answer), 0);
close(sockfd);
return answer.query_data; // answer frontend
}
struct tg_query received;
int retval=-1;
retval = recv(socket, &received, sizeof(struct tg_query), MSG_WAITALL);
A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system
То есть нельзя записать "raw unencoded binary data" в демоне и считать их через "stream of encoded information"
Решил проблему просто:
QLocalSocket *clientConnection = server->nextPendingConnection();
clientConnection->waitForReadyRead(100);
char *data;
data = new char[40];
clientConnection->read(data, 40);
tg_query *query = (tg_query *)data;
pulo,
лучше конечно проверять сколько ты вычитал.
и читать пока есть доступные байты. bool bytesAvaliable() что ли.
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)