Skip to content
Snippets Groups Projects
Commit 1144671d authored by Rico's avatar Rico
Browse files

Landas assignment 2

parent 68aaa91b
No related branches found
No related tags found
No related merge requests found
Showing
with 182 additions and 18 deletions
File added
File added
File added
File added
File added
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
*
* @author Rico
*/
public class LandasMulticastReceiver {
final static String INET_ADDR = "239.1.2.15";
final static int PORT = 1717;
public static void main(String[] args) throws UnknownHostException {
// address that we are connecting to
InetAddress address = InetAddress.getByName(INET_ADDR);
// buffer of bytes
// the incoming bytes containing the information from the server
byte[] buf = new byte[256];
// Multicast socket
try (MulticastSocket clientSocket = new MulticastSocket(PORT)){
// Join
clientSocket.joinGroup(address);
while (true) {
// Receive information and print
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.println("Socket 1 received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
*
* @author Rico
*/
public class LandasMulticastReceiver2 {
final static String INET_ADDR = "239.1.2.15";
final static int PORT = 1717;
public static void main(String[] args) throws UnknownHostException {
// address that we are connecting to
InetAddress address = InetAddress.getByName(INET_ADDR);
// buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server
byte[] buf = new byte[256];
// Multicast socket
try (MulticastSocket clientSocket = new MulticastSocket(PORT)){
// Join
clientSocket.joinGroup(address);
while (true) {
// Receive information and print
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.println("Socket 2 received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*
* @author Rico
*/
public class LandasMulticastSender {
final static String INET_ADDR = "239.1.2.15";
final static int PORT = 1717;
public static void main(String[] args) throws UnknownHostException, InterruptedException {
// address that we are connecting to
InetAddress addr = InetAddress.getByName(INET_ADDR);
// new DatagramSocket
// used to send the data
try (DatagramSocket serverSocket = new DatagramSocket()) {
for (int i = 0; i < 100; i++) {
String msg = "Server 1 sent message no " + i;
// packet
// (in the form of bytes)
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(),
msg.getBytes().length, addr, PORT);
serverSocket.send(msgPacket);
System.out.println("Server 1 sent packet with msg: " + msg);
Thread.sleep(1000);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*
* @author Rico
*/
public class LandasMulticastSender2 {
final static String INET_ADDR = "239.1.2.15";
final static int PORT = 1717;
public static void main(String[] args) throws UnknownHostException, InterruptedException {
// address that we are connecting to
InetAddress addr = InetAddress.getByName(INET_ADDR);
// new DatagramSocket
// used to send the data
try (DatagramSocket serverSocket = new DatagramSocket()) {
for (int j = 0; j < 100; j++) {
String msg = "Server 2 sent message no " + j;
// packet
// (in the form of bytes)
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(),
msg.getBytes().length, addr, PORT);
serverSocket.send(msgPacket);
System.out.println("Server 2 sent packet with msg: " + msg);
Thread.sleep(1000);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
<?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/multicastExample1/MulticastReceiver/src/multicastreceiver/MulticastReceiver.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/multicastExample1/MulticastReceiver/src/multicastreceiver/MulticastReceiver.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/multicastExample1/MulticastSenderExample/src/multicastsenderexample/MulticastSenderExample.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/multicastExample1/MulticastSenderExample/src/multicastsenderexample/MulticastSenderExample.java</file>
</group>
</open-files>
</project-private>
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