从Streams中提取信息并在LinkedList中保存

时间:2022-04-21 20:55:51

I have created a server that uses threads to allow multiple nodes to connect and when they connect the server receives the nodes information and stores it to a linked list. The problem I'm having is that when the node connects to the server, the information is retrieved but will not store in the list. Instead of stating the information requested, Current nodes connected: loadbalancer.NodeList@77c57d28 is shown. Any help would be fantastic. Thanks.

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

Below is the linkedlist, saved as NodeList:

下面是链表,保存为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;
}

}

Below is the class that I used to create the node:

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

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;
}
}

Here is the thread:

这是线程:

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) {

        }
}}
}

UPDATE:

The linkedlist also overwrites the first node added. Any indication of why would be great.

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

Apologies for the state of the code, i'm pretty new to programming and java.

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

Cheers.

2 个解决方案

#1


Do not forget to add getters and setters to your NodeL class in order to be able to get and set data from and to a node respectively.

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

For example:

To retreive information from the first node

从第一个节点检索信息

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

To set new values to the first node

为第一个节点设置新值

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

No need to make NodeList class. Instead you could define a LinkedList of NodeL in your NodeManager class

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

Do it like this

像这样做

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

And When you receive new connection just add new NodeL to nodeList

当您收到新连接时,只需将新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

Or you can change NodeL constructor and do it like so

或者您可以更改NodeL构造函数并执行此操作

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

And then

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

#2


For your question:

对于你的问题:

NodeList@77c57d28 is shown:

NodeList @ 77c57d28显示:

It is because NodeList doesn't have custom overidden toString() method, so it is calling Object.toString() (Object class is default super class for java classes), it prints that "NodeList@77c57d28".

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

Few problems in your class NodeManager:

您的类NodeManager中几个问题:

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

You always end up having last assigned nodeIp and nodePort in your nodeList.

您总是在nodeList中最后分配了nodeIp和nodePort。

I can understand your a java beginner so i added modified classes below. look at once:

我可以理解你的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;
   }
 }

NodeList:

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's run method:

NodeManager的运行方法:

 @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) {

        }
  }
}

#1


Do not forget to add getters and setters to your NodeL class in order to be able to get and set data from and to a node respectively.

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

For example:

To retreive information from the first node

从第一个节点检索信息

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

To set new values to the first node

为第一个节点设置新值

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

No need to make NodeList class. Instead you could define a LinkedList of NodeL in your NodeManager class

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

Do it like this

像这样做

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

And When you receive new connection just add new NodeL to nodeList

当您收到新连接时,只需将新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

Or you can change NodeL constructor and do it like so

或者您可以更改NodeL构造函数并执行此操作

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

And then

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

#2


For your question:

对于你的问题:

NodeList@77c57d28 is shown:

NodeList @ 77c57d28显示:

It is because NodeList doesn't have custom overidden toString() method, so it is calling Object.toString() (Object class is default super class for java classes), it prints that "NodeList@77c57d28".

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

Few problems in your class NodeManager:

您的类NodeManager中几个问题:

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

You always end up having last assigned nodeIp and nodePort in your nodeList.

您总是在nodeList中最后分配了nodeIp和nodePort。

I can understand your a java beginner so i added modified classes below. look at once:

我可以理解你的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;
   }
 }

NodeList:

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's run method:

NodeManager的运行方法:

 @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) {

        }
  }
}