I'm having some problems receiving a String from a multicast.
It's the first time i'm using UDP multicast in Java.
I'm making a multiclient application over a LAN.
So i'm using Local Ip addresses.
I need the clients to find the server's IP address so they can send their data, requests, etc.
I let the server sent out a multicast with his own IP as a string every 5 seconds.
The clients should be able to receive it.
The problem is that they don't receive anything.
I'm testing with 2 devices so i don't need to use localhost.
Here's some of my code:
Server side:
public class MulticastIpSender extends Thread{
private String serverIp;
private int port;
private String multicastAddress;
private long WAITING_TIME = 5000; // 5 seconden
private DatagramSocket socket;
public MulticastIpSender(String serverIp, int port, String multicastAddress) throws SocketException {
super();
this.serverIp = serverIp;
this.port = port;
this.multicastAddress = multicastAddress;
socket = new DatagramSocket(port);
}
public void run() {
while(true){
try {
byte[] buf = new byte[256];
buf = serverIp.getBytes();
InetAddress group = InetAddress.getByName(multicastAddress);
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
socket.send(packet);
System.out.println("sent IP("+serverIp+") to group("+group+") on port "+port);
sleep(WAITING_TIME);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
I put this method in the main class:
private void sendIpAddressToListeners() {
try {
multicastIpSender = new MulticastIpSender(serverIp,PORT,"230.0.0.1");
multicastIpSender.run();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I get the serverIp like this:
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress addrs[] = InetAddress.getAllByName(hostName);
if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
myIp = addr.getHostAddress();
}
}
System.out.println ("\nIP = " + myIp);
return myIp;
on this device it gives me 192.168.1.2
CLIENT SIDE:
multicastSocket = new MulticastSocket(PORT);
InetAddress address = InetAddress.getByName(MULTICAST_ADDRESS);
multicastSocket.joinGroup(address);
DatagramPacket serverIpPacket;
byte[] buf = new byte[256];
serverIpPacket = new DatagramPacket(buf, buf.length);
while(receivedIp ==null){
multicastSocket.receive(serverIpPacket);
receivedIp = new String(serverIpPacket.getData(), 0, serverIpPacket.getLength());
System.out.println("received server ip: " + receivedIp);
}
!! PORT = 4445 in both server and client
I hope somebody can help me with this or can explain a better way to do this.
Send a normal DatagramPacket on broadcast address, the packet will be received by all hosts in local network (with same network configuration, important part is mask)
Use calculator to check your broadcast address, this one works good: http://www.subnet-calculator.com/
Related
I have a Multicast socket open and is receiving Multicast message. From this thread, it seems that the same multicast socket should also be able to receive unicast messages. However, I'm not able to get anything.
Edit: the port number seems the be problem. Port 3702 is used by ws-discovery for unicasting which is related to what I'm trying to do. I'm tracking down a problem where the client's probe to the service is not caught by the service's multicast socket. I'm running this on windows.
My multicast server:
class Server extends Thread {
MulticastSocket multicastSocket;
final Logger LOG;
final int PORT = 3702;
final String MULTICAST_ADDR = "239.255.255.250";
InetAddress multicastGroup;
public Server() {
LOG = Logger.getLogger("Server");
try {
multicastGroup = InetAddress.getByName(MULTICAST_ADDR);
multicastSocket = new MulticastSocket(PORT);
multicastSocket.setInterface(InetAddress.getLocalHost());
multicastSocket.joinGroup(multicastGroup);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (!Global.exit) {
byte[] buf = new byte[1000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
try {
multicastSocket.receive(recv);
String msg = new String(recv.getData(), StandardCharsets.UTF_8);
LOG.log(Level.INFO, "got: " + msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
And the client code:
public void directMsgTest(){
try {
DatagramSocket datagramSocket = new DatagramSocket( 8080,InetAddress.getLocalHost());
String msg = "direct msg";
byte[] buf = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), DST_PORT);
datagramSocket.send(packet);
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
It seems that this is Window's fault. It uses WS discovery in some of its services, thus using port 3702 and eating unicast packets send to port 3702 instead of giving it to my server.
I tried running this on Linux and it was fine.
Only local client can connect only to server.
Client side code:
public InetAddress ipAddress;
private DatagramSocket socket;
public Client()
{
try
{
this.socket = new DatagramSocket();
this.ipAddress = getIpAddress();
}
catch (SocketException e)
{
e.printStackTrace();
}
}
public void run()
{
while(true)
{
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1331);
try
{
socket.send(packet);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private InetAddress getIpAddress()
{
String ipAddress;
try
{
URL url = new URL("http://checkip.amazonaws.com/");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
ipAddress = br.readLine();
} catch (IOException e)
{
e.printStackTrace();
ipAddress = "null";
}
try
{
return InetAddress.getByName(ipAddress);
}
catch (UnknownHostException e)
{
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{
Client client = new Client();
client.start();
}
Server side code:
private DatagramSocket socket;
public Server()
{
try
{
this.socket = new DatagramSocket(null);
socket.bind(new InetSocketAddress(getIpAddress(), 1331));
System.out.println(socket.getInetAddress());
}
catch (SocketException e)
{
e.printStackTrace();
}
}
public void run()
{
while(true)
{
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try
{
socket.receive(packet);
System.out.println("aa");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private InetAddress getIpAddress()
{
try
{
return InetAddress.getByName("0.0.0.0");
}
catch (UnknownHostException e)
{
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{
Server server = new Server();
server.start();
}
I tried to off my firewall, off Windows Defender, add NAT to router.
Also i checked if port is foreign port in netstat.
The only way to work fine server is to change ipAddress of client to "localhost"
Then code is working fine.
Is there and method how can i add address to server and players can connect to server with only ip address?
In order to connect the server multiple other computers, you should set the clients IP address to the address of the server's computer.
How to know what's the IP address of the server computer?
In the computer where the server installed:
Windows: go to CMD and then type ipconfig you need the IPv4 address.
Mac: go to terminal and then type ifconfig you need inet address.
I recently started making a 2D java game, and started making a tcp server 2 days ago, now that all my other issues are fixed, I would like to address the fact that the client can only connect to the server when it is on the same device and using the devices static ip, though it cannot connect using my wan ip address, I have port forwarded and I have checked on many port-checker websites and they all can see my server, my firewall is also disabled, yet the client can't see the server using the wan ip, I made a mine craft server on the same port (Minecraft servers are also tcp) and others were able to connect using my wan ip. When a client does try to connect using my wan ip, it gives a Connection Refused exception. What am i doing wrong?
Client (Can't connect to server over nat):
package com.diedericksclan.main.network;
import java.io.*;
import java.net.*;
import java.util.Enumeration;
public class ClientThread extends Thread {
private ClientHandler client;
private Socket socket;
private InetSocketAddress address;
private int megabyte = 1024 * 1024;
private DataInputStream in;
private DataOutputStream out;
public ClientThread(ClientHandler client, InetSocketAddress address) {
this.client = client;
this.address = address;
socket = new Socket();
try {
socket.setSendBufferSize(megabyte);
socket.setSendBufferSize(megabyte);
socket.setTcpNoDelay(true);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(this.getIP(), 0));
System.out.println(socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + " is a new client!");
socket.connect(address);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private InetAddress getIP() throws SocketException {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback() && ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress().getAddress().length == 4) {
return ia.getAddress();
}
}
}
}
return null;
}
long starttime;
long endtime;
long overall;
public void run() {
byte[] data;
while(true) {
try {
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
data = new byte[in.readInt() - 4];
in.read(data);
client.parsePacket(data, socket.getInetAddress(), socket.getPort());
endtime = System.nanoTime();
overall = endtime - starttime;
//System.out.println("CLIENT >> SERVER >> CLIENT - Time was: " + overall + " nano seconds!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public synchronized void sendData(byte[] data) {
try {
try { socket.connect(address); } catch (IOException e) {}
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
starttime = System.nanoTime();
out.writeInt(data.length + 4);
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void serverShutdown() {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server (Can be seen by everything but client):
package com.diedericksclan.main.network;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import com.diedericksclan.main.network.handling.PlayerMP;
public class ServerThread extends Thread {
private ServerHandler server;
private ServerSocket dataSocket;
private Socket socket;
private InetSocketAddress address;
private int megabyte = 1024 * 1024;
private int dedicated = 1024;
public int RAM = megabyte * dedicated;
private ArrayList<Client> clients = new ArrayList<Client>();
public ServerThread(ServerHandler server, String serverIP, int ram, int backlog) throws Exception {
super(serverIP);
this.server = server;
this.dedicated = ram;
String ip = "localhost";
int port = 2048;
if(serverIP.contains(":")) {
ip = serverIP.split(":")[0];
port = Integer.parseInt(serverIP.split(":")[1]);
} else {
ip = serverIP;
port = 2048;
}
this.dataSocket = new ServerSocket();
this.dataSocket.setReuseAddress(true);
this.address = new InetSocketAddress(InetAddress.getByName(ip), port);
this.dataSocket.bind(address, 0);
}
public ServerThread(ServerHandler server, String ip) throws Exception {
this(server, ip, 1024, 0);
}
public void run() {
while(true) {
try {
socket = dataSocket.accept();
socket.setKeepAlive(true);
socket.setSendBufferSize(megabyte);
socket.setSendBufferSize(megabyte);
socket.setTcpNoDelay(true);
socket.setReuseAddress(true);
InetSocketAddress clientAddress = new InetSocketAddress(socket.getInetAddress(), socket.getPort());
System.out.println("Starting");
if(getClients().size() > 0) {
for(Client c : getClients()) {
if(clientAddress != c.socket.getLocalSocketAddress()) {
Client client = new Client(socket, clientAddress);
getClients().add(client);
client.start();
System.out.println("Added new client!");
break;
}
}
} else {
Client client = new Client(socket, clientAddress);
getClients().add(client);
client.start();
System.out.println("Added new client!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public synchronized void sendData(byte[] data, InetAddress IPaddress, int port) {
if(this.getClient(new InetSocketAddress(IPaddress, port)) != null) {
this.getClient(new InetSocketAddress(IPaddress, port)).sendData(data);
}
}
public void serverShutdown() {
try {
this.dataSocket.close();
if(this.socket != null) this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public int getClientIndex(InetSocketAddress address) {
int index = 0;
for(Client c : getClients()) {
if(c.socket.getRemoteSocketAddress().equals(address)) {
break;
}
index++;
}
System.out.println("Getting client index...");
return index;
}
public synchronized ArrayList<Client> getClients() {
return this.clients;
}
private Client getClient(InetSocketAddress address) {
for(Client c : getClients()) {
if(c.socket.getRemoteSocketAddress().equals(address)) {
return c;
}
}
return null;
}
public class Client extends Thread {
DataInputStream in;
DataOutputStream out;
Socket socket;
public Client(Socket sock, InetSocketAddress IPaddress) {
try {
socket = sock;
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true) {
try {
byte[] data = new byte[in.readInt() - 4];
in.read(data);
server.parsePacket(data, socket.getInetAddress(), socket.getPort());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sendData(byte[] data) {
try {
out.writeInt(data.length + 4);
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I would really appreciate it if someone could help.
-Regards
user.....
Port forwarding does not work when both machines are on the same LAN. Here's why:
The client sends a packet to the server's WAN address. It goes to the router. (Because machines on a LAN use the router to reach any WAN address.)
The router port forwards the packet to the server's LAN address. The source IP address is not changed, it's still the client's LAN address. (That's what port forwarding does.)
The server accepts the TCP connection by sending packets to the address it saw as the source address of the packets it received -- the client's LAN address. And, of course it gives them the only source IP address it can, it's LAN address.
Packets sent from the server to the client's LAN address go directly to the client, the router has no opportunity to NAT them because they are sent to the client's LAN address. (Here's where things go awry. If the client had been on another network, the packets would have gone to the router, since that's how the server reaches other networks.)
The client receives packets from the server's LAN address, but it was expecting packets from the server's WAN address (since that's what it sent them to), so the connection cannot work.
If you want to connect to the server from other machines on the LAN you must either connect to the server's LAN address or use some form of dual-NAT such as hairpin NAT -- port forwarding won't work.
protected String doInBackground(String... f_url) {
int count;
byte [] ip_bytes = new byte[] {(byte)192,(byte)168,(byte)1,(byte)100};
try {
InetAddress address = InetAddress.getByAddress(ip_bytes );
byte buffer[] = new byte[2000];
DatagramPacket p = new DatagramPacket(buffer, buffer.length, address, port);
try {
DatagramSocket ds = new DatagramSocket(port);
Log.d("..........","Perfect Binding .... Waiting for Data");
ds.receive(p);
publishProgress(""+p);
Thread.sleep(100);
Log.d("","Received :) ");
} catch(Exception e)
{
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
....................................
This is code for receiving UDP packet in Async Backgroung Process, But am not getting a single packet ... what is wrong in my code ?? How i can overcome this problem ?
It seems for receiving, you don't need to specify the ip and port in DatagramPacket.
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
I'm developing some peer 2 peer app for my class and I was told for letting servers to discover each other they have to Multicast to their UDP port 1110 and listen to their UDP port 1110. I wrote the code like below for doing that. for testing I run 2 servers which both send and receive. but it seems nothing working. where do you think my problem is ?
I put 2 servers in 2 different folders. and I assigned to IP addresses to my NIC like this ifconfig eth0:3 192.168.0.11 netmask 255.255.255.0 up how should I tell each server about the new ip address?
BroadcastListner
class BroadcastListner implements Callable<Object> {
int PORT = 1110;
String IP = "255.255.255.255";
MulticastSocket socket ;
DatagramPacket packet;
InetAddress IPAD;
byte data[] = null ; //////////////change size
int numOfNodes;
BroadcastListner(String IP, int numOfNodes) {
try {
this.numOfNodes = numOfNodes;
this.IP = IP;
IPAD = InetAddress.getByName(IP);
socket = new MulticastSocket(PORT);
packet = new DatagramPacket(data,data.length);
} catch (Exception e) {
e.printStackTrace();
}
}
BroadcastListner(int numOfNodes) {
try{
this.numOfNodes = numOfNodes;
// this.IP = IP;
IPAD = InetAddress.getByName(IP);
socket = new MulticastSocket(PORT);
packet = new DatagramPacket(data,data.length);
} catch (Exception e) {
e.printStackTrace();
}
}
public String call() {
try{
socket.joinGroup(IPAD);
} catch (Exception e) {
e.printStackTrace();
return "";
}
while(true) {
try {
socket.receive(packet);
String str = new String(packet.getData());
System.out.println(" Time signal received from"+
packet.getAddress() + " Time is : " +str);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
//socket.leaveGroup(IPAD);
//socket.close();
//return "";
}
}
BroadcastSender
class BroadcastSender implements Callable<Object> {
int PORT = 1110;
String IP = "255.255.255.255";
MulticastSocket socket;
DatagramPacket packet;
InetAddress IPAD;
byte[] data = "IAmAServer".getBytes();
//int numOfNodes;
String str = "IAmAServer";
BroadcastSender(String IP) {
try {
// this.numOfNodes = numOfNodes;
this.IP = IP;
IPAD = InetAddress.getByName(IP);
socket = new MulticastSocket();
} catch (Exception e) {
e.printStackTrace();
}
}
BroadcastSender() {
try{
// this.numOfNodes = numOfNodes;
// this.IP = IP;
IPAD = InetAddress.getByName(IP);
socket = new MulticastSocket();
} catch (Exception e) {
e.printStackTrace();
}
}
public String call() {
try {
socket.joinGroup(IPAD);
socket.setTimeToLive(10);
} catch (Exception e) {
e.printStackTrace();
return "";
}
while(true) {
try {
Thread.sleep(2000);
packet = new DatagramPacket (data,str.length(),IPAD,PORT);
socket.send(packet);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
//return "";
}
}
You need to try the broadcast address of 192.168.0.255
An alternative is to use a multi-cast instead of broadcast address like 224.x.x.x which is not tied to a specific subnet.