Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:06:07+00:00 2026-06-12T18:06:07+00:00

I have over the past 5 days tried to find out why my program

  • 0

I have over the past 5 days tried to find out why my program does not work i have been asking countless questions in here with good responses and it seems that i have now found the problem.

Background story:

I have a chat program that is devided into two seperate applications a Client and Server. The idea is to make a chat program so ever client has a GUI that contains a picture and some labels and a few textAreas.

Whenever the client starts up the Gui show and the user is able to hit the button connect to try and connect with the server the follwing this is directly what happens in my program and this part of the program works fine:

** The connect button is being pushed**
Client sends to server: 1
Server sends to client: 1
Server sends to client: 1
Client sends to server: Marc (The chat persons username)

Client sends to server: 5
Server sends to client: 1
server sends to client: 1
Server sends to client: Marc

Client sends to server: 8
Server sends to client: 8

And now all hell breaks loose because now the Thread in my Gui Starts. and from here on the program is apprently unable to recive messages from the server from here on. even though the server recives the messages. How can this be?

in order to not post loads of code i am going to post the important parts of my program if you need more code il be glad to update the post

the Thread (in the simpleController (the one that controls the GUI))

public void run(){ 
    System.out.println("Thread started");
    System.out.println(client.getSocket().isConnected());
    ClientListner cl = new ClientListner(client);
    while (client.getSocket().isConnected()) {
        int key = 10;

            if (client.getInput().hasNext()) {
                txt_ChatPerson2.setText(client.reciveString());
                txt_ChatPerson2.setVisible(true);
            }
            try {
                key = client.reciveCommando();
                System.out.println("jeg er her");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        System.out.println("Key "+key);
        switch (key) {
        // case 2 er recive chat:
        case 2:
            // først find ud af hvilket ID der har sendt chatten:
            int y = 0;
            try {
                y = client.reciveCommando();
                System.out.println("y" + y); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // derefter få beskeden og send den så ud til resten.
            String says = client.reciveChat().toString();
            System.out.println("Says :"+says);
            if (y == 1) {
                txt_ChatPerson1.setText(says);
                txt_ChatPerson1.setVisible(true);
            }else if (y == 2) {
                txt_ChatPerson2.setText(says);
                txt_ChatPerson2.setVisible(true);
            }else {
                chatPerson3.setVisible(true);
                txt_ChatPerson3.setVisible(true);
                txt_ChatPerson3.setText(says);
            }

            break;

        default:
            break;
        }
    }
}

The client class

 import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;


public class Client {
// disse var static
    public final static int portNumber = 6040;
    public Socket socket;
    private PrintWriter pw;
    private Scanner input;
    private int clientId;
    /**
     * @param args
     * @throws IOException 
     */

    public Client(Socket socket, PrintWriter pw, Scanner input, int clientId){
        this.socket = socket;
        this.pw = pw;
        this.input = input;
        this.clientId = clientId;
    }
    public void connect() throws IOException{
        // du kan vælge at bruge inetadressen til at connecte i socketet.
        InetAddress adr = InetAddress.getByName("localhost");
        socket = new Socket("localhost", portNumber);
        input=new Scanner(socket.getInputStream());
        pw = new PrintWriter(socket.getOutputStream());

    }
    /**
     * This method sends the message (that the client(chat person) writes to the user)
     * @param x
     * @throws NullPointerException
     * @throws IOException 
     */
    public void SendChat(String x) throws NullPointerException{
            pw.print("CHAT:"+x);
            pw.flush();
        /*  pw.println(2);
            pw.flush();
            pw.println(SimpleController.currentClientId);
            pw.flush();
            pw.println(x);
            pw.flush(); 
        */
    }
    public int sendCommando(int id) throws IOException{
        System.out.println("Jeg sender"+ id);
        pw.println(id);
        pw.flush();
        /*
         * this part of the program sends a command to the server if the command is 1 then 1 is = Connect.
         * the program then ask the server is the server is full or is it ok to connect? 
         * if the response is not 10 then the program will allow a connection to happen the return type will be the Id of which 
         * the chat person becomes!
         */
        // should the method return 0 the Application will do NOTHING!
        switch (id) {
        case 1:
    int k = reciveCommando();
            if (k== 10) {
                return 10;
            }else if (k < 3) {
                System.out.println("returned k" + k);
                return k;
            }else {

            return 10;
            }
            /*
             * Closes the connection with the server!
             */
        case 3:

            socket.close();
            return 0;

        case 5:
            int y  = reciveCommando();
            return y;

        case 8:
            return 8;
        default:
            return 0;
        }

    }
    /*
     * this method recives a command from the server! the comands can be found in the ChatCommands.txt
     * returns the command as an integer!
     */
    public int reciveCommando() throws IOException{
        Integer i = input.nextInt();
        return i;
    }
    /**
     * Gets a String response from the server. This method i used to create other users and give them the correct username.
     * 
     * @param i
     * @return
     * @throws IOException
     */
    public String getStringResponse(int i) throws IOException {
        pw.print(i);
        pw.flush();
        String x = input.nextLine();
        return x;

    }
/*
 * Work in progress - client getter og setter methoder!
 */

public Socket getSocket(){
    return socket;
}
public Scanner getInput(){
    return input;
}
public PrintWriter getPw(){
    return pw;
}
public int getClientId(){
    return clientId;
}

public void setClientId(int i ){
    clientId = i;
}
public String reciveChat(){
        return getInput().nextLine();

}
public String reciveString(){
    String x =input.next();
    return x;
}
public void sendString(String x){
    pw.println(x);
    pw.flush();
}



}

The server code

    import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Server extends Thread {

    private Socket connection;
    private PrintWriter pw;
    private Scanner input;
    private ServerInformation info = new ServerInformation();
    private ChatPerson p;

    public Server(Socket connection, PrintWriter pw, Scanner input){
        this.connection = connection;
        this.pw = pw;
        this.input = input;
    }

    @Override
    public void run() {
        while (connection.isConnected()) {
            if (input.hasNextInt()) {
                System.out.println("Det var sgu da en int");
                int i = getInput().nextInt();
                System.out.println(i);
                checkCommand(i);    
            }else if (input.hasNext()) {
                System.out.println("det var ikke en int");
                String inString = input.nextLine();
                System.out.println(inString);
                chatCase(inString);
            } 
        }


    }
    private void chatCase(String x) {
        String k =x.substring(6 , x.length());
        System.out.println("k "+k);
        pw.print(x);

        pw.flush();
    }

    private void checkCommand(int i) {
        System.out.println("i "+i);
        switch (i) {
        // this case accepts the connection and creates a new user;
        case 1:


int id = info.getNextID();
        pw.println(1);
        pw.flush();
        pw.println(id);
        pw.flush();
        p = new ChatPerson(id, input.next());
        info.addToList(p);
        break;

        // this is the chat case virker ikke endnu
    case 2:
        int clientID = input.nextInt();
        String x = reciveString();
        System.out.println(x);
        pw.println(clientID);
        pw.flush();
        pw.print(x);
        pw.flush();
        break;
    // this case sends information about other chat users to the client
    case 5:
        pw.println(5);
        pw.flush();
        pw.println(info.getList().size());
        pw.flush();
        for (int j = 0; j < info.getList().size(); j++) {
            pw.println(info.getList().get(j).getId());
            System.out.println("info.get "+info.getList().get(j).getId());
            pw.flush();
            pw.println(info.getList().get(j).getName());
            pw.flush();
        }
        break;

    case 8:
        pw.println(8);
        pw.flush();
        break;
    default:
        break;
    }

}

private String reciveString() {
    if (input.next().contains(" ")) {
        String x = input.nextLine();
        return x;
    }
    return input.next();
}

public Socket getConnection(){
    return connection;
}
public PrintWriter getPw(){
    return pw;
}
public Scanner getInput(){
    return input;
}




}

i hope someone is able to help me since this is a huge problem for me if you need more info please just comment and il provide as much as possible

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:06:09+00:00Added an answer on June 12, 2026 at 6:06 pm

    You have to rework your program.

    1. GUI elements should be updated from within the GUI thread (EDT) only, not from a standalone thread.

    2. Client connection should be divided in 2 weakly connected parts: sending messages and receiving them. Receiving should be a separate thread, and sending can be a thread or a class.

    3. When the receiving thread have read a message, it sends it to EDT using SwingUtilities.invokeLater(Runnable). Examples can be found elsewhere.

    4. When user have entered text, the text is send to the socket directly, or enqueued in a queue and later processed by the sending thread, if any.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been several questions over the past few days about the proper use
Over the past few days, I have been trying to find an answer to
I have been pulling my hair out over the past few days with a
I've been asking a lot of questions here over the past few days trying
For the past few days I have been trying to play any sound over
I have been building my own carasol over the past few days. My Jquery
I have been playing around with Visual Studio 2010 over the past few days
Have been learning ASP.NET (using C#) over the past few days. I have made
Over the past couple of days I have been reading into using autotools to
I have been reading about entity framework over the past couple of days and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.