示例#1
0
文件: client.c 项目: rsenn/dirlist
int
main(int argc, char* argv[]) {
  int s;
  uint16 port = 2342;
  uint32 scope_id = 0;
  char ip[16];
  char buf[8192];
  int i;
  struct pollfd p[2];

  if(argc < 3) {
  usage:
    __write2("usage: client ip port\n\nexample: dump -0 -f - | client 10.0.0.149 8000\n");
    return 0;
  }
  if(argv[1][i = scan_ip6if(argv[1], ip, &scope_id)]) {
    __write2("client: error: invalid ip address!\n");
    goto usage;
  }
  if(argv[2][scan_ushort(argv[2], &port)]) {
    __write2("client: error: invalid port number!\n");
    goto usage;
  }
  if(byte_equal(ip, 12, V4mappedprefix)) {
    s = socket_tcp4b();
    if(s == -1)
      panic("client: error: socket() failed");
    if(socket_connect4(s, ip + 12, port) == -1)
      panic("client: error: connect() failed");
  } else {
    s = socket_tcp6b();
    if(s == -1)
      panic("client: error: socket() failed");
    if(socket_connect6(s, ip, port, scope_id) == -1)
      panic("client: error: connect() failed");
  }
  ndelay_off(s);
  p[0].fd = 0;
  p[0].events = POLLIN;
  p[1].fd = s;
  p[1].events = POLLIN;
  while(poll(p, 2, 5000)) {
    if(p[0].revents) {
      int j;
      if(p[0].revents & POLLERR)
        panic("client: error: poll() signals POLLERR\n");
      i = read(0, buf, sizeof(buf));
      if(i == 0) {
        shutdown(s, SHUT_WR);
        blockingcopy(s, 1);
      }
      for(j = 0; j < i;) {
        int k = write(s, buf + j, i - j);
        if(k == -1)
          panic("client: error: write() failed");
        if(k == 0)
          panic("client: error: short write!\n");
        j += k;
      }
    }
    if(p[1].revents) {
      int j;
      if(p[1].revents & POLLERR)
        panic("client: error: poll() signals POLLERR\n");
      i = read(s, buf, sizeof(buf));
      if(i == 0) {
        shutdown(1, SHUT_WR);
        blockingcopy(0, s);
      }
      for(j = 0; j < i;) {
        int k = write(1, buf + j, i - j);
        if(k == -1)
          panic("client: error: write() failed");
        if(k == 0)
          panic("client: error: short write!\n");
        j += k;
      }
    }
  }
  return 0;
}
示例#2
0
int main(int argc,char* argv[]) {
  int s,t;
  uint16 port=2342;
  uint32 scope_id=0;
  char ip[16];
  char buf[8192];
  int i;
  struct pollfd p[2];

  if (argc<3) {
usage:
    __write2("usage: server ip port\n\nexample: server 0 8000 | restore x -f -\n");
    return 0;
  }
  if (!strcmp(argv[1],"0"))
    byte_zero(ip,16);
  else
    if (argv[1][i=scan_ip6if(argv[1],ip,&scope_id)]) {
      __write2("server: error: invalid ip address!\n");
      goto usage;
    }
  if (argv[2][scan_ushort(argv[2],&port)]) {
    __write2("server: error: invalid port number!\n");
    goto usage;
  }
  if (byte_equal(ip,12,V4mappedprefix)) {
    s=socket_tcp4b();
    if (s==-1) panic("server: error: socket() failed");
    if (socket_bind4_reuse(s,ip+12,port)==-1) panic("server: error: bind() failed");
    if (socket_listen(s,1)==-1) panic("server: error: listen() failed");
    ndelay_off(s);
    if ((t=socket_accept4(s,0,0))==-1) panic("server: error: accept() failed");
  } else {
    s=socket_tcp6b();
    if (s==-1) panic("server: error: socket() failed");
    if (socket_bind6_reuse(s,ip,port,scope_id)==-1) panic("server: error: bind() failed");
    if (socket_listen(s,1)==-1) panic("server: error: listen() failed");
    ndelay_off(s);
    if ((t=socket_accept6(s,0,0,0))==-1) panic("server: error: accept() failed");
  }
  close(s);
  p[0].fd=0; p[0].events=POLLIN;
  p[1].fd=t; p[1].events=POLLIN;
  while (poll(p,2,5000)) {
    if (p[0].revents) {
      int j;
      if (p[0].revents&POLLERR)
	panic("server: error: poll() signals POLLERR\n");
      i=read(0,buf,sizeof(buf));
      if (i==0) { shutdown(t,SHUT_WR); blockingcopy(t,1); }
      for (j=0; j<i; ) {
	int k=write(t,buf+j,i-j);
	if (k==-1) panic("server: error: write() failed");
	if (k==0) panic("server: error: short write!\n");
	j+=k;
      }
    }
    if (p[1].revents) {
      int j;
      if (p[1].revents&POLLERR)
	panic("server: error: poll() signals POLLERR\n");
      i=read(t,buf,sizeof(buf));
      if (i==0) { shutdown(1,SHUT_WR); blockingcopy(0,t); }
      for (j=0; j<i; ) {
	int k=write(1,buf+j,i-j);
	if (k==-1) panic("server: error: write() failed");
	if (k==0) panic("server: error: short write!\n");
	j+=k;
      }
    }
  }
  return 0;
}