Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:51:35+00:00 2026-06-15T16:51:35+00:00

i want to make a game with 2 players .i will use udp server

  • 0

i want to make a game with 2 players .i will use udp server and 2 clients and i dont
know how to connect 2 clients to 1 server at the same time and how they will communicate.
I will use only java.At last how the mouse click will syncronize with server

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

the server

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:51:37+00:00Added an answer on June 15, 2026 at 4:51 pm

    So now you can see in your code that you have a point where the server socket is waiting for a connection. That is the accept() method. At that point, you need to create a new Thread for handling the connection and let the main thread continue waiting for another connection. You may want to keep track of the number of connections made in a variable, say numConnections.

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

    Now you ConnectionHandler class will do the work of the connection:

     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.
            }
     }
    

    A quick Google search turns up many tutorials and examples of doing this such as this one. There are also some YouTude videos. You may want to start with one of those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a game that will be similar to Pac-Man from scratch.
I am trying to make a client/server turn based game. I want this to
I want to make a simple game: 2d, single-player, without tons of animations and
I want to make the XNA game window be in windowed mode but always
I want to make a simple game so that I can play against my
I want to make an indie game for xbox360. Even if it is freeware
I want to make an Android Game App that loads a game slot that
I want to make a multiplayer game that runs on the Internet, not just
So here's the situation. I want to make a simple game for android. It's
I want to do projects to make my resume more appealing to game companies.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.