Skip to content

Instantly share code, notes, and snippets.

@navczydev
Created November 29, 2017 02:20
Show Gist options
  • Select an option

  • Save navczydev/772ff631fc38548ba0fec2fc2a7d0c9d to your computer and use it in GitHub Desktop.

Select an option

Save navczydev/772ff631fc38548ba0fec2fc2a7d0c9d to your computer and use it in GitHub Desktop.
Java Big_ENDIAN implemettation to make udp datagram.
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class LabUnderGrad
{
static HashMap<Integer,String> hashMap = new HashMap<>();
static String course_Info=null;
static String STOP_CONDITION ="no";
static ArrayList<HashMap<Integer,String>> arrayList = new ArrayList<>();
static int key;
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
hashMap.put(249, "Comp249");
hashMap.put(248, "Comp461");
hashMap.put(247, "comp467");
hashMap.put(246, "comp460");
hashMap.put(245, "comp465");
//System.out.println("enter content");
/* while (true)
{
System.out.println("Enter course Info");
course_Info = scanner.next();
if (course_Info.equals(STOP_CONDITION))
{
break;
}
System.out.println("Enter key value");
key = scanner.nextInt();
hashMap.put(key,course_Info);
}*/
/* if (hashMap.containsKey(key))
{
System.out.println("Key is their "+ hashMap.get(key));
}
else
{
System.out.println("Key is not their ");
}*/
/* System.out.println("HashMap Contents are here");
for (Map.Entry m:hashMap.entrySet()
) {
System.out.println(m.getKey()+" "+m.getValue());
}
arrayList.add(hashMap);
System.out.println("Size is "+arrayList.size());*/
//System.out.println("Hey its toString ");
// LabUnderGrad labUnderGrad = new LabUnderGrad();
// System.out.println("Here is toString "+ labUnderGrad.toString());
// System.out.println("Enter key to Serach\n");
// key = scanner.nextInt();
// search(key);
/*if (ByteOrder.nativeOrder().equals(ByteOrder.nativeOrder())) {
System.out.println("Order is " + ByteOrder.BIG_ENDIAN.toString());
} else {
System.out.println("Order is native");
}*/
//int to endian
int SEQUENCE_NUMBER = 1;
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.putInt(SEQUENCE_NUMBER);
byte[] result = byteBuffer.array();
System.out.println("Sequence number is :");
for (int i = 0; i < result.length; i++) {
System.out.print(" " + result[i]);
}
String SERVER_IP_ADDRESS = "192.168.2.3";
System.out.println("\nServer ip is " + SERVER_IP_ADDRESS);
String[] ipArray = SERVER_IP_ADDRESS.split("\\.");
System.out.println("Ip broken into pieces " + ipArray.length);
for (int i = 0; i < ipArray.length; i++) {
System.out.print(" " + ipArray[i]);
}
int port = 8007;
ByteBuffer byteBuffer1 = ByteBuffer.allocate(4);
byteBuffer1.order(ByteOrder.BIG_ENDIAN);
byteBuffer1.putInt(port);
byte[] PORT_RESULT = byteBuffer1.array();
System.out.println("\nPort number is : ");
for (int i = 2; i < PORT_RESULT.length; i++)
{
System.out.print(" " + PORT_RESULT[i]);
}
String PAYLOAD = "Hi S";
ByteBuffer byteBuffer3 = ByteBuffer.allocate(PAYLOAD.length());
byteBuffer3.order(ByteOrder.BIG_ENDIAN);
byte[] PAYLOAD_BYTES = PAYLOAD.getBytes();
byteBuffer3.put(PAYLOAD_BYTES);
byte[] PAYLOAD_BYTE_RESULT = byteBuffer3.array();
System.out.println("\nPayload String is : ");
for (int i = 0; i < PAYLOAD_BYTE_RESULT.length; i++) {
System.out.print (" " + PAYLOAD_BYTE_RESULT[i]);
}
// char[] Payload_Data = PAYLOAD.toCharArray();
// for (int i = 0; i < Payload_Data.length; i++) {
// System.out.println("numeric " + Character.getNumericValue(Payload_Data[i]));
// }
///System.out.println("Bytes are here "+ PAYLOAD.getBytes().toString());
//ByteBuffer byteBuffer2 = ByteBuffer.allocate(4);
}
@Override
public String toString()
{
return "Key is "+hashMap.get(key);
}
private static void search(Integer key)
{
for(int i=0;i<arrayList.size();i++)
{
if(arrayList.get(i).get(key)!=null)
{
System.out.println("Value is " + arrayList.get(i).get(key).toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment