Search code examples
javatcpclient-server

Java TCP Client-Server - stuck in infinite loops in both applications


I have a Client and a Server, they should have a communication in both ways. Everything worked well, client sent some information to server, and server did something with that information. Now that I tried to implement server replying to. After I've tried implementing that, both programs are now stuck in an infinite loop, waiting for information from the other side.

Here is my code for the server side:

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

    logger.log(Level.INFO, "args[0]: {0} args[1]: {1} args[2]: {2} args[3] {3}", new Object[]{args[0], args[1], args[2], args[3]});

    pathToExcel = args[0];
    pathToDatabase = args[1];
    numberOfAccounts = Integer.parseInt(args[2]);
    portNumber = Integer.parseInt(args[3]);

    listIE = new ArrayList<>();

    listIE = Loader.getList(numberOfAccounts, pathToExcel);

    DBBroker.createTables(pathToDatabase);
    System.out.println("Check value: " + DBBroker.checkDB());
    if (DBBroker.checkDB() == false) {
        DBBroker.insertData();
        DBBroker.insertDataBalance();
    } else {
        System.out.println("Data has already been inserted into the database");
    }
    startServer();
}

public static void startServer() throws IOException {
    //ServerSocket ss = new ServerSocket(portNumber);
    ServerSocket ss = new ServerSocket(portNumber);
    logger.log(Level.INFO, "Server started on port number: {0}", portNumber);
    try {
        while (true) {
            Socket clientSocket = ss.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            DataOutputStream clientOutput = new DataOutputStream(clientSocket.getOutputStream());
            System.out.println("Client connected ");
            //***************************************************************************
            String answer = input.readLine();
            //***************************************************************************
            System.out.println("prosao readline");

            //logger.info("Client logged in on port " +portNumber);

            String[] niz = answer.split("_");
            //System.out.println("niz: " +Arrays.toString(niz));
            serverPortNumber = Integer.parseInt(niz[0]);
            accountName = niz[1];
            receiverName = niz[2];
            amount = Integer.parseInt(niz[3]);
            //System.out.println("Server port number: " +serverPortNumber + " accountname: " +accountName +" receiver name: " +receiverName + " amount: " +amount);
            parseRequestFromClient();

            System.out.println("Prosao request");
            
            
            clientOutput.writeBytes("Kraj");
            clientSocket.close();

        }
        //ss.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And here is my code for the client side:

    public static void main(String[] args) throws IOException {
    String messageFromServer = "";
    logger.log(Level.INFO, "args[0]: {0} args[1]: {1} args[2]: {2} args[3] {3}", new Object[]{args[0], args[1], args[2], args[3]});
    Socket socket = new Socket("localhost", Integer.parseInt(args[0]));
    //logger.info("args[0]: " +args[0] +" args[1]: " +args[1] +" args[2]: " +args[2] +" args[3] " +args[3]);
    
    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));


    String dataForServer = args[0]+"_"+args[1]+"_"+args[2]+"_"+args[3]; 
    System.out.println("Data for server: " +dataForServer);
    outputStream.writeBytes(dataForServer);
    System.out.println("prosao dataforserver");

    //***************************************************************************
    String answer = input.readLine();
    //***************************************************************************
    
    System.out.println("prosao readline");
    System.out.println(answer);
    
    socket.close();
}

Server side gets stuck at the ss.accept() line, while the Client side gets stuck at input.readLine()

I didn't add the whole project because a large portion of it is not relevant to the problem and it has a lot of code.


Solution

  • Your server was blocking on readLine(). It blocks for a line terminator. The client was only sending a raw string. The solution is to send a line terminator with each raw string. The same applies when the server responds to the client.

    As Simon has pointed out a printwriter would be a good choice if your message protocol is to pass line terminated strings.