forked from KNEMC/KLALB
KLALB UDP TEST
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package org.kne.cloud.network;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.channels.spi.AbstractInterruptibleChannel;
|
||||
import java.util.*;
|
||||
|
||||
public class PortRelay {
|
||||
private int port;
|
||||
private HostPortMap services;
|
||||
private SocketListener ssc;
|
||||
public PortRelay(int port, HostPortMap services) throws IOException {
|
||||
this.services = services;
|
||||
this.port = port;
|
||||
MultipurposeSocketAddress.getSocketTypeRegister().put("ProtocolDetectorServerSocket",new SocketType(null, new ProtocolDetectorServerSocketFactory()) );
|
||||
ssc=new SocketListener(new MultipurposeSocketAddress("ProtocolDetectorServerSocket", "0.0.0.0", port));
|
||||
}
|
||||
public void start() throws IOException {
|
||||
ssc.setCon((s)->{
|
||||
|
||||
Socket sl = null;
|
||||
ProtocolDetectorSocket pds=(ProtocolDetectorSocket) s;
|
||||
String nx;
|
||||
if(pds.getProtocolStack().isEmpty()) {
|
||||
nx="DEFAULT";
|
||||
}else {
|
||||
nx=pds.getProtocolStack().pop().getName();
|
||||
}
|
||||
MultipurposeSocketAddress hp=services.get(nx);
|
||||
InetSocketAddress sa=(InetSocketAddress) s.getRemoteSocketAddress();
|
||||
System.out.println(sa.getAddress().getHostAddress() + ":" + port
|
||||
+ "-" + "(" + nx + ")->" + hp.getHost() + ":" + hp.getPort());
|
||||
try {
|
||||
sl=hp.connectSocket();
|
||||
|
||||
new SocketBridge(s, sl).run();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
if(sl!=null)
|
||||
sl.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
pds.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
System.out.println("已打开端口:" + port);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user