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

73 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();
ByteBuffer buf = ByteBuffer.allocate(19);
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[9];
buf = buf.get(address,0,9);
String addressString = new String(address);
InetAddress sgcaAddress = InetAddress.getByName(addressString);
buf = buf.get(address,0,7);
short shortVal = buf.getShort();
int port = shortVal >= 0 ? shortVal : 0x10000 + shortVal;
System.out.println("----Address : "+sgcaAddress.getHostAddress());
System.out.println("----Port : "+port);
}
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}