Skip to content
Snippets Groups Projects
Commit b44e561b authored by danielcain's avatar danielcain
Browse files

Homework 3 - CainThomerson Working Copy

parent 8f2d36c4
No related branches found
No related tags found
No related merge requests found
File deleted
/*
* 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 MV3500Cohort2018JulySeptember.homework3.Cain_Thomerson_Homework3;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author danielcain with credit to CDR Angelopolis
*/
public class CainThomersonHw3Receiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT);
multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println(multicastAddress);
// Join group useful on receiving side
multicastSocket.joinGroup(multicastAddress);
// You can join multiple groups here
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
char type = dis.readChar();
if (type == 'A') {
float firstNumber = dis.readFloat();
float secondNumber = dis.readFloat();
float thirdNumber = dis.readFloat();
System.out.println("help! Ninjas took the keg!");
System.out.println("the keg was spotted at coordinates (x: " + firstNumber + ", y: " + secondNumber +", z: " + thirdNumber + " )");
}
if (type == 'B') {
int lngth = dis.readInt();
String informationLine = "the customer says: ";
for(int i=0; i<lngth; i++) {
informationLine += dis.readChar();
}
System.out.println(informationLine);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
package MV3500Cohort2018JulySeptember.homework3.Cain_Thomerson_Homework3;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Random;
/**
*
* @author danielcain with credit to CDR Angelopolis
*/
public class CainThomersonHw3Sender {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket multicastSocket = new MulticastSocket(1718);
multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println(multicastAddress);
// Join group useful on receiving side
System.out.println(multicastAddress);
// Join group useful on receiving side
multicastSocket.joinGroup(multicastAddress);
// You can join multiple groups here
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(byteOut);
Random rand = new Random();
Random rndFLT = new Random();
for(int idx = 1; idx <+ 100; idx++){
float flt = rndFLT.nextFloat();
if (flt <=.5){
dos.writeChars("A");
dos.writeFloat(rand.nextFloat()*100);
dos.writeFloat(rand.nextFloat()*100);
dos.writeFloat(rand.nextFloat()*100);
}
else if (flt < .8 ) {
dos.writeChars("B");
dos.writeInt("Information Bravo.".length());
dos.writeChars("study for networks");
}
else {
dos.writeChars("B");
dos.writeInt("Information Charlie.".length());
dos.writeChars("Drink beer and chill.");
}
byte[] buffer = byteOut.toByteArray();
// Put together a packet to send
// muticast group we are sending to--not a single host
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
// How fast does this go? Does UDP try to slow it down, or does
// this cause network problems? (hint: yes for an unlimited send
// rate, unlike TCP). How do you know on the receiving side
// that you haven't received a duplicate UDP packet, out of
// order packet, or dropped packet?
multicastSocket.send(packet);
byteOut.reset();
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent multicast packet " + idx + " of 100");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group name="MV3500 networking open-dis">
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java</file>
</group>
</open-files>
</project-private>
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group name="MV3500 networking open-dis">
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java</file>
</group>
</open-files>
</project-private>
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/Users/mcgredo/projects/gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java</file>
<file>file:/Users/mcgredo/projects/gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java</file>
</group>
<group name="MV3500 networking open-dis">
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java</file>
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java</file>
</group>
</open-files>
</project-private>
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/Users/mcgredo/projects/gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java</file>
<file>file:/Users/mcgredo/projects/gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java</file>
</group>
<group name="MV3500 networking open-dis">
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java</file>
<file>file:/E:/x-nps-gitlab/NetworkedGraphicsMV3500/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java</file>
</group>
</open-files>
</project-private>
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment