Home > Back-end >  What is causing the loop in my basic client/server socket program to fail on second attempt? [closed
What is causing the loop in my basic client/server socket program to fail on second attempt? [closed

Time:09-18

I made a basic socket program to send messages between the server and client on the same host. I am able to send one message but on the second message it exits. The problem is that it should only exit when I type "Bye". I know something is wrong with my while loop but I am not sure why. Here is my code:

Client:

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client3 {

    public static void main(String[] args) {
        Socket socket = null;
        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;

        try {
            socket = new Socket("localhost", 4321);

            inputStreamReader = new InputStreamReader(socket.getInputStream());
            outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

            bufferedReader = new BufferedReader(inputStreamReader);
            bufferedWriter = new BufferedWriter(outputStreamWriter);

            Scanner scanner = new Scanner(System.in);
             boolean loopIT;
             loopIT = true;

            while (loopIT) {
                String msgToSend = scanner.nextLine();
                bufferedWriter.write(msgToSend);
                bufferedWriter.newLine();
                bufferedWriter.flush();

                System.out.println("Server:"   bufferedReader.readLine());
                if(msgToSend.equalsIgnoreCase("Bye")) {
                    loopIT = false;
                } else {
                    loopIT = true;
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(socket != null)
                    socket.close();
                if(inputStreamReader != null)
                    inputStreamReader.close();
                if(outputStreamWriter != null)
                    outputStreamWriter.close();
                if (bufferedReader != null)
                    bufferedReader.close();
                if (bufferedWriter != null)
                    bufferedWriter.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Server (This should be right but I will add it for context):

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server2 {
    public static void main(String[] args) throws IOException {
        Socket socket = null;
        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        ServerSocket serverSocket = null;

        serverSocket = new ServerSocket(4321);

        while(true) {

            try{
                socket = serverSocket.accept();
                inputStreamReader = new InputStreamReader(socket.getInputStream());
                outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

                bufferedReader = new BufferedReader(inputStreamReader);
                bufferedWriter = new BufferedWriter(outputStreamWriter);

                while(true) {
                    String msgFromClient = bufferedReader.readLine();

                    System.out.println("Client:"   msgFromClient);

                    bufferedWriter.write("MSG received!");
                    bufferedWriter.newLine();
                    bufferedWriter.flush();

                    if (msgFromClient.equalsIgnoreCase("Bye"));
                    break;
                }

                socket.close();
                inputStreamReader.close();
                outputStreamWriter.close();
                bufferedReader.close();
                bufferedWriter.close();


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

CodePudding user response:

In your server, you have an erroneous ; on this statement:

if (msgFromClient.equalsIgnoreCase("Bye")); // <-- HERE
break;

Thus the break; statement is always executed unconditionally, terminating the connection after the 1st message is received and responded to.

You need to remove that ;:

if (msgFromClient.equalsIgnoreCase("Bye"))
    break;
  • Related