简体   繁体   中英

Java Multi Client server socket

I am trying to establish a communication between a server (NewServer class) and a client (NewClient class), accepting two Client communications. I know how to do it with one client but not multiple client connections.

  • Do I have to create 2 readers in the Client class?

  • Can I do this in a recursive way?

Server Class:

import java.net.*;
import java.io.*;

public class NewServer
{
    //Create Server Socket and a Client Socket for each Client.
    static ServerSocket server;
    static Socket client1;
    static Socket client2;

    //Create OutputStreams to send information to each Client.
    static DataOutputStream client1writer;
    static DataOutputStream client2writer;

    static final int PORT = 9999;

    //Main Method
    public static void main(String[]args)
    {
        //Try-Catch Block for Socket Errors.
        try
        {
            //Create the Server Socket as a Host.
            server = new ServerSocket(PORT);

            //Connect Client 1 – First run of the Client class.
            client1 = server.accept();
            client1writer = new    
            DataOutputStream(client1.getOutputStream());

            //Connect Client 2 – Second run of the Client class.
            client2 = server.accept();
            client2writer = new     
            DataOutputStream(client2.getOutputStream());            

            //Assign each Client an ID number – this is how the Client will know
            //    which individual Client it’s representing in RunTime.
            int ID1 = 8675309;
            int ID2 = 8675308;

            //Tell both Clients which one they are representing.
            client1writer.writeInt(ID1);
            client2writer.writeInt(ID2);

            //Close all Sockets when finished.
            client1.close();
            client2.close();
            server.close();
        }
        catch (IOException IOex)
        {
            System.out.println("Server Error.");
        }
    }
}

Client class:

import java.net.*;
import java.io.*;

public class NewClient {

    static Socket client = null;                          
    static final int PORT = 9999;                      
    static final String IP = "localhost";                

    public static void main(String[] args) throws IOException {

        int id1;      
        int id2;                      

        try{
            client = new Socket(IP,PORT);      
            System.out.println("Connection successful!");

            reader = new DataInputStream(client.getInputStream());

            id1 = reader.readInt();
            id2 = reader.readInt();       

            System.out.println("The id of the user is " + id);

            //Closing everything
            client.close();
            reader.close();

        }catch(IOException error) {
            System.err.println("Server error.");
        }
    }
}

You can achieve it by starting a separate thread (also known as Plain threads for every new client connection and then call server.accept() which runs in a loop. It is useful as an learning example but will not help you achieve what you need as you loose control of threads.

Second way is to use Executor Service which provides better management control over the above solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM