请教一个问题:
我做了个探测局域网用户的程序,对方能收到我的udp广播数据报,但用tcp连接总是会超时是什么原因呢·?class detector extends Thread    //主动探测主机信息发送端
  {
    String userIp;
    public detector(String userIp)
    {
      this.userIp=userIp;
    }
    public void run()
    {
      try
      {
        System.out.println("start to connect "+userIp+" ......"); //可以运行到这里
        Socket s = new Socket(InetAddress.getByName(userIp),5001);
        InputStream is = s.getInputStream();
        OutputStream os = s.getOutputStream();
        os.write(InetAddress.getLocalHost().getHostName().getBytes());//写入用户名
        os.write(System.getProperty("user.name").getBytes());  //写入登录名
        os.write(InetAddress.getLocalHost().toString().getBytes()); //写入ip地址        String userName,loginName,userIp;
        byte[] buf=new byte[100];
        int len=is.read(buf);
        if(len!=-1)
        {
          userName=new String(buf,0,len);
          len=is.read(buf);
          if(len!=-1)
          {
            loginName=new String(buf,0,len);
            len=is.read(buf);
            if(len!=-1)
            {
              userIp=new String(buf,0,len);
              al.add(new user(userName,loginName,userIp));
              tableInit();
            }
          }
        }
        is.close();
        os.close();
        s.close();
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
      }
    }
  }

解决方案 »

  1.   

    TCP有很多因素都会导致无法连接,如防火墙等.如果系统里有防火墙,建议先关闭了之后再连.
      

  2.   

    加一个问题,解决了给分:我定义了一个动态表格:
     class myTable extends JTable  //用于显示用户信息的表格类
      {
        public myTable(Object[][] data,Object[] head)
        {
          super(data,head);
         }    public boolean isCellEditable(int row,int column)
        {
          return false;
        }
      }用ArrayList更新数据,每次数据更新后调用以下函数,表格数据是更新了,但视图不能更新
      private void tableInit()   //表格初始化
      {
        String[] cols={"user","loginname","ip"};
        Object[] inf=al.toArray();
        Object[][] userInf=new Object[inf.length][3];
        System.out.println("\ncurrent userlist:");
        for(int i=0;i<inf.length;i++)
        {
          user temp=(user)inf[i];
          userInf[i][0]=temp.name;
          userInf[i][1]=temp.loginName;
          userInf[i][2]=temp.ip;
          System.out.println(inf[i]);
        }
        table= new myTable(userInf,cols);
        userNum.setText("users online "+inf.length);
      }大家有没有好的方法?
      

  3.   

    AbstractTableModel 看看这个类