连接到 X 服务器
我正在尝试编写自己的程序,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您的问题出在您的
write
上:第二个参数应该是
&init
,仅传递init
将有效地展开结构计算机将看到类似这样的内容:由于
write
仅查看三个参数,因此它将使用init
成员之一作为要写入的缓冲区的大小,并且成员可能为零。实际的字节级参数堆栈取决于编译器和体系结构,因此您可以嘲笑我们自己,重要的是您没有提供您想要的write
内容。你的编译器应该已经发现了这一点,如果没有,那么你需要将警告选项调高到最高。
无论如何,您可能希望逐字节地构建发送到 X 服务器的数据,这样您就可以确保获得正确的字节顺序和偏移量。发送
&init
会让您陷入常见问题(打包、对齐和字节顺序)的麻烦。I suspect that your problem is with your
write
:The second parameter should be
&init
, passing justinit
will, effectively, unroll the structure and the computer will see something like this:Since
write
only looks at three arguments, it will be using one of theinit
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 feedingwrite
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).