KLALB
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class KLALBServer {
|
||||
WeakHashMap<UUID, KLALBServerProtocol> whm=new WeakHashMap<>();
|
||||
public KLALBServer(int port) throws IOException {
|
||||
TCPListener tcpl=new TCPListener(port);
|
||||
tcpl.setCon((s)->{
|
||||
try {
|
||||
//s.setSoTimeout(10000);
|
||||
DataInputStream din=new DataInputStream(s.getInputStream());
|
||||
int val=din.readShort()&0xffff;
|
||||
if(val!=59649) {
|
||||
return;
|
||||
}
|
||||
UUID uid=new UUID(din.readLong(),din.readLong());
|
||||
System.out.println(uid);
|
||||
KLALBServerProtocol nx = null;
|
||||
if(whm.containsKey(uid)) {
|
||||
nx=whm.get(uid);
|
||||
}else {
|
||||
nx=new KLALBServerProtocol();
|
||||
Socket soc=new Socket("192.168.1.233",8444);
|
||||
nx.setOut(soc.getOutputStream());
|
||||
nx.setIn(soc.getInputStream());
|
||||
nx.startLocal();
|
||||
whm.put(uid, nx);
|
||||
|
||||
}
|
||||
nx.handleSocket(new TCPConnection(null,s));
|
||||
}catch(ConnectException e) {
|
||||
System.out.println("连接本地服务失败,请检查你的服务程序");
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
s.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
tcpl.open();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
KLALBServer kc=new KLALBServer(4569);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user