单客户端到多客户端支持:java中的多线程转换

时间:2014-03-30 14:06:04

标签: java multithreading client-server client

所以我不知道如何创建多线程服务器。我有客户端和服务器一起工作,但不能正确引入多个客户端。这是我的服务器代码:

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);
    }

}

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

2 个答案:

答案 0 :(得分:1)

首先,在服务器中添加新的Thread(clientThread)并在其上调用start() - 就像在同一个线程上发生的一切一样。

答案 1 :(得分: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方法来创建多线程应用程序。

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

我希望它有所帮助。