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?