1、首先打开电脑之后,在键盘上按下组合键 win+r 打开运行对话框,如下图所示。
2、然后在打开的对话框中输入CMD,如下图所示。
3、按下enter键,打开cmd命令行窗口,在命令行中输入:netstat -na,如下图所示。
4、这时在键盘中按下回车键,如下图所示,就会显示本地连接以及打开的端口。
是。1、这个端口非空闲状态,空闲状态的端口可以自己用来做别的用途,处理使用或监听状态的端口无法更改比方说21端口,当你使用FTP客户端协议的时候,这个端口就会被使用。
2、这时不能改来做别的用如果是安装了服务端FTP软件,它就会监听21端口上有没有请求,如果有就作一系列操作,监听就是监视的意思,作用是通过这个端口得到请求信息。
可以考虑用JAVA实现监听客户端口,参考例子如下:
package testimport java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.lang.reflect.Method
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketException
import java.util.HashMap
import java.util.LinkedList
import java.util.List
import java.util.Random
/**
* 该程序用的DatagramSocket,实现UDP的通讯
* @author D'Addario
*
*/
public class Test{
private int headLen = 12//消息起点和终点的记录 例如classAclassB就是发往classA的消息,发送者是classB
private Random random
private String serverIp
private int serverPort
private HashMap<String,Classes> map
{
serverIp = "127.0.0.1"
serverPort = 9999
random = new Random(System.currentTimeMillis())
map = new HashMap<String,Classes>()
Classes A = Test.this.new Classes()
A.ip = "127.0.0.1"
A.name = "我是A"
A.port = 10000
map.put("classA", A)
Classes B = Test.this.new Classes()
B.ip = "127.0.0.1"
B.name = "我是B"
B.port = 10001
map.put("classB", B)
}
public static void main(String[] a) throws Exception{
Test test = new Test()
new Thread(test.new Server()).start()
Client client1 = test.new Client()
client1.mine = "classA"
client1.remote = "classB"
new Thread(client1).start()
Client client2 = test.new Client()
client2.mine = "classB"
client2.remote = "classA"
new Thread(client2).start()
}
/**
* 成员 用于记录 每个成员的个性信息
* @author D'Addario
*
*/
private class Classes {
private String name //成员姓名
private String ip //成员ip
private int port//成员的client server 监听端口
}
/**
* server 用于转发两个client之间消息的类
* @author D'Addario
*
*/
private class Server implements Runnable{
public void run() {
// TODO Auto-generated method stub
boolean start = false
DatagramSocket socket = null
try {
socket = new DatagramSocket(serverPort)
start = true
} catch (SocketException e1) {
e1.printStackTrace()
}
while(start)
{
try {
byte[] data = new byte[1024]
DatagramPacket packet = new DatagramPacket(data,data.length)
socket.receive(packet)
int len = packet.getLength()
//System.out.println("recevie data:"+new String(data,0,len))
if(len<headLen)
throw new IOException("无效数据")
String id = new String(data,0,headLen/2)//获取发往对象的ID
String id2 = new String(data,headLen/2,headLen/2)
System.out.println("receive from "+id2 +", send to "+id+",msg["+new String(data,headLen,len-headLen)+"]")
Classes one = map.get(id.trim())
DatagramPacket retPack = new DatagramPacket(data,0,len,InetAddress.getByName(one.ip),one.port)
socket.send(retPack)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
/**
* Client 客户端类,用于客户端消息的发送
* @author D'Addario
*
*/
private class Client implements Runnable{
private String mine
private String remote
public void run() {
// TODO Auto-generated method stub
String msg = remote+mine
DatagramSocket socket = null
try {
socket = new DatagramSocket(map.get(mine).port)
ClientServer srv=new ClientServer()
srv.setSocket(socket)
new Thread(srv).start()
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
byte[] data
while(true)
{
String sendStr = msg + "给我"+random.nextInt(5000)+"分吧"
data = sendStr.getBytes()
try {
DatagramPacket retPack = new DatagramPacket(data,0,data.length,InetAddress.getByName(serverIp),serverPort)
socket.send(retPack)
Thread.sleep(random.nextInt(5)*1000)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
/**
* 客户端server 用于监听服务器的消息
* @author D'Addario
*
*/
private class ClientServer implements Runnable{
DatagramSocket socket
public DatagramSocket getSocket() {
return socket
}
public void setSocket(DatagramSocket socket) {
this.socket = socket
}
public void run() {
byte[] data = new byte[1024]
DatagramPacket packet = new DatagramPacket(data,data.length)
while(true){
try {
socket.receive(packet)
int len = packet.getLength()
if(len<headLen)
throw new IOException("无效数据")
System.out.println("I get the message :"+Thread.currentThread().getId()+"-->["+new String(data,0,len)+"]")
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} }
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)