0

I am trying to write a program, where Client is sending a string to a Server using output stream. Server would see what string it is, it compares it to a conditional, and then sends it back to the client and displays a message on client's side. I would like to understand how it works, and what is wrong with my code so far. Once I get it I will implement it with JavaFX.

Client

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class MyClient {

    static Socket socket;
    static int clientNo;
    static DataOutputStream toServer;
    static DataInputStream fromServer;

    public static void main(String[] args) {
        try{
            socket = new Socket("localhost", 8888);
            System.out.println("Client connected to the server");

            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());

            toServer.writeBytes("listAll");
            toServer.flush();
            System.out.println("Sending string to the ServerSocket");

            } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }  
    }

}

Server

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;



public class MyServer {

    static final int PORT = 8888;

    static ServerSocket serverSocket;
    static Socket socket;

    public static void main(String[] args) {
            try {
                serverSocket = new ServerSocket(PORT);
                System.out.println("Server started at " + new Date());
                System.out.println("Server on PORT: " + PORT + " is open");
                System.out.println("Server is ready for multiple clients!");
                System.out.println("Waiting for request...");
                while(true){
                    socket = MyServer.serverSocket.accept();
                    new Thread(new HandleClient(MyClient.socket)).start();
                }

            } catch (IOException ex) {
            System.err.println("Server Error: " + ex.getMessage());

            }
    }
}

Thread

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class HandleClient implements Runnable {

    private Socket socket;
    public static int clientNo = 1;
    public static int condition;

    static DataInputStream input ;
    static DataOutputStream output;
    static String message;

    public HandleClient(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        switch (condition) {
            case 1:
                break;
            case 2:
                System.out.println("list all");
                break;
            case 3:
                break;
            default:
                System.out.println("Client #" + clientNo + " connected!");
                clientNo++;
                try {

                    input = new DataInputStream(socket.getInputStream());
                    output = new DataOutputStream(socket.getOutputStream());

                        message = input.readUTF();
                        System.out.println(message);


                        if("listAll".equals(message)){
                            HandleClient.condition = 2;
                            new Thread(new HandleClient(socket)).start();
                        } 
                } catch (IOException ex) {
                    System.err.println("One of the clients disconnected");
            }
                break;
        }
    }
}

I really appreciate any help! I understand I might have some code missing to send the response back to the client? Please let me know if the direction I am going is good.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Marcel Kredzel
  • 45
  • 1
  • 2
  • 8
  • Please describe what doesn't work with your code right now. As a possible cause of problems: in `HandleClient`, none of the fields should be static, static fields are shared with all instances of that class, which is definitely not what you want in a server. – Mark Rotteveel Jan 24 '20 at 14:36
  • Here is the exception I am getting: Exception in thread "Thread-0" java.lang.NullPointerException at HandleClient.run(HandleClient.java:35) at java.lang.Thread.run(Thread.java:748) Server is up and running, but any time I run the client this error occurs – Marcel Kredzel Jan 24 '20 at 14:43
  • In addition to what Mark's [comment](https://stackoverflow.com/questions/59898462/how-to-use-input-output-streams-correctly-server-client-threads#comment105924281_59898462), you're using `writeBytes` when writing to the server, but `readUTF` when reading from the client. I recommend you read the documentation of both methods to see why that's incorrect. – Jacob G. Jan 24 '20 at 14:44
  • See [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Also, looking at your code, you really should not do `new Thread(new HandleClient(socket)).start();` in the `"listAll".equals(message)` condition. Doing that makes zero sense. – Mark Rotteveel Jan 24 '20 at 14:45
  • @JacobG. Thank you for bringing that up. I changed writeBytes to writeUTF. – Marcel Kredzel Jan 24 '20 at 14:54
  • @MarkRotteveel I removed new Thread from there and replaced it with desirable 'sout' output. I read about NullPointerException, however I still can't find the reason for it to happen :/ Will keep trying... – Marcel Kredzel Jan 24 '20 at 14:54

0 Answers0