So I am currently working on a client application that listens on port 5004 for RTP packets. Since there may be multiple servers sending RTP packets, I can’t use sockets to connect to a specific remote host. Instead, I have tried the following to listen on a local port:
Socket socket = new Socket("127.0.0.1", 5004);
Socket socket = new Socket("localhost", 5004);
Socket socket = new Socket(InetAddress.getByName("localhost"), 5004);
Socket socket = new Socket(InetAddress.getLocalHost(), 5004);
Any of the above will give me this exception:
java.net.ConnectException: Connection refused: connect
I have also tried using a DatagramSocket, but DatagramPackets require that I specify the size of the packet to be read.
To summarize, I need to find a way to listen on local port 5004 for RTP packets of unknown size without connecting to a specific remote host/address. Any help is greatly appreciated!
EDIT:
I now have a ServerSocket set up to listen for connections, but I still can’t manage to read in any packets.
try { ServerSocket server = new ServerSocket(5004);
Socket s = server.accept();
BufferedReader rtpReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true){
int k = rtpReader.read();
if (k == -1) break;
System.out.println(k);
}
}
Note: The RTP packets are sent over a Multicast address.
The problem turned out to be the Multicast. Refer to Nikolai’s answer. Thanks alot!
You have to use
java.net.MulticastSocket, and join the multicast group. Something like: