-1

I'm trying to loop the writeUTF function in a thread and it only runs once despite it being in a while loop. The purpose is to loop through a large byte[] and send it in packets and i'm stuck on writeUTF only running once.

I tried a for loop with dos.flush() outside the loop and that didn't work. writeUTF only runs once in the while loop. A sys.out.print loops but not dos.writeUTF.

If I can loop the writeUTF function I can send a different subsection of the giant byte[] page with each iteration and get some sleep!

Here's what I have:

Server

public void runServer() throws IOException {
    // server is listening on port 22122 
    ServerSocket ss = new ServerSocket(22122);

    while (true) {
        Socket s = null;

        try {
            s = ss.accept();
            System.out.println("Server: A new client is connected : " + s);

            // obtaining input and out streams 
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

            // create a new thread object 
            System.out.println("Server: Creating Client Handler thread");
            Thread t = new ClientHandler(s, dis, dos, page);

            // Invoking the start() method 
            t.start();

        } catch (Exception e) {
            s.close();
            e.printStackTrace();
        }
    }
}

ServerThread

public ServerThread(Socket s, DataInputStream dis, DataOutputStream dos, byte[] page) {
    this.s = s;
    this.dis = dis;
    this.dos = dos;
    this.page = page;
}

@Override
public void run() {
    while (true) {
        try {
            dos.writeUTF("Repeated text");
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Output:

Server: A new client is connected :
Server: Creating Client Handler thread
Client: Repeated text

In the output, the Client shows it received it once, but I want it to keep receiving. Anything helps thank you!

Billy
  • 1
  • 1

1 Answers1

-1

Turns out my while loop on the Client side was asking for input every iteration. Once I took that line and put it outside my loop it began looping how I expected it to.

Billy
  • 1
  • 1