Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
d2e00b3b
Commit
d2e00b3b
authored
7 years ago
by
Kens
Browse files
Options
Downloads
Patches
Plain Diff
Homework2
parent
124a358a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
projects/Assignments/homework2/MaroonMulticastReceiver.java
+63
-37
63 additions, 37 deletions
projects/Assignments/homework2/MaroonMulticastReceiver.java
projects/Assignments/homework2/MaroonMulticastSenderExample.java
+2
-2
2 additions, 2 deletions
...s/Assignments/homework2/MaroonMulticastSenderExample.java
with
65 additions
and
39 deletions
projects/Assignments/homework2/MaroonMulticastReceiver.java
+
63
−
37
View file @
d2e00b3b
...
...
@@ -3,6 +3,11 @@
import
java.io.*
;
import
java.net.*
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
/**
*
...
...
@@ -14,57 +19,78 @@ public class MaroonMulticastReceiver {
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
{
JFrame
guiFrame
=
new
JFrame
();
guiFrame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
guiFrame
.
setTitle
(
"Read a message"
);
guiFrame
.
setSize
(
300
,
250
);
guiFrame
.
setLocationRelativeTo
(
null
);
final
JPanel
output
=
new
JPanel
();
output
.
setVisible
(
true
);
JLabel
outputLbl
=
new
JLabel
(
"Hit the Button"
);
output
.
add
(
outputLbl
);
JButton
MCButton
=
new
JButton
(
"Push Me"
);
MCButton
.
addActionListener
(
new
ActionListener
(){
@Override
public
void
actionPerformed
(
ActionEvent
event
){
try
{
// This is a java/IPv6 problem. You should also add it to the
// arguments used to start the app, eg -Djava.net.preferIPv4Stack=true
// set in the "run" section of preferences. Also, typically
// netbeans must be restarted after these settings.
// https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache
System
.
setProperty
(
"java.net.preferIPv4Stack"
,
"true"
);
// This is a java/IPv6 problem. You should also add it to the
// arguments used to start the app, eg -Djava.net.preferIPv4Stack=true
// set in the "run" section of preferences. Also, typically
// netbeans must be restarted after these settings.
// https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache
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
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
int
count
=
0
;
int
count
=
0
;
while
(
true
)
{
byte
[]
packetArray
=
new
byte
[
1500
];
DatagramPacket
packet
=
new
DatagramPacket
(
packetArray
,
packetArray
.
length
);
byte
[]
packetArray
=
new
byte
[
1500
];
DatagramPacket
packet
=
new
DatagramPacket
(
packetArray
,
packetArray
.
length
);
multicastSocket
.
receive
(
packet
);
count
++;
multicastSocket
.
receive
(
packet
);
count
++;
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
packet
.
getData
());
DataInputStream
dis
=
new
DataInputStream
(
bais
);
int
firstNumber
=
dis
.
readInt
();
int
messageLength
=
dis
.
readInt
();
String
message
=
""
;
for
(
int
i
=
0
;
i
<
messageLength
;
i
++){
message
+=
dis
.
readChar
();
}
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
packet
.
getData
());
DataInputStream
dis
=
new
DataInputStream
(
bais
);
int
firstNumber
=
dis
.
readInt
();
int
messageLength
=
dis
.
readInt
();
String
message
=
""
;
for
(
int
i
=
0
;
i
<
messageLength
;
i
++){
message
+=
dis
.
readChar
();
}
outputLbl
.
setText
(
message
);
System
.
out
.
println
(
"Number received: "
+
count
+
" First number:"
+
firstNumber
+
" message:"
+
message
);
System
.
out
.
println
(
"Number received: "
+
count
+
" First number:"
+
firstNumber
+
" message:"
+
message
);
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
);
}
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
);
guiFrame
.
add
(
output
,
BorderLayout
.
CENTER
);
guiFrame
.
add
(
MCButton
,
BorderLayout
.
NORTH
);
guiFrame
.
setVisible
(
true
);
}
}
This diff is collapsed.
Click to expand it.
projects/Assignments/homework2/MaroonMulticastSenderExample.java
+
2
−
2
View file @
d2e00b3b
...
...
@@ -43,7 +43,7 @@ public class MaroonMulticastSenderExample {
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
dos
.
writeInt
(
1
);
String
message
=
"
Hammertime
"
;
String
message
=
"
**********************************
"
;
dos
.
writeInt
(
message
.
length
());
dos
.
writeChars
(
message
);
byte
[]
buffer
=
baos
.
toByteArray
();
...
...
@@ -52,7 +52,7 @@ public class MaroonMulticastSenderExample {
baos
.
reset
();
dos
.
writeInt
(
2
);
message
=
"
Collaberate and Listen
"
;
message
=
"
<===========================>
"
;
dos
.
writeInt
(
message
.
length
());
dos
.
writeChars
(
message
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment