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
46c9efab
Commit
46c9efab
authored
4 years ago
by
Terry D. Norbraten
Browse files
Options
Downloads
Patches
Plain Diff
no object creation in loops
parent
5df16e09
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/src/UdpMulticastHttpExamples/UdpReceiver.java
+32
-12
32 additions, 12 deletions
examples/src/UdpMulticastHttpExamples/UdpReceiver.java
examples/src/UdpMulticastHttpExamples/UdpSender.java
+19
-8
19 additions, 8 deletions
examples/src/UdpMulticastHttpExamples/UdpSender.java
with
51 additions
and
20 deletions
examples/src/UdpMulticastHttpExamples/UdpReceiver.java
+
32
−
12
View file @
46c9efab
...
@@ -2,6 +2,7 @@ package UdpMulticastHttpExamples;
...
@@ -2,6 +2,7 @@ package UdpMulticastHttpExamples;
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
/**
/**
* An example of receiving UDP packets. Since very often both the
* An example of receiving UDP packets. Since very often both the
* sender and receiver are on the same host we use different ports
* sender and receiver are on the same host we use different ports
...
@@ -20,36 +21,55 @@ public class UdpReceiver
...
@@ -20,36 +21,55 @@ public class UdpReceiver
/**
/**
* @param args the command line arguments
* @param args the command line arguments
* @throws java.io.IOException
*/
*/
public
static
void
main
(
String
[]
args
)
public
static
void
main
(
String
[]
args
)
throws
IOException
{
{
DatagramSocket
udpSocket
=
null
;
DataInputStream
dis
=
null
;
int
packetCount
=
0
;
try
try
{
{
System
.
out
.
println
(
"UdpReceiver started..."
);
System
.
out
.
println
(
UdpReceiver
.
class
.
getName
()
+
" started..."
);
// Create a UDP socket
// Create a UDP socket
DatagramSocket
udpSocket
=
new
DatagramSocket
(
RECEIVING_PORT
);
udpSocket
=
new
DatagramSocket
(
RECEIVING_PORT
);
udpSocket
.
setReceiveBufferSize
(
1500
);
udpSocket
.
setBroadcast
(
false
);
// we're just receiving here
byte
[]
receiveBuffer
=
new
byte
[
udpSocket
.
getReceiveBufferSize
()];
DatagramPacket
receivePacket
=
new
DatagramPacket
(
receiveBuffer
,
receiveBuffer
.
length
);
InputStream
bais
;
float
first
,
second
;
// You need a new receiving packet to read from every packet received
// You need a new receiving packet to read from every packet received
while
(
true
)
while
(
true
)
{
{
byte
[]
receiveBuffer
=
new
byte
[
1500
];
DatagramPacket
receivePacket
=
new
DatagramPacket
(
receiveBuffer
,
receiveBuffer
.
length
);
udpSocket
.
receive
(
receivePacket
);
udpSocket
.
receive
(
receivePacket
);
// Decode the contents by extracting the data from the packet
// Decode the contents by extracting the data from the packet
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
receivePacket
.
getData
());
bais
=
new
ByteArrayInputStream
(
receivePacket
.
getData
());
DataInputStream
dis
=
new
DataInputStream
(
bais
);
dis
=
new
DataInputStream
(
bais
);
// What happens if you read an integer? Two double values? ***
// What happens if you read an integer? Two double values? ***
float
first
=
dis
.
readFloat
();
// alternatives: readFloat(); readInt(); dis.readUTF();
first
=
dis
.
readFloat
();
// alternatives: readFloat(); readInt(); dis.readUTF();
float
second
=
dis
.
readFloat
();
second
=
dis
.
readFloat
();
System
.
out
.
println
(
"first value: "
+
first
+
" second value: "
+
second
);
System
.
out
.
println
(
"first value: "
+
first
+
" second value: "
+
second
+
" packet count = "
+
++
packetCount
);
}
}
}
}
catch
(
IOException
e
)
catch
(
IOException
e
)
{
{
System
.
out
.
println
(
"Problem with UdpReceiver, see exception trace:"
);
if
(
udpSocket
!=
null
)
System
.
out
.
println
(
e
);
udpSocket
.
close
();
if
(
dis
!=
null
)
dis
.
close
();
System
.
err
.
println
(
"Problem with UdpReceiver, see exception trace:"
);
System
.
err
.
println
(
e
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
examples/src/UdpMulticastHttpExamples/UdpSender.java
+
19
−
8
View file @
46c9efab
...
@@ -21,22 +21,27 @@ public class UdpSender
...
@@ -21,22 +21,27 @@ public class UdpSender
public
static
final
int
RECEIVING_PORT
=
1415
;
public
static
final
int
RECEIVING_PORT
=
1415
;
public
static
final
String
DESTINATION_HOST
=
"localhost"
;
public
static
final
String
DESTINATION_HOST
=
"localhost"
;
@SuppressWarnings
(
"SleepWhileInLoop"
)
@SuppressWarnings
(
"SleepWhileInLoop"
)
public
static
void
main
(
String
[]
args
)
public
static
void
main
(
String
[]
args
)
throws
IOException
{
{
DatagramSocket
udpSocket
=
null
;
DataOutputStream
dos
=
null
;
try
try
{
{
System
.
out
.
println
(
"UdpSender started..."
);
System
.
out
.
println
(
"UdpSender started..."
);
// Create a UDP socket
// Create a UDP socket
DatagramSocket
udpSocket
=
new
DatagramSocket
(
SENDING_PORT
);
udpSocket
=
new
DatagramSocket
(
SENDING_PORT
);
// 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
// data, which we put into the UDP packet.
// data, which we put into the UDP packet.
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
dos
=
new
DataOutputStream
(
baos
);
// alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
// alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
dos
.
writeFloat
(
17.0f
);
dos
.
writeFloat
(
17.0f
);
dos
.
writeFloat
(
24.0f
);
dos
.
writeFloat
(
24.0f
);
byte
[]
buffer
=
baos
.
toByteArray
();
byte
[]
buffer
=
baos
.
toByteArray
();
...
@@ -58,14 +63,20 @@ public class UdpSender
...
@@ -58,14 +63,20 @@ public class UdpSender
{
{
udpSocket
.
send
(
packet
);
udpSocket
.
send
(
packet
);
Thread
.
sleep
(
1000
);
// Send 100, one per second
Thread
.
sleep
(
1000
);
// Send 100, one per second
System
.
out
.
println
(
"Bert
S
ent packet "
+
index
+
" of 100"
);
System
.
out
.
println
(
"Bert
s
ent packet "
+
index
+
" of 100"
);
}
}
System
.
out
.
println
(
"UdpSender complete."
);
System
.
out
.
println
(
"UdpSender complete."
);
}
}
catch
(
IOException
|
InterruptedException
e
)
catch
(
IOException
|
InterruptedException
e
)
{
{
System
.
out
.
println
(
"Problem with UdpSender, see exception trace:"
);
if
(
udpSocket
!=
null
)
System
.
out
.
println
(
e
);
udpSocket
.
close
();
if
(
dos
!=
null
)
dos
.
close
();
System
.
err
.
println
(
"Problem with UdpSender, see exception trace:"
);
System
.
err
.
println
(
e
);
}
}
}
}
}
}
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