3

I'm trying to make an console chat application using MulticastSocket for school exercise. However my problem is that I don't know how I can make the chat lines coming from another person display whilst the client is typing/waiting for input. Now if there is chat lines incoming, it interrupts the inputline as so (user1's message goes directly after the input that has been written out so far): enter image description here

So the question is: is there anyway to keep the "username: message" line always in the bottom of the console and let the incoming messages print out normally above it? And after the user presses enter to send message, it prints it into the console.

My code so far:

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

public class MulticastChatLahettaja implements Runnable {
    //this bool keeps the while-loops running
    public static boolean paalla = true;

    Thread t;

    public MulticastChatLahettaja(){
        t = new Thread(this, "ABC");
        t.start();
    }

    @Override
    public void run(){

        //Here inside the loop it would try to receive packets from the
        //socket and print them
        while (paalla){

            //test chat lines
            System.out.println("user1: some chat lines, mumble mumble...");
            try{
                //Put the thread to sleep for 5s to prevent spamming
                //when testing
                Thread.sleep(5000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {

        //Around here it connects to the MulticastSocket
        String ip = "239.0.0.1";
        int portti = 6666;
        Console console = System.console();

        //Enters name
        String nimi = console.readLine("Syota Nimi:");

        //start Thread
        new MulticastChatLahettaja();

        System.out.println("Liityttiin ip: "+ip+", porttiin:   "+Integer.toString(portti)+", nimella: "+nimi);

    System.out.println("Sulje asiakas syottamalla ':quit'");

    try {

        //In here I'm waiting for the input and sending it to the Socket
        while (paalla) {

            String syote = console.readLine(nimi+": ");

            //If input is ":quit" it stops the loops and application closes
            if (syote.equals(":quit")) paalla = false;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        }
    }
}

(It is very raw stage yet, since I'm just trying to get the input and output to work smoothly)

Juuseri
  • 41
  • 4
  • As long as your application is restricted to a command line interface, that's something you'll have to deal with. Changing to a GUI would let you divorce your input stream from your output stream. – ankh-morpork Aug 10 '15 at 14:41
  • @dohaqatar7 Damn, I was afraid that would be the case :( Guess I'll have to start looking into how to make GUIs :D Thanks for the anwer! – Juuseri Aug 11 '15 at 21:24
  • Any response to my answer below? – Gray Oct 06 '15 at 13:21
  • hi there, I have the same question but I see this is for a year ago. could you find anything? – Sep GH Oct 29 '16 at 13:35

1 Answers1

0

You can implement this on the command line but it is hard. When a chat comes in, you can print a "\r" which is a CR and takes the cursor to the start of the line, draw the chat from the remote user, and then redraw the prompt and the input the user has entered.

The problem is that you would need to be able to see each user input character and by default, all applications start in "line mode". See: How to read a single char from the console in Java (as the user types it)?

There is no good portable solution for doing this but if you are just running on Linux or MacOS there are ways to get it to work.

Community
  • 1
  • 1
Gray
  • 108,756
  • 21
  • 270
  • 333