- 论坛徽章:
- 0
|
大家好,我为了深入学习下x-window的实现,使用xorg的早期版本X.V10R3进行xclients的调试工作。
发现XOpenDisplay无法connect到xserver.涉及的核心代码如下:
使用的DISPLAY是系统默认的:0.0
struct sockaddr_in inaddr; /* INET socket address. */
struct sockaddr *addr; /* address to connect to */
char displaybuf[256];
int addrlen; /* length of address */
int dispnum; /* display number. */
int indian; /* to determine which indian. */
int fd;
struct hostent *host_ptr;
register XReq *req; /* XReq request packet pointer. */
XRep rep; /* XRep reply packet. */
gethostname(displaybuf, sizeof (displaybuf)); /* get local host name “localehost.localdomain” */
/* Get the statistics on the specified host. */
if ((host_ptr = gethostbyname(displaybuf)) == NULL)
{
/* No such host! */
errno = EINVAL;
return(NULL);
}
/* Check the address type to see if it is an internet host. */
if (host_ptr->h_addrtype != AF_INET) {
/* Not an Internet host! */
errno = EPROTOTYPE;
return(NULL);
}
/* Set up the socket data. */
inaddr.sin_family = AF_INET;
inaddr.sin_port = 0;
indian = 1;
if (*(char *) &indian)
{
inaddr.sin_port += 5800;
}
else
{
inaddr.sin_port += 5900;
}
inaddr.sin_port = htons(inaddr.sin_port);
inaddr.sin_addr = *((struct in_addr *)host_ptr->h_addr);
addr = (struct sockaddr *) &inaddr;
addrlen = sizeof (struct sockaddr_in);
}
if((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0)
{
/* Socket call failed! */
/* errno set by system call. */
fprintf(stderr, "socket create error! \n");
return(NULL);
}
/* Open the connection to the specified X server. */
if(connect(fd, addr, addrlen) == -1)
{
/* Connection call failed! */
/* errno set by system call. */
fprintf(stderr, "socket connect error.\n");
close (dpy->fd);
free (dpy);
return(NULL);
}
我现在的问题是connect调用失败,我分析原因是
1.xserver不支持我这样的socket连接,是不是需要一个认证什么的?
2.我连接的端口地址不对?
希望高手们可以帮忙解答下,非常感谢。 |
|