I found an implementation variant of client-server interaction via UDP on java. Server:

import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Server { public static void main(String args[]) { try { //Создаем сокет DatagramSocket sock = new DatagramSocket(7000); //буфер для получения входящих данных byte[] buffer = new byte[65536]; DatagramPacket incoming = new DatagramPacket(buffer, buffer.length); System.out.println("Ожидаем данные..."); while(true) { //Получаем данные sock.receive(incoming); byte[] data = incoming.getData(); String s = new String(data, 0, incoming.getLength()); System.out.println("Сервер получил: " + s); //Отправляем данные клиенту DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort()); sock.send(dp); } } catch(IOException e) { System.err.println("IOException " + e); } } } 

Customer:

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Example2 { public static void main(String args[]) { DatagramSocket sock = null; BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); try { sock = new DatagramSocket(); while(true) { //Ожидаем ввод сообщения серверу System.out.println("Введите сообщение серверу: "); String s = (String)cin.readLine(); byte[] b = s.getBytes(); //Отправляем сообщение DatagramPacket dp = new DatagramPacket(b , b.length , InetAddress.getByName("localhost") , 7000); sock.send(dp); //буфер для получения входящих данных byte[] buffer = new byte[65536]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); //Получаем данные sock.receive(reply); byte[] data = reply.getData(); s = new String(data, 0, reply.getLength()); System.out.println("Сервер: " + reply.getAddress().getHostAddress() + ", порт: " + reply.getPort() + ", получил: " + s); } }catch(IOException e) { System.err.println("IOException " + e); } } } 

The problem is that the interaction goes through localhost and works within one device ... I have spent several days trying to figure out what to send there instead of localhost so that you can start the server on one device, And the client is completely different, connected to another network, tried to push local ip and ip, which were issued by various sites ... everything does not work ... please tell me UPD: having rummaged, I came across such a thing as NAT and that I do not connect directly to the Internet, so actually how can you get around this?

    2 answers 2

    decided this question and decided to share, will anyone push something on something ... as @Victor said my ip from outside was unavailable, so the router provided a single external ip for all connected devices, and my ip was internal, I will not especially to paint this moment, because I can not say something correctly (just google NAT). I solved the problem by connecting to the Internet not through a router, but directly by plugging the cable into the laptop, so I directly (!) connected to the Internet and my laptop’s ip was accessible from the outside, which allowed a person from another city to connect to my server =) You can find out by typing ipconfig on the command line (for Windows, for Unix, I don’t know) or by checking it on one of many sites. If they match, then you are directly connected to the Internet and you can safely specify this ip instead of localhost. If something is written is not correct, add in the comments.

    • one
      ifconfig for unix :) - Victor

    Well, actually, the connection will go according to your ip (local or external), there is no magic in it. Simply, instead of localhost, substitute the ip-address of your server (for example, 192.168.1.30) and enjoy life. another thing, if you do not have an external ip-address, for example, you have a local area network of the enterprise. In this case, getting to your ip from the outside will be very problematic, and there is reason to think about a separate server for this business.

    There are no problems in the code. When communicating within the local network problems should not be the same. If you need to configure your router \ server for access from outside, then alas, this is not java, but network settings.