title: Connecting to X Server
body: <p>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.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdint.h&gt;
  
  int main()
  {
    int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  
    struct sockaddr_un serv_addr;
    memset((void*)&amp;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*)&amp;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;
  }
  </code></pre>
  
  <p>What is missing from my code? The connect and write functions return 0.</p>