我刚试了,没有出现上面的运行结果 如果你出现了那种情况,那么必定有某种程序在连接你本机的TCP端口8888 那么把 public static final int TCP_PORT = 8888; 换成 public static final int TCP_PORT = 9999; 在重启TankServer,估计它就会在s = ss.accept();等待了 import java.io.*; import java.net.*; import java.util.*; import java.util.List;public class TankServer { private static int ID = 100; public static final int TCP_PORT = 8888; public static final int UDP_PORT = 6666;
List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new TankServer().start(); }
public void start() { ServerSocket ss = null; try { ss = new ServerSocket(TCP_PORT); } catch (IOException e) { e.printStackTrace(); } while (true) { Socket s = null; try { s = ss.accept(); DataInputStream dis = new DataInputStream(s.getInputStream()); String IP = s.getInetAddress().getHostAddress(); int udpPort = dis.readInt(); Client c = new Client(IP, udpPort);//服务器为什么发这个udp端口给客户端???????????
clients.add(c); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeInt(ID++); // s.close(); System.out.println("A Client Connect! Addr- " + s.getInetAddress() + ":" + s.getPort() + "----UDP Port:" + udpPort); } catch (IOException e) { e.printStackTrace(); } finally { if (s != null) { try { s.close(); s = null; } catch (IOException e) { e.printStackTrace(); } } } } } private class Client { String IP; int udpPort; public Client(String IP, int udpPort) { this.IP = IP; this.udpPort = udpPort; } } }
如果你出现了那种情况,那么必定有某种程序在连接你本机的TCP端口8888
那么把 public static final int TCP_PORT = 8888;
换成 public static final int TCP_PORT = 9999;
在重启TankServer,估计它就会在s = ss.accept();等待了
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;public class TankServer {
private static int ID = 100;
public static final int TCP_PORT = 8888;
public static final int UDP_PORT = 6666;
List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) {
new TankServer().start();
}
public void start() {
ServerSocket ss = null;
try {
ss = new ServerSocket(TCP_PORT);
} catch (IOException e) {
e.printStackTrace();
} while (true) {
Socket s = null;
try {
s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String IP = s.getInetAddress().getHostAddress();
int udpPort = dis.readInt();
Client c = new Client(IP, udpPort);//服务器为什么发这个udp端口给客户端???????????
clients.add(c);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(ID++);
// s.close();
System.out.println("A Client Connect! Addr- "
+ s.getInetAddress() + ":" + s.getPort()
+ "----UDP Port:" + udpPort);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
s = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} private class Client {
String IP;
int udpPort; public Client(String IP, int udpPort) {
this.IP = IP;
this.udpPort = udpPort;
}
}
}
执行上面的命令,看看谁在连8888端口
tcp 127.0.0.1:57025 127.0.0.1:8888 SYN_SENT 3844
这是什么意思 前段时间用8888都没问题
[mysqld.exe](进程名)这段的意思是 mysqlld.exe(PID:1412)的进程在本机所有ip的3306端口监听