zmt
3 years ago
8 changed files with 336 additions and 12 deletions
-
18app/build.gradle
-
2zmt_module/build.gradle
-
43zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java
-
8zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java
-
53zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java
-
164zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java
-
4zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java
-
56zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java
@ -0,0 +1,43 @@ |
|||||
|
package io.dcloud.zmt_module; |
||||
|
|
||||
|
public class MyThread extends Thread { |
||||
|
String TAG = "zmt---"; |
||||
|
public void run(){ |
||||
|
System.out.println("MyThread thread running..."); |
||||
|
|
||||
|
TcpServer server = new TcpServer(); |
||||
|
server.initSocket((Integer.parseInt("16666"))); |
||||
|
|
||||
|
while (true){ |
||||
|
String message = server.receiveMessage(SocketConfig.UTF_8); |
||||
|
System.out.println(TAG+"接收消息:"+message); |
||||
|
|
||||
|
String send_msg = "收到消息了,你说的对!"; |
||||
|
|
||||
|
send_msg = SocketHelper.getMessageByReadLine(send_msg); |
||||
|
server.sendMessage(send_msg,SocketConfig.UTF_8); |
||||
|
try { |
||||
|
Thread.sleep(1000); |
||||
|
} catch (InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// while (true){ |
||||
|
// String message = server.receiveMessage(SocketConfig.UTF_8); |
||||
|
// System.out.println(TAG+"接收消息:"+message); |
||||
|
// |
||||
|
// String send_msg = "101010"; |
||||
|
// |
||||
|
// send_msg = SocketHelper.getMessageByReadLine(send_msg); |
||||
|
// server.sendMessage(send_msg,SocketConfig.UTF_8); |
||||
|
// try { |
||||
|
// Thread.sleep(1000); |
||||
|
// } catch (InterruptedException e) { |
||||
|
// e.printStackTrace(); |
||||
|
// } |
||||
|
// |
||||
|
// } |
||||
|
} |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package io.dcloud.zmt_module; |
||||
|
|
||||
|
public class SocketConfig { |
||||
|
|
||||
|
// 设置编码 |
||||
|
public static final String UTF_8 = "UTF-8"; |
||||
|
public static final String GBK = "GBK"; |
||||
|
} |
@ -0,0 +1,53 @@ |
|||||
|
package io.dcloud.zmt_module; |
||||
|
|
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public class SocketHelper { |
||||
|
public static String getMessageByReadLine(String message) { |
||||
|
message = message + "\n"; |
||||
|
return message; |
||||
|
} |
||||
|
|
||||
|
public static byte[] toByteArray(InputStream in) throws IOException { |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
byte[] buffer = new byte[1024 * 4]; |
||||
|
int n = 0; |
||||
|
while ((n = in.read(buffer)) != -1) { |
||||
|
out.write(buffer, 0, n); |
||||
|
} |
||||
|
return out.toByteArray(); |
||||
|
} |
||||
|
|
||||
|
public static byte[] toIntByteArray(InputStream in) throws IOException { |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
byte[] buffer = new byte[100]; |
||||
|
|
||||
|
System.out.println("zmt-toIntByteArray--" + buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + " " + Arrays.toString(buffer)); |
||||
|
int n = 0; |
||||
|
while ((n = in.read(buffer)) != -1) { |
||||
|
out.write(buffer, 0, n); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
return out.toByteArray(); |
||||
|
} |
||||
|
|
||||
|
public static String byte2hex(byte[] bytes) { |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
String tmp = null; |
||||
|
for (byte b : bytes) { |
||||
|
//将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制 |
||||
|
tmp = Integer.toHexString(0xFF & b); |
||||
|
if (tmp.length() == 1) { |
||||
|
tmp = "0" + tmp; |
||||
|
} |
||||
|
sb.append(tmp); |
||||
|
|
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,164 @@ |
|||||
|
package io.dcloud.zmt_module; |
||||
|
|
||||
|
import java.io.BufferedInputStream; |
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.io.OutputStream; |
||||
|
import java.io.OutputStreamWriter; |
||||
|
import java.io.PrintStream; |
||||
|
import java.io.PrintWriter; |
||||
|
import java.net.InetAddress; |
||||
|
import java.net.ServerSocket; |
||||
|
import java.net.Socket; |
||||
|
import java.nio.charset.Charset; |
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public class TcpServer { |
||||
|
// 私有变量 |
||||
|
private String TAG = "zmt---"; |
||||
|
private ServerSocket mServerSocket; |
||||
|
// private DatagramSocket mServerSocket; |
||||
|
private Socket mServer; |
||||
|
|
||||
|
private OutputStream mOutStream; |
||||
|
private PrintWriter mPrinter; |
||||
|
|
||||
|
private InputStream mInputStream; |
||||
|
private BufferedInputStream mBufferedInputStream; |
||||
|
private InputStreamReader mInputStreamReader; |
||||
|
private BufferedReader mBufferedReader; |
||||
|
|
||||
|
/** |
||||
|
* 建立socket server |
||||
|
* @param port 端口号:0-65535 |
||||
|
*/ |
||||
|
public void initSocket(int port){ |
||||
|
System.out.println(TAG+"initSocket"); |
||||
|
try{ |
||||
|
if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; } |
||||
|
|
||||
|
if (mServerSocket==null){ |
||||
|
mServerSocket = new ServerSocket(port); |
||||
|
mServerSocket.setReuseAddress(true); |
||||
|
|
||||
|
// mServerSocket.bind(new InetSocketAddress(port)); |
||||
|
InetAddress inetAddress = InetAddress.getLocalHost(); //本机地址 |
||||
|
System.out.println(TAG+"tcp server 建立完成:host name"+inetAddress.getHostName()+"\n ---host address:" + |
||||
|
inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress()+" 端口:"+port); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// if(mServerSocket==null){ |
||||
|
// //自己IP收信 |
||||
|
//// IPAddress ip = IPAddress.Parse("192.168.1.96"); |
||||
|
// |
||||
|
//// String ip = "192.168.1.96"; |
||||
|
// InetAddress ip = InetAddress.getByName("192.168.1.96"); |
||||
|
// |
||||
|
// mServerSocket = new ServerSocket(port); |
||||
|
// mServerSocket.bind(new IPEndPoint(ip,port)); |
||||
|
// |
||||
|
// Socket mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
||||
|
// m_serverListenSocket.Bind(new IPEndPoint(ip, 10035)); |
||||
|
// m_serverListenSocket.Listen(100); |
||||
|
// } |
||||
|
}catch (IOException e){ |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 接收消息 |
||||
|
public String receiveMessage(String charsetName){ |
||||
|
String receiveData=null; |
||||
|
String readLine=null; |
||||
|
|
||||
|
// if(mServerSocket==null){ |
||||
|
// System.out.println(TAG+"Socket 未启动"); |
||||
|
// return null; |
||||
|
// } |
||||
|
System.out.println(TAG+"tcp server receiveMessage ing..."); |
||||
|
try{ |
||||
|
// 获取输入信息 |
||||
|
mServer = mServerSocket.accept(); |
||||
|
mInputStream = mServer.getInputStream(); |
||||
|
|
||||
|
byte[] data = SocketHelper.toByteArray(mInputStream); |
||||
|
|
||||
|
// String _rs = new String(data, "gbk"); |
||||
|
|
||||
|
// System.out.println(TAG+"二进制读取结果:"+data+" - "+_rs +" - "+SocketHelper.toIntByteArray(mInputStream) + Arrays.toString(data)) ; |
||||
|
|
||||
|
System.out.println(TAG+SocketHelper.byte2hex(data)); |
||||
|
receiveData = SocketHelper.byte2hex(data); |
||||
|
// 读取输入流 |
||||
|
// charsetName = null; |
||||
|
// if(charsetName!=null&&charsetName!=""){ |
||||
|
// mInputStreamReader = new InputStreamReader(mInputStream, Charset.forName(charsetName)); |
||||
|
// }else{ |
||||
|
// mInputStreamReader = new InputStreamReader(mInputStream); |
||||
|
// } |
||||
|
|
||||
|
// mBufferedReader = new BufferedReader(mInputStreamReader); |
||||
|
// readLine = mBufferedReader.readLine(); |
||||
|
// while (readLine!=null&&readLine!=""){ |
||||
|
// System.out.println(TAG+"读取结果:"+readLine); |
||||
|
// break; |
||||
|
// } |
||||
|
System.out.println(TAG+"接收结束,数据:"+receiveData); |
||||
|
|
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return receiveData; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// 发送消息 |
||||
|
public void sendMessage(String message,String charsetName){ |
||||
|
if(mServer != null){ |
||||
|
System.out.println(TAG+"sendMessage ing..."); |
||||
|
|
||||
|
try { |
||||
|
mOutStream = mServer.getOutputStream(); |
||||
|
mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName))); |
||||
|
mPrinter.println(message); |
||||
|
mPrinter.flush(); |
||||
|
|
||||
|
System.out.println(TAG+"发送成功 :"+message); |
||||
|
} catch (IOException e) { |
||||
|
System.out.println(TAG+"tcp server sendMessage err :"+message); |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 关闭连接 |
||||
|
public void close(){ |
||||
|
try { |
||||
|
if (mPrinter!=null){ |
||||
|
mPrinter.close(); |
||||
|
} |
||||
|
if (mOutStream!=null){ |
||||
|
mOutStream.close(); |
||||
|
} |
||||
|
if (mInputStream!=null){ |
||||
|
mInputStream.close(); |
||||
|
} |
||||
|
if (mInputStreamReader!=null){ |
||||
|
mInputStreamReader.close(); |
||||
|
} |
||||
|
if (mBufferedReader!=null){ |
||||
|
mBufferedReader.close(); |
||||
|
} |
||||
|
if (mServer!=null){ |
||||
|
mServer.close(); |
||||
|
} |
||||
|
System.out.println(TAG+"关闭tcp server <------"); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue