sendreceive.java
来自「JAVA实现的SOCKET通讯源码,里面有双方制定的报文标示!」· Java 代码 · 共 78 行
JAVA
78 行
package testDzjs.yhServer.com;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.math.BigInteger;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-4-16
* Time: 19:10:35
* To change this template use File | Settings | File Templates.
*/
public class SendReceive {
/**
* 读取数据
* @param in
* @return
*/
public String receive(BufferedInputStream in) throws IOException {
//读取协议头部分
byte[] head = new byte[16];
in.read(head);
//读取有效数据长度
byte[] length = new byte[4];
System.arraycopy(head, 4, length, 0, 4);
int span = new BigInteger(length).intValue();
byte[] body = new byte[span];
in.read(body);
return new String(body);
}
/**
* 读取数据 电话申报
* @param in
* @return
*/
public String receiveDH(BufferedInputStream in,int length) throws IOException {
// //读取协议头部分
// byte[] head = new byte[16];
// in.read(head);
// //读取有效数据长度
// byte[] length = new byte[4];
// System.arraycopy(head, 4, length, 0, 4);
// int span = new BigInteger(length).intValue();
//
byte[] body = new byte[length];
in.read(body);
return new String(body);
}
/**
* 发送数据
* @param message
* @param out
*/
public void sendByte(byte[] message, BufferedOutputStream out) throws IOException {
//每次发送1024字节
int num = (message.length % 1024 == 0) ? message.length / 1024
: message.length / 1024 + 1;
for (int i = 0; i < num; i++) {
int lenght = (message.length % 1024 != 0 && i == num - 1) ? message.length % 1024
: 1024;
byte[] buffer = new byte[lenght];
System.arraycopy(message, i * 1024, buffer, 0, lenght);
out.write(buffer);
}
out.flush();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?