1台服务器2客户端java

时间:2012-12-10 15:58:15

标签: java multithreading webserver udp client

我想与2名玩家制作游戏。我将使用udp服务器和2个客户端,我不会 知道如何将2个客户端同时连接到1个服务器以及它们如何通信。 我将只使用java.At last鼠标单击将如何与服务器同步

  public void mousePressed(MouseEvent event) { 
  }  
  public void mouseReleased(MouseEvent event) { 
  }  
  public void mouseEntered(MouseEvent event) { 
  }  
  public void mouseExited(MouseEvent event) { 
  } 

服务器

public class Provider {
public ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
String[] torino={"null","null"};
Provider() {}
void run()
{
    try {
        //1. creating a server socket (8080=port , 2 =number of connections)
        providerSocket = new ServerSocket(8080, 2 );
        //2. Wait for connection
        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + connection.getInetAddress().getHostName());
        //3. get Input and Output streams
        out = new ObjectOutputStream(connection.getOutputStream());
        // flush= clean the object out
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
        sendMessage("Connection successful");
        //4. The two parts communicate via the input and output streams
        try {
            //take the message from client
            message = (String)in.readObject();
            if (torino[0]=="null") 
                torino[0]=message;
            } else if (torino[1]=="null") {
                torino[1]=message;              
            }
        }
        catch(ClassNotFoundException classnot) {
            System.err.println("Data received in unknown format");
        }
    } catch(IOException ioException) {
        ioException.printStackTrace();
    }
    finally {
        //4: Closing connection
        try {
            in.close();
            out.close();
            providerSocket.close();
        } catch(IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
//method to send messages from server to clients
void sendMessage(String msg)
{
    try {
        out.writeObject(msg);
        out.flush();
        System.out.println("server>" + msg);
    }
    catch(IOException ioException) {
        ioException.printStackTrace();
    }
}

//main method
public static void main(String args[])
{
    Provider server = new Provider();
    while(true) {
        server.run();
    }
}

1 个答案:

答案 0 :(得分:0)

现在,您可以在代码中看到服务器套接字正在等待连接。这是accept()方法。此时,您需要创建一个用于处理连接的新线程,并让主线程继续等待另一个连接。您可能希望跟踪变量中的连接数,例如numConnections。

 while(numConnections < 2){
       connection = providerSocket.accept();
       Thread t = new Thread(new ConnectionHandler(connection));
       t.start();
       numConnections++;
 }

现在ConnectionHandler类将完成连接的工作:

 class ConnectionHandler implements Runnable{
        Socket connection;
        public ConnectionHandler(Socket connection){
             this.connection = connection;
        }

        public void run(){
           //here you do all the code associated with handling the connection
           //such as your Object Streams and so on.
        }
 }

快速的Google搜索会出现许多教程和示例,例如one。还有一些YouTude视频。你可能想从其中一个开始。