2014-03-30 113 views
1

所以我不知道谁去创建一个多线程服务器。我的客户端和服务器一起工作良好,但无法正确引入多个客户端。这里是我的服务器代码:单客户端到多客户端支持:在java中的多线程转换

package dod; 
import java.net.*; 
import java.io.*; 

import dod.game.GameLogic; 

public class Server{ 
    Server(GameLogic game, int port) throws IOException{ 
     ServerSocket ss = null; 
     Socket sock = null; 
     try{ 
      ss = new ServerSocket(4444);//port no. 
      while(true){ 
       try{ 
        sock = ss.accept();  
        ClientThread thread = new ClientThread(game, sock); 
        System.out.println("Adding new player..."); 
        thread.run(); 
       }catch(final Exception e) { 
        System.err.println(e.getMessage()); 
        System.exit(1); 
       } 
      } 
     }catch(Exception d){ 
      System.out.println(d); 
     }finally{ 
      if(ss!=null){ 
       ss.close(); 
      } 
     } 
    } 
} 

这里是我的线程类:

package dod; 

import java.io.*; 
import java.net.Socket; 
import dod.game.GameLogic; 
import dod.game.PlayerListener; 

public class ClientThread extends CommandLineUser implements PlayerListener, Runnable{ 
    DataInputStream in; 
    PrintStream out; 

    // The game which the command line user will operate on. 
    // This is private to enforce the use of "processCommand". 
    ClientThread(GameLogic game, Socket sock) { 
     super(game); 
     try{ 
      in = new DataInputStream(sock.getInputStream()); 
      out = new PrintStream(sock.getOutputStream()); 
     }catch(IOException ioe){ 
      System.out.println(ioe); 
     } 
     game.addPlayer(this); 
    } 

    /** 
    * Constantly asks the user for new commands 
    */ 
    public void run() { 
     System.out.println("Added new human player."); 
     // Keep listening forever 
     while(true){ 
      try{ 
       // Try to grab a command from the command line 
       final String command = in.readLine();; 
       // Test for EOF (ctrl-D) 
       if(command == null){ 
        System.exit(0); 
       } 
       processCommand(command); 

      }catch(final RuntimeException e){ 
       System.err.println(e.toString()); 
       System.exit(1); 
      } catch (final IOException e) { 
       System.err.println(e.toString()); 
       System.exit(1); 
      } 
     } 
    } 

    /** 
    * Outputs a message to the player 
    * 
    * @param message 
    *   the message to send to the player. 
    */ 
    public void outputMessage(String message) { 
     out.print(message); 
    } 

} 

不要求新的代码,例如,只需要指针,以什么我需要做的有在多个客户端连接同时!感谢任何有帮助的人!

+0

您是否在向其他客户端发送有关新客户端连接的消息? –

+0

'DataInputStream.readLine()'是一个不推荐的方法。不要使用它。 – Braj

+0

你必须阅读,如何开始一个线程?您正在直接调用run方法。调用start(),它将在内部调用run()。 – Braj

回答

1

要开始,请在服务器中添加新线程(clientThread),并在其上调用start() - 因为所有事情都发生在同一线程上。

0
public class Server{ 
    Server(GameLogic game, int port) throws IOException{ 
     ServerSocket ss = null; 
     Socket sock = null; 
     try{ 
      ss = new ServerSocket(4444);//port no. 
      while(true){ 
       try{ 
        sock = ss.accept();  
        ClientThread thread = new ClientThread(game, sock); 
        System.out.println("Adding new player..."); 
        thread.start(); //you have to use start instead of run method to create multi thread application. 
       }catch(final Exception e) { 
        System.err.println(e.getMessage()); 
        System.exit(1); 
       } 
      } 
     }catch(Exception d){ 
      System.out.println(d); 
     }finally{ 
      if(ss!=null){ 
       ss.close(); 
      } 
     } 
    } 
} 

您必须使用start而不是run方法来创建多线程应用程序。

如果您想发送有关新连接的消息,则必须在列表中保存sock,并在新连接接受时将消息发送到列表中的所有套接字对象。 (服务器广播到所有连接的插座)

我希望它有帮助。

+0

ClientThread不是线程类。 – Braj

+0

你说得对。 CommandLineUser可以从Thread扩展?不是一个明确的例子,我认为 –

+0

好吧我有一个列表,每次套接字是接受,套接字被添加到数组列表。我如何确保来自进程命令的正确响应被发回? –