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

  • SEARCH
  • Home
  • 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 8077481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:29:15+00:00 2026-06-05T15:29:15+00:00

Got a programm with 2 threads. one thread is writing some stuff into the

  • 0

Got a programm with 2 threads. one thread is writing some stuff into the console.

public class Konsole extends Thread {

    static int id = 0;

    Client client;

    public Konsole(Client client) {
        this.client = client;
    }

    public void run() {
        System.out.println("========= Konsole "+ ++id +" started");

    while(true) {


        try {

            String line = client.fromServer.readLine();

            client.commands.add(line);
                System.out.println(line);

                if(line == null) {break;}


        } catch (IOException e) {}

    }

in the Client class exits a public static stack.
But the stack is allways empty, the Konsole isn’t able to access the stack.
Can someone give me a hint why?

Client class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Stack;
import java.util.StringTokenizer;


public class Client extends Thread {

    // Verbindungsvariablen
    final String user = "user";
    final String pass = "pass";
    public String host = "localhost";
    int port = 21;


    // PASV
    int portNew;
    String ipNew = "";

    public static Stack<String> commands = new Stack<String>();

    boolean lastCommand = false;

    // Sockets
    Socket serverSocket;
    Socket pasvSocket;
    PrintWriter writeCommands;
    BufferedReader fromServer;

    PrintWriter writePasvCommands;
    BufferedReader fromPasvServer;

    // Baut die Verbindung auf
    public void connect() throws IOException {

        try {
            serverSocket = new Socket(host, port);
            System.out.println("Baue Verbindung auf ...");
            fromServer = new BufferedReader(new InputStreamReader(
                    serverSocket.getInputStream()));
            writeCommands = new PrintWriter(serverSocket.getOutputStream(),
                    true);
        } catch (UnknownHostException e) {
            System.out.println("Host unbekannt!");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void USER() throws IOException {
        writeCommands.print("USER " + user + "\n");
        writeCommands.flush();
    }

    public void PASS() {
        writeCommands.print("PASS " + pass +"\n");
        writeCommands.flush();
    }

    public void NOOP() {
        writeCommands.print("NOOP\n");
        writeCommands.flush();
    }

    public void getStatus() {
        Thread konsole = new Thread(new Konsole(this));
        konsole.start();
    }

    // PASV anschalten
    public void PASV() throws IOException, InterruptedException {

        writeCommands.print("PASV\n");
        writeCommands.flush();
        //getPasvCon();


    }
public void getPasvCon() throws IOException, InterruptedException {
        System.out.println("!!!!!!");
        // Commands abholen


        // IP Adresse holen

        String pasv = commands.lastElement();
        String ipAndPort = pasv.substring(pasv.indexOf("(") + 1,
                pasv.indexOf(")"));

        StringTokenizer getIp = new StringTokenizer(ipAndPort);

        // holt die IP
        String ipNew = "";   // IP für den neuen Socket
        for (int i = 0; i < 4; i++) {
            if (i < 3) {
                ipNew += (getIp.nextToken(",") + ".");
            } else {
                ipNew += (getIp.nextToken(","));

            }
        }


        Integer portTemp1 = new Integer( getIp.nextToken(","));
        Integer portTemp2 = new Integer (getIp.nextToken(","));
        portNew = (portTemp1 << 8 )+ portTemp2;


        try {

            pasvSocket = new Socket(ipNew, portNew);

        } catch (UnknownHostException e) {
            System.out.println("Host unbekannt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            fromPasvServer = new BufferedReader(new InputStreamReader(
                    pasvSocket.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            writePasvCommands = new PrintWriter(pasvSocket.getOutputStream(),
                    true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   




    }

    public void LIST() throws IOException  {
    writeCommands.print("LIST\n");
    writeCommands.flush();

    }


    public void run() {
        try {
            connect();

    //      getStatus();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            USER();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PASS();


        try {
            PASV();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Client() throws IOException {

    }



    public static void main(String[] args) throws IOException, InterruptedException {
        Client test = new Client();
        Thread client = new Thread(test);
        client.start();

        Thread.sleep(300);


        Thread konsole = new Thread(new Konsole(test));
        konsole.start();



        Disrupter disrupt = new Disrupter(test);
        disrupt.start();



    }



}

(static is senseless, isn’t it?)

  • 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-05T15:29:16+00:00Added an answer on June 5, 2026 at 3:29 pm

    I have solved my problem using Thread.sleep().

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

Sidebar

Related Questions

In one of my classes, I have got code: private static void Notify(string url,
It seems that I've finally got to implement some sort of threading into my
I've got the following simple code: package main; import java.util.concurrent.*; public class Main {
While deploying my application, I got the error message: Thread 1:Program received signal: EXC_BAD_ACCESS.
If variables in Java are accessed from multiple threads, one must ensure that they
This one is a tough one... I've got a C# app that runs an
On a regular basis I find myself writing little utility programs that use some
I'm writing a game. I have StartScene class: In StartScene.h: @interface StartScene : UIView
I have a Visual C++ program that opens a file in one thread with
I've got a program that opens N sockets to a server, each one in

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.