没有获得多线程数据流的多个实例

时间:2017-02-02 15:14:22

标签: java multithreading sockets object stream

我正在使用一个数据生成器,它使用端口来传输数据。我使用多个端口来接收数据。我正在编写的软件为每个端口创建一个新的套接字。每个套接字都应该创建一个名为' Interpreter'的类的实例。

我遇到的问题如下:我能够创建多个Interpreter实例,但我认为数据只被解析为其中一个实例。我认为这是因为数据被合并了#39;在输出中。

这是代码中最重要的内容:

public class Main {

    public static void main (String[] args) {
        Socket socket;
        ServerSocket serverSocket=null;
        System.out.println("Server Listening..");
        try{
            serverSocket = new ServerSocket(7789);
        }
        catch(IOException e){
            e.printStackTrace();
            System.out.println("Error");
        }

        while(true){
            try{
                socket= serverSocket.accept();
                Interpreter interp = new Interpreter();
                ServerThread serverThr=new ServerThread(socket, interp);
                serverThr.start();
            }
            catch(Exception e){
                e.printStackTrace();
                System.out.println("Connection Error");
            }
        }
    }
}
class ServerThread extends Thread {
    String line = null;
    BufferedReader is = null;
    PrintWriter os = null;
    Socket s = null;

    public ServerThread(Socket s, Interpreter interp) {
        this.interp = interp;
        this.s = s;
        System.out.println("interp "+interp);
        System.out.println("s: "+s);
        System.out.println("poort: "+s.getPort());
    }
    public void run() {
        try {
            is = new BufferedReader(new InputStreamReader(s.getInputStream()));
            os = new PrintWriter(s.getOutputStream());
        } catch (IOException e) {
            System.out.println("IO error in server thread");
        }

        try {
            line = is.readLine();
            while (line.compareTo("QUIT") != 0) {
                os.println(line);
                os.flush();
                Interpreter.interpreter(line);
                line = is.readLine();
            }
        } catch (IOException e) {
            line = this.getName();
            System.out.println("IO Error/ Client " + line + " terminated abruptly");
        } catch (NullPointerException e) {
            line = this.getName();
            System.out.println("Client " + line + " Closed");
        } finally {
            try {
                System.out.println("Connection Closing..");
                if (is != null) {
                    is.close();
                    System.out.println(" Socket Input Stream Closed");
                }
                if (os != null) {
                    os.close();
                    System.out.println("Socket Out Closed");
                }
                if (s != null) {
                    s.close();
                    System.out.println("Socket Closed");
                }
            } catch (IOException ie) {
                System.out.println("Socket Close Error");
            }
        }
    }
}

这是重要的'口译员的一部分:

public class Interpreter{
    static AtomicInteger nextId = new AtomicInteger();
    int id = nextId.incrementAndGet();

    public Interpreter(){
        System.out.println("ID of demo "+id);
    }
}

0 个答案:

没有答案