简体   繁体   中英

Multiple Servers on the same ip

I want to know if it is possible to have multiple sockets for servers on the same ip and different ports, and if it is not possible why.

For Example:

public static void main (String[]args){
    int nrServers = 2;

    for(int i=0;i<nrServers;i++){
            new Socket("127.0.0.1",5000+i);
    }
}

Thanks.

If by "server" you mean "server process" (or even just a single process listening on several ports), yes, that's not just possible, it's common. Nearly all computers (frequently even ones that aren't "servers") do it. Your code is a start toward doing it (you might want to use ServerSocket rather than Socket , and of course you want to actually do something with them, but...).

For example, a dedicated server in a data center might well be listening on port 22 (SSH), port 80 (HTTP), port 443 (HTTPS), and possibly others.

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