I’m writing socket programming with Java. I have 2 different threads running on the server. When a user connects to the server, Login and Register threads are executed. I think there is a problem related to creating sInput and sOutput objects in the thread section. The system works correctly when only the register or login thread runs on the server. However, when I run both the login and register threads on the server, I get an error. The error is as follows:
“Error connecting to server: java.net.ConnectException: Connection refused: connect Exception in thread “Thread-2” Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException: Cannot invoke “java.io.DataInputStream.read(byte[])” because “this.sInput” is null at client_pkg.Client.run(Client.java:81) java.lang.NullPointerException: Cannot invoke “java.io.DataOutputStream.write(byte[])” because “this.sOutput” is null”
I think I need to use a different socket for each connection, but when I change the code like that, the messages from the client no longer reach my login or register threads. So, the connection is established to the server, but the server cannot establish communication between the threads and the client.
How can I solve this problem?
public class Server extends Thread {
public ServerSocket serverSocket;
private int port = 8090;
//Sisteme kayıt olan kullanıcılar
//Servera bağlanan kullanıcılar
public ArrayList<SClient> current_user_list;
public int client_id = 0;
public HashMap<String, String> system_users;
boolean listening = false;
//Constructor
Register_thread register_thread;
Login_thread login_thread;
public Server() {
system_users = new HashMap<>();
}
public void start_server() {
try {
// Client Soket nesnesi
serverSocket = new ServerSocket(this.port);
getSystem_users().put("admin", "admin");
current_user_list = new ArrayList<SClient>();
} catch (Exception err) {
System.out.println("Error connecting to server: " + err);
}
}
public void start_listening() {
this.listening = true;
//Thread'i baslat
this.start();
}
@Override
public void run() {
while (this.listening) {
try {
System.out.println("Server waiting client...");
Socket clientSocket;
clientSocket = this.serverSocket.accept();
//İstek register için mi yollandı, kontrol et
//register_thread = new Register_thread(clientSocket,this);
//register_thread.Listen();
login_thread = new Login_thread(clientSocket, this);
login_thread.Listen();
//login oldugunda Sclient'a ekle
if(login_thread.success_login)
{
SClient sc = new SClient(clientSocket, this);
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class Login_thread extends Thread {
public Socket socket;
// gönderilecek alınacak bilgileri byte dizisine çevirmek için
private DataInputStream sInput;
//private DataInputStream sInput;
private DataOutputStream sOutput;
public boolean isListening = false;
//sistemde kayitlimi
public boolean to_login = false;
//Yapılan request register icin mi
public boolean request_login = false;
public boolean success_login = false;
public Server server;
public Login_thread(Socket socket, Server server) {
try {
this.socket = socket;
this.sInput = new DataInputStream(this.socket.getInputStream());
this.sOutput = new DataOutputStream(this.socket.getOutputStream());
this.server = server;
} catch (IOException ex) {
Logger.getLogger(Register_thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void check_request_is_login(String message) {
String[] message_arr = message.split(",");
for (String string : message_arr) {
System.out.println(string);
if (string.equals("Login")) {
System.out.println("istek login");
this.request_login = true;
break;
}
this.request_login = false;
}
}
public void SendMessage(byte[] msg) {
try {
sOutput.write(msg);
} catch (IOException err) {
System.out.println("Exception writing to server: " + err);
}
}
// Kullanıcı adı ve şifreyi kontrol eden fonksiyon
public void checkCredentials(String username, String password) {
// Kullanıcı adının HashMap'te olup olmadığını kontrol edelim
HashMap<String, String> userCredentials = server.system_users;
if(userCredentials.containsKey(username)) {
// Kullanıcı adı varsa, şifreyi kontrol edelim
String storedPassword = userCredentials.get(username);
System.out.println("Parola"+storedPassword);
if(storedPassword.equals(password)) {
// Eğer şifre doğruysa, giriş başarılıdır
this.to_login = true;
}
else{
this.to_login = false;}
}
// Kullanıcı adı veya şifre hatalıysa, giriş başarısızdır
}
public void Disconnect() {
try {
this.isListening = false;
this.socket.close();
this.sInput.close();
this.sOutput.close();
} catch (IOException ex) {
}
}
}
public class Register_thread extends Thread {
public Socket socket;
// gönderilecek alınacak bilgileri byte dizisine çevirmek için
private DataInputStream sInput;
//private DataInputStream sInput;
private DataOutputStream sOutput;
public boolean isListening = false;
//sistemde kayitlimi
public boolean to_register = false;
//Yapılan request register icin mi
public boolean request_is_register = false;
public boolean success_register = false;
public Server server;
public Register_thread(Socket socket, Server server) {
try {
this.socket = socket;
this.sInput = new DataInputStream(this.socket.getInputStream());
this.sOutput = new DataOutputStream(this.socket.getOutputStream());
this.server = server;
} catch (IOException ex) {
Logger.getLogger(Register_thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
I want to learn how I can make my code work on different ports.