?? lhserver.java
字號:
import javax.microedition.io.*;
import java.io.*;
public class LHServer implements Runnable {
private LHCanvas canvas;
private DatagramConnection dc;
private String address;
private boolean connected;
public LHServer(LHCanvas c) {
canvas = c;
connected = false;
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Connect to the peer client
canvas.setStatus("Waiting for peer client...");
dc = null;
while (dc == null)
dc = (DatagramConnection)Connector.open("datagram://:5555");
while (true) {
// Try to receive a datagram packet
Datagram dg = dc.newDatagram(32);
dc.receive(dg);
address = dg.getAddress();
// Make sure the datagram actually contains data
if (dg.getLength() > 0) {
String data = new String(dg.getData(), 0, dg.getLength());
if (data.equals("Client")) {
// Notify the user of a successful network connection
canvas.setStatus("Connected to peer client.");
connected = true;
// Try to reply with a connection message
sendMessage("Server");
}
else {
// Send along the network data
canvas.receiveMessage(data);
}
}
}
}
catch (IOException ioe) {
System.err.println("The network port is already taken.");
}
catch (Exception e) {
}
}
public void sendMessage(String message) {
// Send the message
try {
// Convert the string message to bytes
byte[] bytes = message.getBytes();
// Send the message
Datagram dg = null;
dg = dc.newDatagram(bytes, bytes.length, address);
dc.send(dg);
}
catch (Exception e) {
}
}
public void stop() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -