I am trying to access data from my list but it always end up with null or nothing in it. I run my Class Server
That have public static ArrayList<String> clientUsernameList = new ArrayList<>();
In the code. If my server accept a socket this code below should occur.
package multi.threaded_server_application;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.Clock;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.nashorn.internal.codegen.CompilerConstants;
/**
*
* @author Seeya
*/
public class Server {
public static ArrayList<String> clientUsernameList = new ArrayList<>();
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
int port = 8900; //Port number the ServerSocket is going to use
ServerSocket myServerSocket = null; //Serversocket for sockets to connect
Socket clientSocket = null; //Listerner for accepted clients
ClientThread[] clientsConnected = new ClientThread[20]; //Max clients in this server
DataInputStream IN = null;
try {
myServerSocket = new ServerSocket(port);
System.out.println("Server waiting for clients on port:" + port);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
while (true) {
try { //Freezes while-loop untill a socket have been accepted or if some failure occur
clientSocket = myServerSocket.accept();
IN = new DataInputStream(clientSocket.getInputStream());
String userName = IN.readUTF();
Server.clientUsernameList.add(userName);
System.out.println("Client have connected from:" + clientSocket.getLocalAddress().getHostName());
System.out.println("Username:" + userName);
System.out.println(Server.clientUsernameList);
} catch (Exception e) {
//Print out exception
System.out.println(e);
}
//For-loop that counts every element in Array-clientsConnected
for (int i = 0; i < clientsConnected.length; i++) {
//If-statement checks if current element is null
if(clientsConnected[i] == null){
//If current element in the Array is null then create a new object from ClientThread class
//With a socket and the object of itself as parameter.
(clientsConnected[i] = new ClientThread(clientSocket, clientsConnected)).start();
//Must have a break otherwise it will create 20 objects (Exit for-loop)
break;
} //Exit if-statement
} //Exit for-loop
} //Exit while-loop
}
}
I recieve the username from my Class Client
when it is running. I print it out to see if I added a username to the list. Now when I try to access the data from my Class Client
I recieve nothing/null.
package Server_application;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Seeya
*/
public class Client extends Thread {
ClientGUI GUI = null;
DataInputStream IN = null;
DataOutputStream OUT = null;
Socket SOCKET = null;
String Message = null;
String userName = null;
String ipAdress = "localhost";
int port = 8900;
public Client(ClientGUI gui,String username) {
this.userName = username;
this.GUI = gui;
}
@Override
public void run() {
try {
//Handle Input/Output from client
SOCKET = new Socket(ipAdress, port);
IN = new DataInputStream(SOCKET.getInputStream());
OUT = new DataOutputStream(SOCKET.getOutputStream());
//Sends username to server!
OUT.writeUTF(userName);
System.out.println(Server.clientUsernameList);
GUI.addToList(Server.clientUsernameList.toString());
while (true) {
CheckForMessages(IN);
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
//-----------------------------------------------------------------
//Method for sending Messages!
public void write(String s) throws IOException{
//Writes message to server so it can send it to other clients.
//Also sends to clients self GUI
String A = s;
OUT.writeUTF(A);
GUI.writeGUI("You said: " + A);
}
//------------------------------------------------------------------
//Method for listening on messages
private void CheckForMessages(DataInputStream in) throws IOException {
try {
String IncomingMessage = in.readUTF();
System.out.println(IncomingMessage);
GUI.writeGUI(IncomingMessage);
} catch (Exception e) {
System.err.println(e);
}
}
}
All that is printed out is []
from my Class Client
. Is there something wrong with how I try to access my static variable?
EDIT! This is the code I use to send/recieve message to all clients in server.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package multi.threaded_server_application;
import com.sun.security.ntlm.Client;
import java.awt.List;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
/**
*
* @author Seeya
*/
//THIS CLASS TAKES CARE OF HANDLING MESSAGES TO OTHER CLIENTS!
//-------------------------------------------------------
public class ClientThread extends Thread {
private ClientThread[] clientsConnected;
private Socket SOCKET = null;
private DataInputStream IN = null;
private DataOutputStream OUT = null;
private String userName = null;
//-------------------------------------------------------
//Constructor
public ClientThread(Socket socket, ClientThread[] clientThread) {
this.SOCKET = socket;
this.clientsConnected = clientThread;
}
//This starts as soon as the constructor is done/been created.
@Override
public void run() {
try {
//Handleing the messages
IN = new DataInputStream(SOCKET.getInputStream());
OUT = new DataOutputStream(SOCKET.getOutputStream());
String message = null;
while (true) {
//This will be read second from the socket DataOutPutStream
//We will use this string to send a message to every client that is online
message = IN.readUTF();
//For-loop to check how many people are actually online
//for (ClientThread k : clientsConnected) = For each client in clientsConnected ends up in variable k
for (int i = 0; i < clientsConnected.length; i++) {
//don't send message to yourself
if (clientsConnected[i]!= null && clientsConnected[i].userName != this.userName){
// loops through all the list and calls the objects sendMessage method.
clientsConnected[i].sendMessage(message, userName);
}
}
}
} catch (IOException ex) {
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void sendMessage(String message, String userName) {
try {
Date date = new Date();
DateFormat format = new SimpleDateFormat("HH:mm:ss");
OUT.writeUTF(format.format(date) + " " + userName + " says: " + message);
OUT.flush();
} catch (IOException ex) {
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Something failed to send message");
}
}
}