我编写了一个创建线程并打开套接字的服务器。客户端是用Flash编写的。一段时间后服务器不再响应客户端。
public appGameServer(String Ip){
try{
_SS = new ServerSocket(appMain.CONFIG_GAME_PORT);
_SS.setSoTimeout(0);
_startTime = System.currentTimeMillis();
_gameServerThread = new Thread(this);
_gameServerThread.start();
try{
_startFunctions();
}
catch (Exception e) {
e.printStackTrace();
Debug.err.println(e.getMessage());
}
} catch (IOException e) {
Debug.err.println("Could not listen on port " + appMain.CONFIG_GAME_PORT);
e.printStackTrace();
System.exit(0);
}
}
public void _startFunctions(){
actiuniGlobale.schedule(new TimerTask(){
public void run(){
}
}, 0,500);
}
@Override
public void run(){
while(appMain.isRunning) {
try {
_clients.add(new appMainSystem(_SS.accept()));
_jucatoriOnline = _clients.size();
}catch(IOException e){
e.printStackTrace();
Debug.err.println(e.getMessage());
}
}
}
public static void delClient(appMainSystem serverPrincipal) {
try{
synchronized (_clients){
_clients.remove(serverPrincipal);
_jucatoriOnline = _clients.size();
}
}catch(Exception e){
e.printStackTrace();
Debug.err.println(e.getMessage());
}
}
public static void kickAll()
{
try {
_SS.close();
} catch (IOException e) {}
List<appMainSystem> c = Collections.synchronizedList(new ArrayList<appMainSystem>());
c.addAll(_clients);
for(appMainSystem GT : c) {
try {
GT.closeSocket();
} catch(Exception e) {
e.printStackTrace();
Debug.err.println(e.getMessage());};
}
}
包处理程序:
public appMainSystem(Socket sock) {
try{
_s = sock;
_s.setSoTimeout(0);
_in = new BufferedReader(new InputStreamReader(_s.getInputStream()));
_out = new PrintWriter(_s.getOutputStream(),true);
try{
_t = new Thread(this);
_t.setDaemon(true);
_t.start();
}
catch (Exception e) {
Debug.err.println(e.getMessage());
e.printStackTrace();
}
}
catch (IOException e) {
Debug.err.println(e.getMessage());
e.printStackTrace();
if(!_s.isClosed()){
try{
_s.close();
} catch (IOException e1) {Debug.err.println(e1.getMessage());e1.printStackTrace();}
}
}
}
public void run(){
try{
String packet = "";
char charCur[] = new char[1];
while(_in.read(charCur)!=-1 && appMain.isRunning) {
if (charCur[0] != '\u0000' && charCur[0] != '\n' && charCur[0] != '\r') {
packet += charCur[0];
} else if(!packet.isEmpty()) {
packet = new String(packet.getBytes(),"UTF8");
try {
verificaPacket(packet);
} catch(Exception e) {
Debug.err.println(e.getMessage());
e.printStackTrace();
}
packet = "";
}
}
}catch(Exception e) {
kick();
}
finally
{
if(!lastPacketRecv.isEmpty()) kick();
}
}
public void kick()
{
try{
appGameServer.delClient(this);
closeSockets();
} catch(Exception e){
Debug.err.println(e.getMessage());
e.printStackTrace();
}
}
public void closeSockets() throws IOException {
_in.close();
_out.close();
if(!_s.isClosed()) closeSocket();
_t.interrupt();
}
public void closeSocket()
{
try {
_s.close();
} catch (IOException e) {
Debug.err.println(e.getMessage());
e.printStackTrace();
}
}
答案 0 :(得分:1)
我无法从您的代码中判断出此建议是否适用,但是从套接字读取或写入的线程应该没有其他内容。读线程应该对它们的数据进行排队,并且应该从数据队列中提供写线程。阅读和写作应该在不同的线程中完成。
否则,缓冲和实际数据传输的不可预测性可确保发生意外的停顿条件。