-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcus Lankenau
committed
May 20, 2011
1 parent
673087b
commit fd2c6ce
Showing
4 changed files
with
111 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.galaxy.net; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
|
||
import org.galaxy.Ship; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.JSONValue; | ||
|
||
public class UDPUtil { | ||
|
||
/** | ||
* receive a packet from udp | ||
* @param socket | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String receive(DatagramSocket socket) throws IOException { | ||
byte[] buffer = new byte[100000]; | ||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); | ||
socket.receive(packet); | ||
return new String(buffer, packet.getOffset(), packet.getLength(), "UTF-8"); | ||
} | ||
|
||
|
||
/** | ||
* send a packet | ||
* @param socket | ||
* @param message | ||
* @throws IOException | ||
*/ | ||
public static void send(DatagramSocket socket, String message) throws IOException { | ||
byte[] bytes; | ||
try { | ||
bytes = message.toString().getBytes("UTF-8"); | ||
DatagramPacket packet = new DatagramPacket(bytes, bytes.length); | ||
socket.send(packet); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters