连接到 X 服务器

发布于 2024-10-10 05:38:54 字数 1161 浏览 10 评论 0原文

我正在尝试编写自己的程序,使用 X Window 系统协议文档连接到不使用 Xlib 或 XCB 的本地 X 服务器。将连接数据写入服务器套接字后,我似乎无法读回服务器的回复,因为读取功能被阻止。

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

int main()
{
  int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

  struct sockaddr_un serv_addr;
  memset((void*)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sun_family = AF_UNIX;
  strcpy(serv_addr.sun_path, "/tmp/.X11-unix/X0");
  int servlen = 17 + sizeof(serv_addr.sun_family);

  int err = connect(sockfd, (struct sockaddr*)&serv_addr, servlen);

  printf("%d\n", err);

  struct
  {
    uint8_t en;
    uint8_t pad;
    uint16_t major;
    uint16_t minor;
    uint16_t name_len;
    uint16_t data_len;
    uint16_t pad2;
    uint8_t name[4];
    uint8_t data[4];
  } init;

  init.en = 'l';
  init.major = 11;
  init.minor = 0;
  init.name_len = 0;
  init.data_len = 0;

  err = write(sockfd, init, sizeof(init));

  printf("%d\n", err);

  uint8_t buf[5000];

  int n = read(sockfd, buf, 5000);

  return 0;
}

我的代码中缺少什么? connect 和 write 函数返回 0。

I'm trying to write my own program that connects to a local X server that does not use Xlib or XCB, using the X Window System Protocol documentation. After writing the connection data to the server socket I can't seem to read back the server's reply, as the read function blocks.

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

int main()
{
  int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

  struct sockaddr_un serv_addr;
  memset((void*)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sun_family = AF_UNIX;
  strcpy(serv_addr.sun_path, "/tmp/.X11-unix/X0");
  int servlen = 17 + sizeof(serv_addr.sun_family);

  int err = connect(sockfd, (struct sockaddr*)&serv_addr, servlen);

  printf("%d\n", err);

  struct
  {
    uint8_t en;
    uint8_t pad;
    uint16_t major;
    uint16_t minor;
    uint16_t name_len;
    uint16_t data_len;
    uint16_t pad2;
    uint8_t name[4];
    uint8_t data[4];
  } init;

  init.en = 'l';
  init.major = 11;
  init.minor = 0;
  init.name_len = 0;
  init.data_len = 0;

  err = write(sockfd, init, sizeof(init));

  printf("%d\n", err);

  uint8_t buf[5000];

  int n = read(sockfd, buf, 5000);

  return 0;
}

What is missing from my code? The connect and write functions return 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-10-17 05:38:54

我怀疑您的问题出在您的 write 上:

err = write(sockfd, init, sizeof(init));

第二个参数应该是 &init,仅传递 init 将有效地展开结构计算机将看到类似这样的内容:

err = write(sockfd, init.en, init.pad, ..., sizeof(init));

由于 write 仅查看三个参数,因此它将使用 init 成员之一作为要写入的缓冲区的大小,并且成员可能为零。实际的字节级参数堆栈取决于编译器和体系结构,因此您可以嘲笑我们自己,重要的是您没有提供您想要的 write 内容。

你的编译器应该已经发现了这一点,如果没有,那么你需要将警告选项调高到最高。

无论如何,您可能希望逐字节地构建发送到 X 服务器的数据,这样您就可以确保获得正确的字节顺序和偏移量。发送 &init 会让您陷入常见问题(打包、对齐和字节顺序)的麻烦。

I suspect that your problem is with your write:

err = write(sockfd, init, sizeof(init));

The second parameter should be &init, passing just init will, effectively, unroll the structure and the computer will see something like this:

err = write(sockfd, init.en, init.pad, ..., sizeof(init));

Since write only looks at three arguments, it will be using one of the init members as the size of the buffer to write and that member is probably zero. The actual byte-level argument stack depends on the compiler and architecture so you can tease that our yourself, the important thing is that you're not feeding write what you want to.

Your compiler should have caught this, if it didn't then you need to crank up the warning options as high as they will go.

You'll probably want to build the data you send to the X server byte by byte anyway, that way you can make sure you get the byte order and offsets right. Sending &init will get you into trouble with the usual suspects (packing, alignment, and byte order).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文