02.1、建立Server(保持这个TankServer一直运行)
package server;
public class TankServer {
public static void main(String[] args) {
try {
// new一个ServerSocket对象,并时时刻刻监听着7575端口
ServerSocket ss = new ServerSocket(7575);
while(true) {
// 接受Socket信息
Socket s = ss.accept();
System.out.println("TankServer.java:监听到" + s.getInetAddress() + ":" + s.getPort());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
02.2、建立Client(新建一个NetClient模型)
package client;
public class NetClient {
//Client连接Server
public void connect(String IP, int port) {
try {
// IP地址与端口
Socket s = new Socket(IP, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
02.3、使Client与Server连接()
package client; public class GameClient extends Frame {
//新建一个网络客户端模型
NetClient nc = new NetClient();
//实现与TankServer连接
this.nc.connect("192.168.1.1", TankServer.TCP_PORT);
}