This is my Server class that opens a server socket to get information from one client and then uses the class Users to send the String to all users in the array:
public class Server {
static ServerSocket server;
static Socket client;
static DataOutputStream out;
static DataInputStream in;
static int port = 7767;
static Users[]User = new Users[10];
public static void main(String[] args)throws Exception{
try {
System.out.print("starting");
server = new ServerSocket(port);
while((client = server.accept() )!= null ){
for(int i=0; i<10; i++){
// System.out.println("input connection");
out= new DataOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
if(User[i] == null)
{
User[i] = new Users(out, in, User);
Thread thread = new Thread(User[i]);
thread.start();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
This Users class just holds information of who is a client and then sends all users the string that the Server was sent, i used to get a NullPointException for message = in.readUTF();
but now, nothing happens at all.
public class Users implements Runnable {
DataOutputStream out;
DataInputStream in;
Users[] User = new Users[10];
public Users(DataOutputStream out, DataInputStream in, Users[] User)
{
this.in = in;
this.out = out;
this.User = User;
}
public void run(){
while(true){
try{
BufferedReader bri = new BufferedReader(new InputStreamReader(in));
String message;
message = bri.readLine();
for(int i=0; i<10; i++){
if(User[i] != null)
{
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
User[i].br.write(message + "\n");
}
}
}
catch(IOException e){
// catching and doing something about it and stuff
}
}
}`
Client:(minus all the swing stuff to save question space)
public class Client implements WriteGui {
static Socket client;
static DataInputStream in;
static DataOutputStream out;
JTextArea msgout;
private JFrame frame;
private JTextField msgA;
private JTextField nameA;
/**
* Create the application.
*/
public Client() {
initialize();
CStart();
}`
public void actionPerformed(ActionEvent arg0) {
if(nameA.getText() == ""){
nameA.setText(getname());
}
String message;
message = (nameA.getText()+": "+ msgA.getText());
try {
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
br.write(message+"\n");
} catch (IOException e) {
msgout.append("error!");
}
}
public void write(String s) {
msgout.append(s+ "/n" );
}
private String getname(){
return JOptionPane.showInputDialog(frame,"fill out name" , "name", JOptionPane.QUESTION_MESSAGE);
}
public void CStart(){
int port = 7767;
String host = "localhost";
try {
client = new Socket(host, port);
msgout.append("starting");
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
Input input = new Input(in, this);
Thread thread = new Thread(input);
thread.start();
}
catch(Exception e) {
msgout.append("error");
}
I Also have a short input class that inputs the messages into the client:
public class Input implements Runnable {
WriteGui gui;
DataInputStream in;
static BufferedReader br;
public Input(DataInputStream in, WriteGui gui){
this.in = in;
this.gui = gui;
}
public void run() {
String message;
br = new BufferedReader(new InputStreamReader(in));
try {
message = br.readLine();
gui.write(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
like I said, i got the error traced to the Users class, but the NullPointerException is not guaranteed to happen every time. It still seems to be in the Server/Users code though as it uses more resources at it should. Can anyone help me figure out why this Datainputstream does not seem to work?
Edit: it stopped producing NullPointerException, and now after pressing start after several attempts instead of the message, it sends a box shape. So it does seem to be a problem with the UTF reading. even with changing the OutputStreamWriter to just a DataInputStream, it will not work.