Extracting Information from Streams and Saving in LinkedList

ぃ、小莉子 提交于 2019-12-12 02:22:18

问题


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.

Below is the linkedlist, saved as 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.

Cheers.


回答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.

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

Do it like this

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

And When you receive new connection just add new NodeL to 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

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:

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".

Few problems in your class NodeManager:

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

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

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

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:

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

        }
  }
}


来源:https://stackoverflow.com/questions/30006221/extracting-information-from-streams-and-saving-in-linkedlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!