package com.louis.net.socket.packet;import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;/**
 * <p>
 * 1.header,传输数据标志,1字节
 * </p>
 * <p>
 * 2.time,发送时间 ,8字节
 * </p>
 * <p>
 * 3.uuid,发送目标,36字节
 * </p>
 * <p>
 * 4.bodyLength,数据长度 ,4字节
 * </p>
 * <p>
 * 5.body,数据内容,长度由bodyLength决定
 * </p>
 * <p>
 * 6.descriptionLength,数据内容描述的长度
 * </p>
 * <p>
 * 7.description,数据内容描述.长度有descriptionLength决定
 * </p>
 * @author louisjiang
 * 
 */
public class Packet { public static int MAX_LENGTH = Integer.MAX_VALUE;// 2147483647 private byte[] header = new byte[1]; private byte[] time = new byte[8];//发送的时间 private byte[] uuid = new byte[36]; private byte[] bodyLength = new byte[4];// 数据长度,最大为2147483647

private byte[] body = new byte[0]; private byte[] descriptionLength = new byte[4];

private byte[] description = new byte[0]; public static byte TEXT = 0x01;

public static byte ATTACHMENT = 0x02;

private static SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd HH:mm:ss");

public Packet() {
time = this.getLongBytes(new Date().getTime());
descriptionLength= getIntegerBytes(0);
}

public byte[] getBytes() {
byte[] bytes = new byte[header.length + time.length + uuid.length + bodyLength.length  + body.length + descriptionLength.length + description.length];
System.arraycopy(header, 0, bytes, 0, header.length);
System.arraycopy(time, 0, bytes, header.length, time.length);
System.arraycopy(uuid, 0, bytes, header.length + time.length, uuid.length);
System.arraycopy(bodyLength, 0, bytes, header.length + time.length + uuid.length, bodyLength.length);
if(body.length != 0){
System.arraycopy(body, 0, bytes, header.length + time.length + uuid.length + bodyLength.length, body.length);
}
System.arraycopy(descriptionLength, 0, bytes, header.length + time.length + uuid.length + bodyLength.length + body.length, descriptionLength.length);
if(description.length != 0){
System.arraycopy(description, 0, bytes, header.length + time.length + uuid.length + bodyLength.length + body.length + descriptionLength.length, description.length);
}
return bytes;
}
public byte[] getIntegerBytes(int i) {
byte[] b = new byte[4];
b[0] = (byte) (i & 0xff);
b[1] = (byte) ((i >> 8) & 0xff);
b[2] = (byte) ((i >> 16) & 0xff);
b[3] = (byte) (i >> 24);
return b;
} public int getInteger(byte[] b) {
int i = (b[0] & 0xff) | ((b[1] << 8) & 0xff00) | ((b[2] << 24) >>> 8)
| (b[3] << 24);
return i;
} public byte[] getLongBytes(long l) {
byte[] b = new byte[8];
b[0] = (byte) (l >> 56);
b[1] = (byte) (l >> 48);
b[2] = (byte) (l >> 40);
b[3] = (byte) (l >> 32);
b[4] = (byte) (l >> 24);
b[5] = (byte) (l >> 16);
b[6] = (byte) (l >> 8);
b[7] = (byte) (l >> 0);
return b;
} public long getLong(byte[] b) {
long l = (((long) b[0] & 0xff) << 56) | (((long) b[1] & 0xff) << 48)
| (((long) b[2] & 0xff) << 40) | (((long) b[3] & 0xff) << 32)
| (((long) b[4] & 0xff) << 24) | (((long) b[5] & 0xff) << 16)
| (((long) b[6] & 0xff) << 8) | (((long) b[7] & 0xff) << 0);
return l;
}


public byte[] getBytesHeader() {
return header;
}

public byte getByteHeader(){
return header[0];
} public void setHeader(byte header) {
this.header[0] = header;
} public byte[] getBytesBodyLength() {
return bodyLength;
}

public int getIntegerBodyLength(){
return getInteger(bodyLength);
} public byte[] getBytesTime() {
return time;
} public String getStringTime(){
return format.format(getLong(time));
} public byte[] getBytesUuid() {
return uuid;
}

public String getStringUuid(){
String uuid = null;
try {
uuid = new String(this.uuid, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return uuid;
} public void setUuid(String uuid) {
this.uuid = uuid.getBytes();
} public byte[] getBytesBody() {
return body = new byte[getInteger(bodyLength)];
} public byte[] getBody(){
return body;
}

public String getStringBody(){
String body = null;
try {
body = new String(this.body, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return body;
}

public void setBody(String body) {
try {
this.body = body.getBytes("UTF-8");
this.bodyLength = getIntegerBytes(this.body.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} public void setBody(byte[] body){
this.body = body;
this.bodyLength = getIntegerBytes(body.length);
} public byte[] getBytesDescription() {
return description;
}

public String getStringDescription() {
String description = null;
try {
description = new String(this.description, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return description;
} public void setDescription(String description) {
try {
this.description = description.getBytes("UTF-8");
this.descriptionLength = getIntegerBytes(this.description.length);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public byte [] getBytesDescriptionLength(){
return descriptionLength;
}

public int getIntegerDescriptionLength(){
return getInteger(this.descriptionLength);
}
}
package com.louis.net.socket;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Collection;import com.louis.net.socket.packet.Packet;public class ServerProcessor implements Runnable { private Server server;
private Socket socket;
private boolean isRunning = false;
private Thread thread;
private String uuid; public ServerProcessor(Server server, Socket socket, String uuid) {
this.server = server;
this.socket = socket;
this.uuid = uuid; } public void start() {
if (socket == null) {
throw new NullPointerException("socket is null");
}
isRunning = true;
thread = new Thread(this);
thread.start();
} public void stop() {
isRunning = false;
try {
thread.interrupt();
socket.close();
server.getProcessors().remove(uuid);
server.minusAcceptNumber();
} catch (IOException e) {
e.printStackTrace();
}
} private void listener() {
try {
InputStream in = socket.getInputStream();
Packet packet = new Packet();
in.read(packet.getBytesHeader());
in.read(packet.getBytesTime());
in.read(packet.getBytesUuid());
in.read(packet.getBytesBodyLength());
if (packet.getIntegerBodyLength() != 0) {
in.read(packet.getBytesBody());
}
in.read(packet.getBytesDescriptionLength());
if (packet.getIntegerDescriptionLength() != 0) {
in.read(packet.getBytesDescription());
}
Collection<ServerProcessor> serverProcessors = server.getServerProcessors();
for (ServerProcessor serverProcessor : serverProcessors) {
serverProcessor.send(packet.getBytes());
}
} catch (IOException e) {
this.stop();
}
} @Override
public void run() {
while (isRunning) {
listener();
}
} public void send(byte[] b) {
try {
OutputStream out = socket.getOutputStream();
System.out.println(b.length);
out.write(b);
out.flush();
} catch (IOException e) {
e.printStackTrace();
this.stop();
}
} public void setServer(Server server) {
this.server = server;
} public Server getServer() {
return server;
} public String getUuid() {
return uuid;
} public void setUuid(String uuid) {
this.uuid = uuid;
}
}当客服端发送的byte数组的长度超过100万的时候,客服端能够成功发送,服务端也能够超过接收,问题就在于接收到数据转发的时候,当前的socket一直在那发送,不停止. 客服端接收到的数据,能够解析出time,但是数据bodyLength为0