阵列失去价值/回报为null初始化为循环后 [英] Array loses value/returns to null after initialized in for loop
问题描述
我所试图做的是从客户端发送邮件 - >服务器,然后发回给其它客户多数民众赞成连接到服务器(多线程)。从不同的客户端发送到服务器工作正常,但是当我试图把它送回来,什么都不会发生。
What I am trying to do is send a message from Client -> Server, then send back to every other client thats connected to the server (multithreaded). Sending from different clients to the server works fine, but when I try to send it back, nothing happens.
每一次有人连接,它会在我的用户[]数组的新对象。就这样,我可以可以触发输出流为每个用户(用户中有一个Stream对象,其中包含了输入和输出流,所以每次创建新的用户对象的时间,所以是一种新的流)。
Each time someone connects, it creates a new object in my User[] array. With that, I can can trigger the output stream for each user (User has a Stream object in it, which contains both input and output streams, so each time a new User object is created, so is a new stream).
有了这个,我试图发送进来回给所有其他用户在我的用户阵列的消息(这并不== NULL)。
With this, i'm trying to send the message that came in back out to every other User in my user array (that doesnt == null).
问题是,当我在我的用户[]数组发送消息回来,我所有用户的一部分又回到空。
The problem is, when I get to the part of sending the message back, all my Users in my User[] array are back at "null".
Server.java:
Server.java:
package Main;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ForkJoinPool;
import Streams.Stream;
public class Server {
public static final int maxConnections = 10;
ServerSocket serverSocket; Socket socket;
ForkJoinPool pool = new ForkJoinPool();
static User[] users = new User[maxConnections];
public Server() {
try {
serverSocket = new ServerSocket(43594);
while(Stream.streams < maxConnections) {
socket = serverSocket.accept();
for(User user : users) {
if(user == null) {
user = new User(socket);
System.out.println(user +", 1");
pool.execute(user);
System.out.println("Someone has joined the chat!");
break;
} else {
System.out.println("Connection declined. Too many users.");
break;
}
}
}
}catch(IOException e) { e.printStackTrace(); }
}
public static void main(String[] args) {
new Server();
}
}
User.java:
User.java:
package Main;
import java.io.IOException;
import java.net.Socket;
import Streams.Stream;
public class User implements Runnable {
Stream stream;
public User(Socket socket) {
stream = new Stream(socket);
}
public void run() {
String textInput, textOutput;
while(stream.exists()) {
try{
textInput = (String) stream.recieveData();
sendGlobalMessage(textInput);
}catch(IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e) { e.printStackTrace(); }
}
stream.close();
}
public void sendMessage(String message) throws IOException {
stream.sendData(message);
}
public void sendGlobalMessage(String message) throws IOException {
for(User user : Server.users) {
if(user == null) {
System.out.println(message);
}else{
user.sendMessage(message);
}
}
}
}
Stream.java:
Stream.java:
package Streams;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Stream {
public static int streams = 0;
Socket socket;
ObjectInputStream input; ObjectOutputStream output;
Object data;
public Stream(Socket userSocket) {
streams++;
socket = userSocket;
try{
input = new ObjectInputStream(userSocket.getInputStream());
output = new ObjectOutputStream(userSocket.getOutputStream());
}catch(IOException e) { e.printStackTrace(); }
}
public void sendData(Object data) throws IOException {
output.writeObject(data);
output.flush();
}
public Object recieveData() throws IOException, ClassNotFoundException {
return data = input.readObject();
}
public boolean exists() {
if(socket.isClosed()) return false; else return true;
}
public void close() {
try {
input.close();
output.close();
socket.close();
}catch(IOException e) { e.printStackTrace(); }
}
}
我不知道,如果它与我的服务器的结构做。它看起来pretty罚款对我来说,我真的不知道为什么它会做这个。
I'm not sure if it has to do with the structure of my server. It looks pretty fine to me, i'm really not sure why it would be doing this.
推荐答案
的
用户参考你在这里是数组中的引用的副本,而不是实际的参考
The User
reference you have here is a copy of the reference in the array, not the actual reference.
for(User user : users) {
if(user == null) {
user = new User(socket);
这是等同于
User user = user[i];
更改参考用户
不修改参考用户[I]
。
您需要使用索引循环初始化(或rereference)数组中的元素
You need to use an indexed for loop to initialize (or rereference) the elements of the array
for (int i = 0; i <users.lenght; i++) {
if(users[i] == null) {
users[i] = new User(socket);
} ...
...
}
这篇关于阵列失去价值/回报为null初始化为循环后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!