【问题标题】:Extracting Information from Streams and Saving in LinkedList从流中提取信息并保存在 LinkedList 中
【发布时间】:2015-05-02 18:55:11
【问题描述】:

我创建了一个服务器,它使用线程来允许多个节点连接,当它们连接时,服务器接收节点信息并将其存储到链表中。我遇到的问题是,当节点连接到服务器时,会检索信息但不会存储在列表中。不是说明请求的信息,而是显示当前连接的节点:loadbalancer.NodeList@77c57d28。任何帮助都会很棒。谢谢。

下面是链表,保存为NodeList:

public class NodeList {

private String name;
private InetAddress address;
private int port;
private String nodeInformation;

LinkedList nodeList = new LinkedList();

public void Name(String name){
    this.name = name;
}

public void Address(InetAddress address){
    this.address = address;
}

public void Port(int port){
    this.port = port;
}

public void addNode (NodeL node){
    node = new NodeL(address,port);
    nodeList.add(name + address + port);

}

public String getNode(){
    Object nodeInf = nodeList.getFirst();
    nodeInformation = nodeInf.toString();
    return nodeInformation;
}

}

下面是我用来创建节点的类:

public class NodeL {

private String name;
private InetAddress address;
private int port;
private String nodeInfo;

public NodeL(InetAddress a, int p){
    address = a; port = p;
}

@Override
public String toString(){
    return address + " " + port;
}
}

这里是线程:

public class NodeManager extends Thread{
private Socket serverSocket = null;

private BufferedReader in;
private PrintWriter out;
public InetAddress nodeIP;
public int nodePort;
public String string;


public NodeManager(Socket serverSocket) throws IOException {
this.serverSocket = serverSocket;

}
//LinkedList nodeList = new LinkedList()
@Override
public void run() {

    NodeList nodelist = new NodeList();
    while(true){
        try {
            //byte[] buffer = new byte [1024];
            in = new BufferedReader(new          InputStreamReader(serverSocket.getInputStream()));  
            out = new PrintWriter(this.serverSocket.getOutputStream(), true);
            String received = in.toString();

            nodeIP = serverSocket.getInetAddress();
            nodePort = serverSocket.getPort();
            System.out.println("Node IP: " + nodeIP);
            System.out.println("Node Port number: " + nodePort);

            nodelist.Address(nodeIP);
            nodelist.Port(nodePort);

            nodelist.addNode(null);
            System.out.println("Current nodes connected: " + nodelist);
    break;       
            } catch (Exception error) {

        }
}}
}

更新:

链表还会覆盖添加的第一个节点。任何说明为什么都会很棒。

为代码的状态道歉,我对编程和 java 还很陌生。

干杯。

【问题讨论】:

    标签: java netbeans linked-list inputstream


    【解决方案1】:

    不要忘记将getterssetters 添加到NodeL 类中,以便能够分别从节点获取和设置数据。

    例如:

    从第一个节点获取信息

    nodeList.get(0).getName(); // to get node name
    nodeList.get(0).getPort(); // to get port
    ...
    

    为第一个节点设置新值

    nodeList.get(0).setName(); // to get node name
    nodeList.get(0).setPort(); // to get port
    

    无需创建NodeList 类。相反,您可以在 NodeManager 类中定义 LinkedListNodeL

    这样做

    LinkedList<NodeL> nodeList = new LinkedList<NodeL>();
    

    当您收到新连接时,只需将新的NodeL 添加到nodeList

    nodeList.add(new NodeL(serverSocket.getInetAddress(),serverSocket.getPort()));
    nodeList.getLast().setName("whatever"); // to set node name
    nodeList.getLast().setNodeInfo("whatever"); // to set node info
    

    或者您可以更改NodeL 构造函数并这样做

    public NodeL(String name, InetAddress address, int port , String nodeInfo){
        this.name=name;
        this.address = address ;
        this.port = port;
        this.nodeInfo=nodeInfo;
    }
    

    然后

    nodeList.add(new NodeL("whatever",serverSocket.getInetAddress(),serverSocket.getPort(),"whatever"));
    

    【讨论】:

      【解决方案2】:

      对于您的问题:

      NodeList@77c57d28显示:

      这是因为NodeList 没有自定义覆盖toString() 方法,所以它调用Object.toString()Object 类是java 类的默认超类),它打印出“NodeList@77c57d28”。

      你班上的小问题NodeManager

      nodelist.Address(nodeIP);
      nodelist.Port(nodePort);
      nodelist.addNode(null);
      

      你最后总是在你的nodeList 中分配nodeIpnodePort

      我可以理解你是一个 java 初学者,所以我在下面添加了修改后的类。看一次:

      public class NodeL {
      
         private String name;
         private InetAddress address;
         private int port;
         private String nodeInfo;
      
         public NodeL(InetAddress a, int p){
            address = a; port = p;
         }
      
         public InetAddress getAddress() {
             return address;
         }
      
         public int getPort() {
             return port;
         }
      
         @Override
         public String toString(){
           return address + " " + port;
         }
       }
      

      节点列表:

      public class NodeList {
      
         private String name;
         private String nodeInformation;
      
         LinkedList nodeList = new LinkedList();
      
         public void Name(String name){
             this.name = name;
         }
      
         public void addNode (NodeL node){
            nodeList.add(name + node.getAddress() + node.getPort());
        }
      
        @Override      
        public String toString() {
           StringBuilder sb = new StringBuilder();
           for(Object node : nodeList) {
              sb.append(node + "\n");
           }
           return sb.toString();
         }
      }
      

      NodeManager 的run 方法:

       @Override
       public void run() {
      
       NodeList nodelist = new NodeList();
       while(true){
          try {
              //byte[] buffer = new byte [1024];
              in = new BufferedReader(new            InputStreamReader(serverSocket.getInputStream()));  
              out = new PrintWriter(this.serverSocket.getOutputStream(),  true);
              String received = in.toString();
      
              nodeIP = serverSocket.getInetAddress();
              nodePort = serverSocket.getPort();
              System.out.println("Node IP: " + nodeIP);
              System.out.println("Node Port number: " + nodePort);
      
              nodelist.addNode(new NodeL(address, port));
              System.out.println("Current nodes connected: " + nodelist);
              break;       
              } catch (Exception error) {
      
              }
        }
      }
      

      【讨论】:

      • @JavaNovice 很高兴为您提供帮助。对不起!我忘记添加 return type 的方法,我直接在编辑框中编辑,所以我没弄明白。无论如何更新了我的答案。谢谢:)
      • 非常感谢!浏览了一下,出现了几个错误: public getAddress() { return address; } 公共 getPort() { 返回端口;这些返回;无效的方法声明。另一个是: public void addNode (NodeL node){ nodeList.add(name + node.getAddress() + node.getPort());这会返回;不允许使用 void 类型。我玩过,不知道这里需要什么。最后的错误: for( node : nodeList) { sb.append(node + "\n");这说“对象不能转换为字符串”谢谢@Dilip
      • @JavaNovice 我以为你创建了nodeList 类型为String。现在我看看你刚刚创建的原始代码LinkedList。所以你可以用for(Object node : nodeList)更新for循环它应该可以工作:)
      • @JavaNovice 我已经更新了NodeList 类。请检查。我已经告诉过你,我错过了这些方法的返回类型,现在我在回答中更正了。
      • 对不起!我错过了!在 [NodeManager] 中: nodelist.addNode(new NodeL (address, port));它无法找到地址和端口。只是说找不到符号。有任何想法吗? @Dilip
      猜你喜欢
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多