Program won't continue after initializing input/output streams?

Dioxin

Before people suspect that I have no idea what I'm doing at all (and end up voting this down for no reason at all), please read this:

It connects to my server just fine! I'm getting no errors (from the client OR server), and my server is recognizing the connection. It works with my friend's client that he made, but I wanted to make my own client, and apparently I'm doing something wrong. PLEASE STAY ON TOPIC! Thanks :)


Title basically says it all. I've tested with println messages above and below the setupStream() in my Client.java run(), but only the message above the setupStream() prints. I'm not sure how I'm supposed to initialize my stream without making my program come to a halt.

Client.java

import java.io.IOException;


public class Client extends Stream implements Runnable {
    public boolean running = false;
    private Thread clientThread;

    Frame frame;
    public Client() {
        super("localhost", 43594);

        frame = new ClientFrame(500, 500);
        start();
    }

    public synchronized void start() {
        if(running) return;
        running = true;

        clientThread = new Thread(this);
        clientThread.start();
    }
    public synchronized void stop() {
        if(!running) return;
        running = false;

        clientThread.interrupt();
        try {
            clientThread.join();
        } catch (InterruptedException e) {e.printStackTrace();}
    }

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

        while(running) {
            System.out.println("running");
        }
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try{
            out.close();
            in.close();
            socket.close();
            clientThread.join();
            }catch(IOException | InterruptedException e) { e.printStackTrace(); }
        }
    }
    public static void main(String[] args) {
        new Client();
    }
}

Stream.java

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class Stream {

    Socket socket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String data;

    public Stream(String host, int port) {
        try {
            socket = new Socket(host, port);
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }

    protected void setupStream() throws IOException {
        out = new ObjectOutputStream(socket.getOutputStream());
        out.flush();
        in = new ObjectInputStream(socket.getInputStream());
    }

}

My Server Thread:

package Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class User extends Thread {
    public static int users = 0;
    public int ID;
    public String username;
    boolean online = false;

    public static ArrayList<String> usernames = new ArrayList<String>();

    Socket socket;

    DataOutputStream out;
    DataInputStream in;
    String input;

    public User(Socket socket) {
        this.socket = socket;

    }

    public String decode(String input) {
        String[] split = input.split(" ");

        if(input.startsWith("::")) {
            try {
                switch(split[0].substring(2, split[0].length()).toLowerCase()) {
                case "setname": 
                case "changename":
                case "newname":
                    if(usernames.contains(split[1].toLowerCase())) {
                        out.writeUTF("This name is already taken! Please choose a different one.");
                        out.flush();
                        return null;
                    }
                    if(username == null) {
                        username = split[1].substring(0, 1).toUpperCase() + split[1].substring(1, split[1].length());
                        Server.users.put(split[1].toLowerCase(), Server.user[ID]);
                        usernames.add(split[1].toLowerCase());
                    } else {
                        usernames.remove(username.toLowerCase());
                        username = split[1].substring(0, 1).toUpperCase() + split[1].substring(1, split[1].length());
                        usernames.add(split[1].toLowerCase());
                    }
                        return null;
                case "rank+":
                    return null;
                case "[sm]=":
                    return null;
                }
            }catch(IOException e) { }
        }
        return input;
    }

    String timeStamp;
    public void run() {
        try {

            out = new DataOutputStream(socket.getOutputStream());
            out.flush();
            in = new DataInputStream(socket.getInputStream());

            while((input = in.readUTF()) != null) {
                input = decode(input);

                if(input != null) {
                    if(username != null) {
                        timeStamp = new SimpleDateFormat("[h:mm:ss] ").format(Calendar.getInstance().getTime());
                        Server.sendGlobalMessage(timeStamp + username +": "+input);
                    } else {
                        timeStamp = new SimpleDateFormat("[h:mm:ss] ").format(Calendar.getInstance().getTime());
                        Server.sendGlobalMessage(timeStamp + "Guest "+ID+": "+input);
                    }
                }
            }

        }catch(IOException e) { e.printStackTrace(); } finally { 
            try{
                out.close();
                in.close();
                socket.close();
            } catch(IOException e) { e.printStackTrace(); }
        }

    }

}

I haven't touched the code of my Server Thread for a while, since it has always worked up until I made my new client.

user207421

I suspect that your server does not create an ObjectOutputStream, so when the client constructs its ObjectInputStream, it blocks waiting for the object stream header, which never arrives.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Program that exec commands won't continue

From Dev

Flask won't continue to execute after redirect

From Dev

Ubuntu won't boot after initializing the Build Environment for Android

From Dev

Ubuntu won't boot after initializing the Build Environment for Android

From Dev

Program Won't Continue to Run When Testing for Palindromes (C++)

From Dev

Program won't continue when I press enter

From Dev

If statement won't continue

From Dev

Scanf won't continue after I seized an input text

From Dev

Program won't run after a while, and I get this

From Dev

WPF program won't load after code change

From Dev

git rebase --continue won't work

From Java

Why won't this program print?

From Dev

Why won't the program exit?

From Dev

Program won't execute pthread

From Dev

Program won't work in FireFox

From Dev

Program won't execute function?

From Dev

Program with JFrame won't start

From Dev

Continue Execution even after program catches exception

From Dev

continue in js program after finishing ajax calls

From Dev

How to continue program after another window is opened

From Dev

continue in js program after finishing ajax calls

From Dev

Continue program execution after showAndWait javafx

From Dev

Standalone java program reading from ActiveMQ queue won't auto reconnect after broker is down

From Java

continue doesn't work when using streams and the map

From Dev

continue doesn't work when using streams and the map

From Dev

Won't continue executing statement on ACTION_DOWN

From Dev

Why won't my do-while loop continue looping?

From Dev

Why won't my do-while loop continue looping?

From Dev

Java program won't throw the desired exception

Related Related

HotTag

Archive