Making VMs listen on an address?

I recently had a chance to play with Apache's "listen on address" function and I have to say thats very interesting. It seems very cool to be able to host several servers on the same IP and the same port and the same server, and I'm still a bit confused as to how it works so forgive me if this is completely unrealistic and not possible. Could you theoretically host several servers (not Apache, anything, game servers, database servers, mail servers, anything) on one port, with different domain names (I own a domain so I can make DNS records for it if need be) and have it work like Apache? That would be really cool if I could host several (for example) Minecraft servers on port 25565 with different domain names. Is there a networking tool out there to do that, or is it impossible?

Yes, you can do that.

If you are using virtual servers you have to set up NAT/PAT on those servers / pass-through on ports you want.

Under apache to allow many domains on same ip you need to make it listen on everything on port.

NameVirtualHost [ip]:[port] there might be more you'd have to look closer, and google it.

I don't think so. Listen on address is talking about the server's IP address. You'd use it if you had multiple interfaces and only wanted Apache to listen on one of them. You can use Apache to host different sites based on domain names, or redirect to another server or Port using the domain name, but I'm fairly sure this would only work for Web traffic. Look up mod_rewrite for Apache if you want to try that.

But yeah, pretty sure it's not possible to host multiple services on the same IP AND same port. Not to mention that some things won't work properly if you don't use the default ports, mail servers for example won't work properly because other mail servers won't know which port you're using and therefore won't be able to connect.

You are wrong, this can be done.

The header will decide where it will go. SMTP (mail) can be set to run at any port without any issues. Your machine will listen on ports it has specified to listen at; then you specify the servername which will decide where it actually goes. [servername is the header url it has to have specified root destination]

 

Yes, I know you can change the ports used by SMTP, but if you do how will other mail servers be able to connect to you? They have no way of knowing what port you're using. The domain name will only point you to the machine, if you're not using a standard port then you have to specify it so if an email gets sent to your from Gmail for example then Gmail won't be able to connect to your mail server because it doesn't know what port to connect to.

You can by adding it inside of your client.

Most of private mail servers do not use mx records and are not limited to std ports; even though this can be bypassed and still used with custom ports.

Forget about domain names for a second. They are just user-friendly ways to render IPs and have nothing to do with TCP/IP under the hood.

Short answer: You can have multiple services on the same port so long as they are the same application. So like you can run multiple web servers so long as the web server you're implementing can interpret all the communications sent to it correctly and forward each connection to the correct server. This is why one web server can talk to multiple clients. But like, you can't have 2 different applications (smtp, snmp) listening on the same port and expect everything to work correctly. 

This is because prior to getting sent to an application, an OS has to determine which application a particular piece of information belongs to and ports are how that is organized. If there are 2 or more applications listening on the same port, which application does the OS hand a piece of data to? How can the OS tell which one it's destined for? There's ambiguity there and computers don't handle ambiguity well. It tends to cause app crashes or un-responsive apps, or one application getting all the traffic and the other one getting a "socket in use" error.

I don't see how it's possible to run multiple applications on the same socket unless either by Voo-doo magic or if you had a "host" application listen at that port and separate the traffic it receives and send each traffic type to the appropriate application (same as above where one application knows internally what to do with different traffic).

That's the short answer, the long answer is very very long. Let me know if you want a wall of text or three o.<

 

Yes, you can change your client configuration and connect to it on whatever port you want, but that has nothing to do with receiving emails. If you want a mail server that can receive mail from other public mail servers then you will need to use standard ports. This is my point. Some things will not work properly if you don't use the standard ports. Besides, what he is asking is can you run everything on one port? the answer is no.