Make a program for exchanging packages with the client and server using DatagramSocket . Everything works on my computer, and if I launch the client from another computer and the server on my computer, it does not work. There are no problems with the network (the web server works between computers). The firewall has been completely disabled.

Client code:

 public class Client { public static DatagramSocket socket; public static void main(String... args) { try { socket = new DatagramSocket(); InetAddress ip = InetAddress.getByName("localhost"); byte[] data = ("Hello, World!").getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, ip, 3000); socket.send(dp); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 

Server code:

 public class Server { public static DatagramSocket socket; public static void main(String... args) { try { socket = new DatagramSocket(3000); DatagramPacket dp = new DatagramPacket(new byte[1024], 1024); socket.receive(dp); System.out.println(new String(dp.getData())); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 
  • one
    Show your code. - Sergey Gornostaev
  • Is it written on the client side that your computer is a server? - Sergey
  • @SergeyVlasov Yes - MaximPixel
  • one
    Can I see the code? - Sergey
  • @SergeyVlasov added the code - MaximPixel

1 answer 1

Register in class Client

 socket.connect(ip, port);