初始化输入/输出流后程序不会继续?

时间:2013-10-04 12:02:50

标签: java sockets data-structures stream

在人们怀疑我根本不知道我在做什么之前(并且完全无缘无故地投票),请阅读:

它连接到我的服务器就好了!我没有收到任何错误(来自客户端或服务器),我的服务器正在识别连接。它与我朋友的客户合作,但我想创建自己的客户,显然我做错了。请留在主题!谢谢:))


标题基本上都说明了一切。我已在我的Client.java println中的setupStream()上方和下方使用run()条消息进行了测试,但只显示了setupStream()上方的消息。我不确定如何在不让我的程序停止的情况下初始化我的流。

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());
    }

}

我的服务器线程:

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(); }
        }

    }

}

我暂时没有触及服务器线程的代码,因为它一直在我创建新客户端之前一直工作。

1 个答案:

答案 0 :(得分:2)

我怀疑你的服务器没有创建一个ObjectOutputStream,所以当客户端构造它的ObjectInputStream时,它会阻塞等待永远不会到达的对象流头。