Comunicación vía internet en Java

What I mean is like servers on video games. You can run an application and it will set up a server on your computer with an IP and a port.

For example, how would you make an application where one host application sets up a thing where it has an IP and a port, and another computer that has access to the internet as well can type in the IP and port and it would be able to communicate with the host? I mean simple communication, like sending a boolean or String.

And would there be any security problems that would be needed to fix?

preguntado el 09 de enero de 11 a las 02:01

4 Respuestas

I guess I grasp the concept of your question... You want two computers to connect via internet right? If that is the case, then you will have to use a thing called "sockets" that do connections between computers. About the server thing, well, for starters the client must always know what IP the server as (direct IP or by a DNS), and then you can connect your client to your server. There is a tutorial for sockets at the java pages: . About security issues, well, you must make sure that your server can handle anything that comes from the client (i really mean everything), i mean, accepting every type of data that is supposed to receive and deny everything that is not (trash per say). If you have that in mind then there is no problem (and of course, the server must have a firewall also to control the sockets, but that's not up to you).

Respondido el 09 de enero de 11 a las 05:01

In general, the thing you probably want is a Socket. Sockets allow you to send bytes to an endpoint via TCP or UDP. This is very low-level, though, and are somewhat tricky because you have to design your own application protocol. You may want to use something that offers more abstraction.

Java sockets expose a stream interface so you could just encode integers as strings, for instance, and send them line by line, or you could do something fancier and more efficient like using a DataOutputStream to wrap it.

Respondido el 09 de enero de 11 a las 05:01

Handling the following issues can improve security.

  1. If you have router ,set different ports for routing.

    Example: If you are running server say on port 6001, map a virtual port say 9001 , which would be exposed to public.

  2. DDos

  3. IP Restriction - Not every user can access your machine !

  4. Enabling router firewall does handle most of the issues.

Respondido el 20 de junio de 20 a las 12:06

Here is an example of how to use sockets to send a string from a server to a client.

The site has about 20 examples of how to do what you are trying to do. In general I find this site to be the best JAVA resource that I know.

Respondido el 09 de enero de 11 a las 07:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.