Projet_COO_POO/POO/src/communication/udp/UDPClient.java

43 lines
No EOL
1.3 KiB
Java

package communication.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import messages.*;
public class UDPClient {
private DatagramSocket sockUDP;
private InetAddress broadcast;
public UDPClient(int port) throws SocketException, UnknownHostException {
this.sockUDP = new DatagramSocket(port);
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
System.out.println(this.broadcast);
System.out.println(InetAddress.getLocalHost());
}
//Send a message casted as string to the specified port on localhost
protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException {
String messageString= message.toString();
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
this.sockUDP.send(outpacket);
}
protected void destroyAll() {
this.sockUDP.close();
this.sockUDP = null;
this.broadcast = null;
}
}