?? c4client.java
字號:
import javax.microedition.io.*;
import java.io.*;
public class C4Client implements Runnable {
private C4Canvas canvas;
private DatagramConnection dc;
private boolean connected;
public C4Client(C4Canvas c) {
canvas = c;
connected = false;
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Connect to the peer server
canvas.setStatus("Connecting to peer server...");
dc = null;
while (dc == null)
dc = (DatagramConnection)Connector.open("datagram://localhost:5555");
while (true) {
// Try to send a connection message
if (!connected)
sendMessage("Client");
// Try to receive a datagram packet
Datagram dg = dc.newDatagram(32);
dc.receive(dg);
// Make sure the datagram actually contains data
if (dg.getLength() > 0) {
String data = new String(dg.getData(), 0, dg.getLength());
if (data.equals("Server")) {
// Notify the user of a successful network connection
canvas.setStatus("Connected to peer server.");
canvas.newGame();
connected = true;
}
else {
// Send along the network game data
canvas.receiveMessage(data);
}
}
}
}
catch (ConnectionNotFoundException cnfe) {
System.err.println("The network server is unavailable.");
}
catch (IOException ioe) {
}
}
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);
dc.send(dg);
}
catch (Exception e) {
}
}
public void stop() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -