0
我正在做一個快速的java服務器,但我似乎只能連接一次到服務器之前,它斷開連接,有時輸入塞滿了。有人可以幫忙嗎?(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);
}
不,我在主要方法中使用它來啓動服務器並啓動線程,該線程具有運行方法 –