70 lines
1.8 KiB
Java
70 lines
1.8 KiB
Java
/*
|
|
* 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();
|
|
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");
|
|
byte address[] = new byte[15];
|
|
buf = buf.get(address,0,15);
|
|
String addressString = new String(address);
|
|
InetAddress sgcaAddress = InetAddress.getByName(addressString);
|
|
//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();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|