私はこの通気孔のハブシステム用のJavaコードを持っています。しかし、VentNode
オブジェクトは、リスト挿入値と同じすべてのアクティブ値を書き込んでいます。私はString array
を作成して、同じことをするかどうかをテストしましたが、String array
が「正常に」動作しました。私のオブジェクト配列はすべて自分自身で上書きされますが、文字列配列はそうではありません - Java
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ThreadedEchoServer {
private static final int port = 50103;
private static final int timeout = 500;
private VentNode[] nodes = new VentNode[256];
private String[] ips = new String[256];
public static void main(String args[]) {
ThreadedEchoServer server = new ThreadedEchoServer();
Scanner scanner = new Scanner(System.in);
System.out.println("Vent system 10,000");
server.scan();
server.list();
while(true){
System.out.print("$ ");
String msg = scanner.next();
if(msg.equals("scan")){
server.scan();
}else if(msg.equals("on")){
server.command(msg);
}else if(msg.equals("off")){
server.command(msg);
}else if(msg.equals("list")){
server.list();
}else if(msg.equals("shutdown")){
System.out.println("Shutting down");
break;
}else{
System.out.println("Unknown command: " + msg);
}
}
scanner.close();
}
public void command(String cmd){
for (VentNode node : nodes) {
if (node != null){
Socket s;
try {
s = new Socket(node.getSocket(),port);
new EchoThread(s,cmd);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("Error connecting to node " + node.getId());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error connecting to node " + node.getId());
e.printStackTrace();
}
}
}
}
public void list(){
for (String node : ips) {
if (node != null)
System.out.println("Socket " + node + ": " + node);
}
for (VentNode node : nodes) {
if (node != null)
System.out.println("Socket " + node.getId() + ": " + node.getSocket());
}
}
public void scan(){
System.out.println("Scanning for vents. This will take a minute.");
try {
String currentIP = InetAddress.getLocalHost().toString();
String subnet = getSubnet(currentIP);
for (int i=13;i<42;i++){
Socket socket;
String host = subnet + i;
if (InetAddress.getByName(host).isReachable(timeout)){
//System.out.println(host + " is reachable");
try {
socket = new Socket(host, port);
System.out.println("Adding vent: " + i);
new EchoThread(socket);
nodes[i] = new VentNode(host,i,0,0);
ips[i] = host;
}catch(Exception s){
//System.out.println("failed to connect to " + host + " on port " + port);
}
}
}
}catch(Exception e){
System.out.println(e);
}
}
private String getSubnet(String currentIP) {
int firstSeparator = currentIP.lastIndexOf("/");
int lastSeparator = currentIP.lastIndexOf(".");
return currentIP.substring(firstSeparator+1, lastSeparator+1);
}
}
と、次のように出力は以下のとおりです。
Vent system 10,000
Scanning for vents. This will take a minute.
Adding vent: 14
Adding vent: 41
Socket x.x.x.14: x.x.x.14
Socket x.x.x.41: x.x.x.41
Socket 41: x.x.x.41
Socket 41: x.x.x.41
$
あなたはそれが私がしようとして時間苦労することができます41 14ベントと41ベントの両方に書き込み、最後の二つの出力で見ることができるようになぜこれが起こっているのか理解してください。私はやめて専門家に尋ねると思った。
ありがとうございます!
ようこそスタックオーバーフロー! [ツアー](http://stackoverflow.com/tour)を見て回り、[ヘルプセンター](http://stackoverflow.com/help)、特に[どのように私に質問しますか良い質問?](http://stackoverflow.com/help/how-to-ask)と[ここで私はどのような話題を聞くことができますか?](http://stackoverflow.com/help/on-topic)。 - クラスventNodeのコードを表示してください。 –
そして、Java命名規約に固執してください。クラス名**は大文字で始まる必要があります。 'ventNode'は' VentNode'でなければなりません。 –