Im have Android Studio-server and client in Intelij Idea.
The Server in Android Studio:
new Thread(new Runnable() {
@Override
public void run() {
try {
DatagramSocket serverSocket = new DatagramSocket(5556);
byte[] receivingDataBuffer = new byte[1024];
byte[] sendingDataBuffer = new byte[1024];
DatagramPacket inputPacket = new DatagramPacket(receivingDataBuffer, receivingDataBuffer.length);
System.out.println("Waiting for a client to connect...");
serverSocket.receive(inputPacket);
System.out.println(inputPacket.toString());
} catch (SocketException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
Client
try {
String message = "Test";
DatagramSocket serverSocket = new DatagramSocket(5556);
InetAddress IPAddress = InetAddress.getByName("192.168.1.67");
DatagramPacket outputPacket = new DatagramPacket(
message.getBytes(), message.getBytes().length, IPAddress, 5556);
serverSocket.send(outputPacket);
} catch (SocketException e) {
throw new RuntimeException(e);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
This code server is not accepting requests receive, he can send datagram and connecting to TCP server, but he cant accepting datagram and .accept() connect. This code works in Intelij idea, i tried create server and client in Intelij Idea and all worked. After i undestand that my server in Andoid Studio has a different port, somewhere around 65000+ (which changes with a restart), and i want to know how i am can to send request to my fixed port 5556?
New contributor