From edce7b0579347925b387862ab56bb2b3d9d200d9 Mon Sep 17 00:00:00 2001 From: wuw Date: Tue, 19 Dec 2017 21:52:17 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/bin/bullet | 5 --- .../java/com/wuweibi/bullet/client/Main.java | 5 ++- .../wuweibi/bullet/client/SocketThread.java | 31 +++++++++++++++-- .../java/com/wuweibi/bullet/ConfigUtils.java | 13 +++++--- .../java/com/wuweibi/bullet/SocketUtils.java | 33 +++++++------------ 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/Client/bin/bullet b/Client/bin/bullet index cdfb2a8d..7f300f06 100755 --- a/Client/bin/bullet +++ b/Client/bin/bullet @@ -1,11 +1,6 @@ #!/bin/sh - - - - - # _ooOoo_ # o8888888o # 88" . "88 diff --git a/Client/src/main/java/com/wuweibi/bullet/client/Main.java b/Client/src/main/java/com/wuweibi/bullet/client/Main.java index a85dca5e..c72241b1 100644 --- a/Client/src/main/java/com/wuweibi/bullet/client/Main.java +++ b/Client/src/main/java/com/wuweibi/bullet/client/Main.java @@ -24,7 +24,7 @@ public class Main { - public static void main(String[] args) throws URISyntaxException, IOException, DeploymentException { + public static void main(String[] args) throws URISyntaxException, IOException, DeploymentException, InterruptedException { String url = ConfigUtils.getTunnel() +"/"+ ConfigUtils.getDeviceId(); @@ -36,6 +36,9 @@ public static void main(String[] args) throws URISyntaxException, IOException, D Session session1 = container.connectToServer(Client.class, new URI(url)); // 连接会话 + while (true){ + Thread.sleep(3000000L); + } } diff --git a/Client/src/main/java/com/wuweibi/bullet/client/SocketThread.java b/Client/src/main/java/com/wuweibi/bullet/client/SocketThread.java index a236e3ca..cccbbf39 100644 --- a/Client/src/main/java/com/wuweibi/bullet/client/SocketThread.java +++ b/Client/src/main/java/com/wuweibi/bullet/client/SocketThread.java @@ -7,6 +7,7 @@ import com.wuweibi.bullet.protocol.Message; import com.wuweibi.bullet.protocol.MsgHead; import com.wuweibi.bullet.protocol.MsgProxyHttp; +import io.netty.buffer.ByteBuf; import org.apache.commons.io.IOUtils; import org.java_websocket.client.WebSocketClient; import org.slf4j.Logger; @@ -86,13 +87,21 @@ public void run() { socketChannel.shutdownOutput(); } catch (IOException e) { - logger.error("", e); +// logger.error("", e); + msg.setContent(getMessage(e.getMessage())); } // 处理响应 logger.debug("接收响应内容..."); - byte[] bytesout = SocketUtils.receiveData(socketChannel); + byte[] bytesout = new byte[0]; + try { + bytesout = SocketUtils.receiveData(socketChannel); + msg.setContent(bytesout); + } catch (IOException e) { + msg.setContent(getMessage(e.getMessage())); - msg.setContent(bytesout); + } catch (NullPointerException e) { + msg.setContent(getMessage("server "+msg.getServerAddr()+":"+msg.getPort()+" invoke faild!")); + } System.out.println("wlen=" + bytesout.length); ByteArrayOutputStream os = new ByteArrayOutputStream(); @@ -108,4 +117,20 @@ public void run() { } + + + /** + * 发送消息 + * + * @param msg 消息内容 + */ + private byte[] getMessage(String msg){ + StringBuilder sb = new StringBuilder(); + sb.append("HTTP/1.1 200 OK\n"); + sb.append("Content-Type:text/html; charset:GBK"); + sb.append("\n\n"); + sb.append(msg); + return sb.toString().getBytes(); + } + } diff --git a/Common/src/main/java/com/wuweibi/bullet/ConfigUtils.java b/Common/src/main/java/com/wuweibi/bullet/ConfigUtils.java index 10e84374..b5b642e8 100644 --- a/Common/src/main/java/com/wuweibi/bullet/ConfigUtils.java +++ b/Common/src/main/java/com/wuweibi/bullet/ConfigUtils.java @@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject; import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.*; @@ -15,24 +17,27 @@ * @create 2017-12-06 下午9:43 **/ public class ConfigUtils { + /** 日志 */ + private static Logger logger = LoggerFactory.getLogger(ConfigUtils.class); static JSONObject CONF ; static { String result = "{}"; try { - InputStream inputStream = new FileInputStream("./conf/config.json"); + File file = new File("./conf/config.json"); + System.out.println(file.getAbsoluteFile()); + InputStream inputStream = new FileInputStream(file); // InputStream inputStream = ConfigUtils.class.getResourceAsStream("/config.json"); result = IOUtils.toString(inputStream, "UTF-8"); + logger.debug("{}", result); } catch (IOException e) { - e.printStackTrace(); + logger.error("", e); } - - CONF = JSONObject.parseObject(result); } diff --git a/Common/src/main/java/com/wuweibi/bullet/SocketUtils.java b/Common/src/main/java/com/wuweibi/bullet/SocketUtils.java index 3a21ad25..438cba0b 100644 --- a/Common/src/main/java/com/wuweibi/bullet/SocketUtils.java +++ b/Common/src/main/java/com/wuweibi/bullet/SocketUtils.java @@ -42,33 +42,24 @@ public static byte[] getInputStreamBytes(Socket socket) throws IOException { * @return * @throws IOException */ - public static byte[] receiveData(SocketChannel socketChannel) { + public static byte[] receiveData(SocketChannel socketChannel) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 缓冲区 ByteBuffer buffer = ByteBuffer.allocate(1024); - try { - byte[] bytes; - int size = 0; - int count = 0; - while ((size = socketChannel.read(buffer)) >= 0) { - buffer.flip(); - bytes = new byte[size]; - buffer.get(bytes); - baos.write(bytes); - buffer.clear(); - count++; - } - } catch (IOException e) { - logger.error("", e); - } finally { - try { -// socketChannel.shutdownInput(); -// socketChannel.close(); - } catch (Exception ex) { - } + byte[] bytes; + int size = 0; + int count = 0; + while ((size = socketChannel.read(buffer)) >= 0) { + buffer.flip(); + bytes = new byte[size]; + buffer.get(bytes); + baos.write(bytes); + buffer.clear(); + count++; } + return baos.toByteArray(); }