这些天,我正在学习APUE.2e(UNIX®环境中的高级编程,第二版),并且已经阅读了第16章网络IPC:套接字。当我运行程序ruptime(用于客户端)和ruptimed(用于服务器端)时,发生了一些奇怪的事情。客户端程序显示与服务器通信以从服务器获取输出的客户端命令。
系统的uptime命令,而服务器程序ruptimed(注意为d)则接收该命令,执行程序是uptime命令,并将uptime的输出返回给客户端。这两个程序都编译成功,然后将ruptime 4000/tcp添加到文件/etc/service中,这意味着服务ruptime绑定(bind)到端口4000/tcp。我在同一台机器(Ubuntu 11.04)上运行了两个程序,首先运行了ruptimed(当然):

$ ./ruptimed
$ ./ruptime 127.0.0.1
 21:35:48 up 13:06,  3 users,  load average: 0.56, 0.85, 0.94
输出是我所期望的。但是,当我运行时:
$ ./ruptimed
$ ./ruptime 192.168.1.221
  # the terminal blocks here
IP地址192.168.1.221是计算机在Intranet中的实际IP。怎么了我搜索了Internet,发现/etc/init.d/中似乎需要一个shell脚本。然后,我在ruptime目录中添加一个名为/etc/init.d/的文件。这里是:
#! /bin/sh

### BEGIN INIT INFO
# Provides:     ruptimed
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:
# Short-Description:    ruptime
### END INIT INFO
start()
{
    echo "start ruptime"
    /home/tlh1987/apue-practice/tlh-practice/ruptimed
    exit 0;
}
stop()
{
    killall ruptimed
    echo "stop ruptime"
}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    ;;
    *)
    echo "usage: $0 start|stop|restart"
    exit 0;
esac
但是,它没有用,我也不知道为什么。我还设置了ufw启用并允许ruptime
像这样:
sudo ufw allow ruptime
它也不起作用。 ruptimeruptimed的源代码如下:
connect_retry()
// the  functions used in the two program
// the  connect_retry() function
#include "apue.h"
#include <sys/socket.h>

#define MAXSLEEP 128

int
connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
{
  int nsec;

  for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
    if (connect(sockfd, addr, alen) == 0) {
      return (0);
    }
    if (nsec <= MAXSLEEP/2)
      sleep(nsec);
  }
  return (-1);
}
init_server()
// the initserver function
#include "apue.h"
#include <errno.h>
#include <sys/socket.h>

int
initserver(int type, const struct sockaddr *addr, socklen_t alen,
       int qlen)
{
  int fd;
  int err = 0;
  if ((fd = socket(addr->sa_family, type, 0)) < 0)
    return (-1);
  if (bind(fd, addr, alen) < 0) {
    err = errno;
    goto errout;
  }
  if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
    if (listen(fd, qlen) < 0) {
      err = errno;
      goto errout;
    }
  }
  return(fd);

 errout:
  close(fd);
  errno = err;
  return (-1);
}
客户端
// the client end i.e. ruptime.c
#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>

#define MAXADDRLEN 256

#define BUFLEN 128
extern int connect_retry(int, const struct sockaddr *, socklen_t);

void
print_uptime(int sockfd)
{
  int n;
  char buf[BUFLEN];

  while ((n = recv(sockfd, buf, BUFLEN, 0)) > 0)
    write(STDOUT_FILENO, buf, n);
  if (n < 0)
    err_sys("recv error");
}

int
main(int argc, char *argv[])
{
  struct addrinfo   *ailist, *aip;
  struct addrinfo    hint;
  int            sockfd, err;

  if (argc !=2 )
    err_quit("usage: ruptime hostname");
  hint.ai_flags = 0;
  hint.ai_family = 0;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_protocol = 0;
  hint.ai_addrlen = 0;
  hint.ai_canonname = NULL;
  hint.ai_addr = NULL;
  hint.ai_next = NULL;
  if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0)
    err_quit("getaddrinfo error: %s", gai_strerror(err));
  for (aip = ailist; aip != NULL; aip = aip->ai_next) {
    if ((sockfd = socket(aip->ai_family, SOCK_STREAM, 0)) < 0)
      err = errno;
    if (connect_retry(sockfd, aip->ai_addr, aip->ai_addrlen) < 0) {
      err = errno;
    } else {
      print_uptime(sockfd);
      exit(0);
    }
  }
  fprintf(stderr, "can't connect to %s: %s\n", argv[1],
      strerror(err));
  exit(1);
}
服务器端
//the server end i.e. ruptimed is as follows:
#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <sys/socket.h>

#define BUFLEN 128
#define QLEN 10

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif

extern int initserver(int, struct sockaddr *, socklen_t, int);

void
serve(int sockfd)
{
  int   clfd;
  FILE *fp;
  char buf[BUFLEN];

  for ( ; ; ) {
    clfd = accept(sockfd, NULL, NULL);
    if (clfd < 0) {
      syslog(LOG_ERR, "ruptimed: accept error: %s",
         strerror(errno));
      exit(1);
    }
    if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
      sprintf(buf, "error: %s\n", strerror(errno));
      send(clfd, buf, strlen(buf), 0);
    } else {
      while (fgets(buf, BUFLEN, fp) != NULL)
    send(clfd, buf, strlen(buf), 0);
      pclose(fp);
    }
    close(clfd);
  }
}

int
main(int argc, char *argv[])
{
  struct addrinfo   *ailist, *aip;
  struct addrinfo    hint;
  int            sockfd, err, n;
  char          *host;

  if (argc != 1)
    err_quit("usage: ruptimed");
#ifdef _SC_HOST_NAME_MAX
  n = sysconf(_SC_HOST_NAME_MAX);
  if (n < 0)
#endif
    n = HOST_NAME_MAX;
  host = malloc(n);
  if (host == NULL)
    err_sys("malloc error");
  if (gethostname(host, n) < 0)
    err_sys("gethostname error");
  daemonize("ruptimed");
  hint.ai_flags = AI_CANONNAME;
  hint.ai_family = 0;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_protocol = 0;
  hint.ai_addrlen = 0;
  hint.ai_canonname = NULL;
  hint.ai_addr = NULL;
  hint.ai_next = NULL;
  if ((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0) {
    syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s",
       gai_strerror(err));
    exit(1);
  }
  for (aip = ailist; aip != NULL; aip = aip->ai_next) {
    if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr,
                 aip->ai_addrlen, QLEN)) >= 0)
      serve(sockfd);
    exit(0);
  }
  exit(1);
}
有没有人可以帮忙?
netstat -nap命令的标题行:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:4000          0.0.0.0:*               LISTEN      2252/ruptimed
tcp        0  13033 192.168.1.221:47380     91.189.89.114:443       LAST_ACK    -
tcp       74      0 192.168.1.221:43914     91.189.89.76:443        ESTABLISHED 1846/python
tcp        0      0 127.0.0.1:4000          127.0.0.1:56344         TIME_WAIT   -
tcp        0      1 192.168.1.221:35442     192.168.1.89:24800      SYN_SENT    1478/synergyc
tcp        0      0 192.168.1.221:34957     74.125.71.125:5222      ESTABLISHED 1618/chrome
tcp        0      0 192.168.1.221:57795     203.208.46.163:443      ESTABLISHED 1618/chrome
tcp6       0      0 :::22                   :::*                    LISTEN      -
第三项记录是我的服务时间。有什么事吗我对它的意思不是很了解。

最佳答案

我不是C专家,但我知道一件事:您的守护程序可能仅绑定(bind)到一个地址/接口(interface),并且仅接受来自它的连接。

使用netstat -nap检查守护程序绑定(bind)到的地址。

如果它正在侦听0.0.0.0,则表示它正在接受来自每个接口(interface)(包括本地主机)的连接。

如果正在监听127.0.0.1,它将仅接受本地主机。

我敢打赌,它仅绑定(bind)到127.0.0.1,这就是为什么您不能与任何其他地址连接的原因。

08-04 15:53