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
4ee5e60b
Commit
4ee5e60b
authored
4 years ago
by
Terry D. Norbraten
Browse files
Options
Downloads
Patches
Plain Diff
close socket
parent
e774ef57
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/src/UdpMulticastExamples/MulticastSender.java
+15
-5
15 additions, 5 deletions
examples/src/UdpMulticastExamples/MulticastSender.java
with
15 additions
and
5 deletions
examples/src/UdpMulticastExamples/MulticastSender.java
+
15
−
5
View file @
4ee5e60b
...
@@ -3,6 +3,8 @@ package UdpMulticastExamples;
...
@@ -3,6 +3,8 @@ package UdpMulticastExamples;
import
edu.nps.moves.dis7.utilities.DisThreadedNetIF
;
import
edu.nps.moves.dis7.utilities.DisThreadedNetIF
;
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
/**
* Looks a lot like UdpSender. Start this after launching MulticastReceiver.
* Looks a lot like UdpSender. Start this after launching MulticastReceiver.
...
@@ -28,11 +30,13 @@ public class MulticastSender {
...
@@ -28,11 +30,13 @@ public class MulticastSender {
public
static
final
int
LOOPSIZE
=
20
;
// 20000
public
static
final
int
LOOPSIZE
=
20
;
// 20000
public
static
final
String
QUIT_SENTINEL
=
"QUIT QUIT QUIT!"
;
public
static
final
String
QUIT_SENTINEL
=
"QUIT QUIT QUIT!"
;
private
static
NetworkInterface
ni
;
@SuppressWarnings
(
"SleepWhileInLoop"
)
@SuppressWarnings
(
"SleepWhileInLoop"
)
public
static
void
main
(
String
[]
args
)
throws
IOException
public
static
void
main
(
String
[]
args
)
throws
IOException
{
{
MulticastSocket
multicastSocket
=
null
;
MulticastSocket
multicastSocket
=
null
;
InetSocketAddress
group
=
null
;
// Put together a message with binary content. "ByteArrayOutputStream"
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// is a java.io utility that lets us put together an array of binary
...
@@ -51,18 +55,18 @@ public class MulticastSender {
...
@@ -51,18 +55,18 @@ public class MulticastSender {
System
.
setProperty
(
"java.net.preferIPv4Stack"
,
"true"
);
System
.
setProperty
(
"java.net.preferIPv4Stack"
,
"true"
);
// multicast group we are sending to--not a single host
// multicast group we are sending to--not a single host
multicastSocket
=
new
MulticastSocket
(
DESTINATION_PORT
);
multicastSocket
=
new
MulticastSocket
(
/*
DESTINATION_PORT
*/
);
multicastSocket
.
setTimeToLive
(
TTL
);
// time to live reduces scope of transmission
multicastSocket
.
setTimeToLive
(
TTL
);
// time to live reduces scope of transmission
InetAddress
multicastAddress
=
InetAddress
.
getByName
(
MULTICAST_ADDRESS
);
InetAddress
multicastAddress
=
InetAddress
.
getByName
(
MULTICAST_ADDRESS
);
System
.
out
.
println
(
"Multicast address/port: "
+
multicastAddress
.
getHostAddress
()
+
"/"
+
DESTINATION_PORT
);
System
.
out
.
println
(
"Multicast address/port: "
+
multicastAddress
.
getHostAddress
()
+
"/"
+
DESTINATION_PORT
);
InetSocketAddress
group
=
new
InetSocketAddress
(
multicastAddress
,
DESTINATION_PORT
);
group
=
new
InetSocketAddress
(
multicastAddress
,
DESTINATION_PORT
);
// Join group useful on receiving side
// Join group useful on receiving side
multicastSocket
.
joinGroup
(
group
,
DisThreadedNetIF
.
findIpv4Interface
());
multicastSocket
.
joinGroup
(
group
,
ni
=
DisThreadedNetIF
.
findIpv4Interface
());
// You can join multiple groups here
// You can join multiple groups here
byte
[]
buffer
=
baos
.
toByteArray
();
byte
[]
buffer
=
baos
.
toByteArray
();
DatagramPacket
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
,
multicastAddress
,
DESTINATION_PORT
);
DatagramPacket
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
,
group
/*
, DESTINATION_PORT
*/
);
for
(
int
index
=
0
;
index
<
LOOPSIZE
;
index
++)
for
(
int
index
=
0
;
index
<
LOOPSIZE
;
index
++)
{
{
...
@@ -99,8 +103,14 @@ public class MulticastSender {
...
@@ -99,8 +103,14 @@ public class MulticastSender {
System
.
err
.
println
(
e
);
System
.
err
.
println
(
e
);
}
finally
{
}
finally
{
if
(
multicastSocket
!=
null
)
if
(
multicastSocket
!=
null
&&
!
multicastSocket
.
isClosed
())
{
try
{
multicastSocket
.
leaveGroup
(
group
,
ni
);
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
MulticastSender
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
multicastSocket
.
close
();
multicastSocket
.
close
();
}
dos
.
close
();
dos
.
close
();
}
}
...
...
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