?? netclient.java
字號:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class NetClient {
TankClient tc;
private static int UDP_PORT_START = 2223;
private int udpPort;
public NetClient(TankClient tc) {
udpPort = UDP_PORT_START ++;
this.tc = tc;
}
public void connect(String IP, int port) {
Socket s = null;
try {
s = new Socket(IP, port);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(udpPort);
DataInputStream dis = new DataInputStream(s.getInputStream());
int id = dis.readInt();
tc.myTank.id = id;
System.out.println("Connected to server! and server give me a ID:" + id);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(s != null) {
try {
s.close();
s = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -