(Java)服务器不接受多个连接

时间:2016-12-07 01:24:35

标签: java server

我正在制作一个快速的java服务器,但我似乎只能在断开连接之前连接一次到服务器,有时输入填满了。有人可以帮忙吗?。

Main.java

package com.obwan02.server;

import java.io.IOException;

public class Main {

static boolean once = true;

public static void main(String[] args)
{


    Server server1 = new Server(6969, new IConnectionHandler(){

        @Override
        public void OnConnect(ConnectionHandler connection) {
        }

        @Override
        public void whileConnected(ConnectionHandler connection) {
            // TODO Auto-generated method stub

            try {
                if(once){
                connection.write("Hello");
                once = false;
                connection.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});

    server1.start();
}

}

Server.java

package com.obwan02.server;
import java.io.IOException;
import java.net.*;

public class Server implements Runnable{

    final int port;
    private Thread connectionListener;
    private ServerSocket server;
    private boolean running;
    public IConnectionHandler handler;

    @Override
    public void run() 
    {
        try {
            while(running)
            {
            Socket s = server.accept();
            System.out.println("Connected");
            Runnable run = new ConnectionHandler(s, handler);
            Thread thread = new Thread(run);
            thread.start();
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public void start()
    {
        connectionListener = new Thread(this);
        connectionListener.start();
        running = true;
    }

    public Server(int port, IConnectionHandler hand)
    {
        this.port  = port;
        this.handler = hand;
        try 
        {
            running = false;
            server = new ServerSocket(this.port);
        }
        catch (IOException e) 
         {
            System.out.println("Exception");
        }
    }
}

ConnectionHandler.java

package com.obwan02.server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ConnectionHandler implements Runnable{

    private class Read implements Runnable
    {

        public Read(ConnectionHandler conn) {
            super();
            this.conn = conn;
        }


        private ConnectionHandler conn;

        //Connection must end with ((char) 13) (carriage return)
        public String read() throws IOException
        {
            if(!conn.open)
            {
                conn.isText = false;
                return "";
            }

            StringBuffer input = new StringBuffer();

            int curChar = conn.in.read();

            int count = 1;

            while(curChar != 13)
            {
                input.append((char)curChar);
                curChar = conn.in.read();
                count++;
                if(count > 1000)
                    break;
            }

            conn.isText = true;
            return input.toString();
        }


        @Override
        public void run() 
        {
            System.out.println("reading");
            while(true)
            {
                try
                {
                    String text = read();
                    conn.currentReadingText = text;
                    isText = false;

                } catch (Exception e) {

                    e.printStackTrace();

                }
            }
        }

    }

    protected Socket socket;
    protected IConnectionHandler handler;

    private InputStream inStream;
    private OutputStream outStream;

    protected InputStreamReader in;
    protected OutputStreamWriter out;

    private BufferedInputStream bis;
    private BufferedOutputStream bos;

    private boolean open;
    public String currentReadingText;
    public boolean isText;

    private Thread readStream;

    public ConnectionHandler(Socket s, IConnectionHandler handler) throws IOException
    {
        this.socket = s;
        this.handler = handler;
        this.handler.OnConnect(this);

        inStream = socket.getInputStream();
        outStream = socket.getOutputStream();

        bis = new BufferedInputStream(inStream);
        in = new InputStreamReader(bis, "US-ASCII");

        bos = new BufferedOutputStream(outStream);
        out = new OutputStreamWriter(bos, "US-ASCII");

        open = true;
        isText = false;
    }

    public void write(String text) throws IOException
    {
        this.out.write(text);
        this.out.flush();
        System.out.println("Wrote: " + text);
    }

    @Override
    public void run() 
    {
        readStream = new Thread(new Read(this));
        readStream.start();
        while(!socket.isClosed())
        {
            this.handler.whileConnected(this);
        }
        open = false;
        System.out.println("Connection Closed!");
    }

    public void close() throws IOException
    {
        this.in.close();
        this.out.close();

        this.bis.close();
        this.bos.close();

        this.inStream.close();
        this.outStream.close();

        this.socket.close();
        open = false;
    }

}

IConnectionHanlder.java

package com.obwan02.server;

public interface IConnectionHandler 
{
    public void OnConnect(ConnectionHandler connection);
    public void whileConnected(ConnectionHandler connection);
}

1 个答案:

答案 0 :(得分:0)

running需要volatile,并且需要在启动线程之前设置。否则,run()方法只会看到running为false并立即退出。