sysdis-project/command-terminal/CommandTerminal/src/commandterminal/CommandTerminal.java

70 lines
1.8 KiB
Java
Raw Normal View History

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandterminal;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
/**
*
* @author lmascaro
*/
public class CommandTerminal {
private final static int SGCA_MULTICAST_PORT = 4446;
private final static String SCGA_MULTICAST_ADDRESS = "224.0.0.3";
/**
* @param args the command line arguments
*/
public static void main(String[] args){
/*
* Handshake
*/
try {
DatagramSocket socket = new DatagramSocket();
2017-04-13 23:43:16 +00:00
ByteBuffer buf = ByteBuffer.allocate(21);
buf.clear();
buf.put((byte)(0x01|0x02));
DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
socket.send(payload);
socket.receive(payload);
buf = ByteBuffer.wrap(payload.getData());
if(buf.get() == 3){
System.out.println("--Connection request successful");
2017-04-13 23:43:16 +00:00
byte address[] = new byte[15];
buf = buf.get(address,0,15);
String addressString = new String(address);
InetAddress sgcaAddress = InetAddress.getByName(addressString);
2017-04-13 23:43:16 +00:00
//emulate an unsigned short
char cast = buf.getChar();
int port = (int) cast;
System.out.println("----Address : "+sgcaAddress.getHostAddress());
System.out.println("----Port : "+port);
}
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}