DataOutputStream.write not returns

package com.gtd.sms.cutc;

import java.io.*;

import java.net.Socket;

import java.nio.*;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;

import com.gtd.sms.cutc.sgip.*;

import com.gtd.sms.cutc.log.Loger;

public class SocketManager {

/** SP作为客户端发起请求的Socket */

public static Socket socket;

/** 与客户端Socket绑定的输入流 */

public static DataInputStream in;

/** 与客户端Socket绑定的输出流 */

public static DataOutputStream out;

/** 该值指示是否应该关闭SP发起的客户端Socket */

public static boolean disConFlag = true;

/** 用于超时计算 */

public static long startTime = System.currentTimeMillis() / 1000L;

/** 用于超时计算 */

public static long endTime = System.currentTimeMillis() / 1000L;

/**

* 同步客户端Socket读写

*/

public static DataOutputStream getClientOutput() {

return out;

}

/**

* SocketManager

*/

public SocketManager() {

try {

jbInit();

} catch (Exception ex) {

ex.printStackTrace();

Loger.loger.writeLog(ex);

}

}

/**

* 建立SP客户端Socket

*

* @param ip String

* @param port int

* @return boolean

* @throws RunException

*/

public static boolean getConnection(String ip, int port) throws

RunException {

boolean rs = false;

try {

socket = new Socket(ip, port);

socket.setSoTimeout(2000);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

rs = true;

} catch (Exception e) {

e.printStackTrace();

Loger.loger.writeLog(e);

}

return rs;

}

/**

* 字符串长度不足时,用'/0'补齐

*

* @param str String

* @param num int

* @return byte[]

*/

private static byte[] putString(String str, int num) {

StringBuffer bf = new StringBuffer(str);

for (int i = 1; i <= num - str.getBytes().length; i++) {

bf = bf.append('\0');

}

String str_re = bf.toString();

return str_re.getBytes();

}

/**

* 生成SGIP协议头

*

* @param head PackageHead

* @param totle int

* @return byte[]

*/

private static byte[] putHead(PackageHead head, int totle) {

ByteBuffer bb = MappedByteBuffer.allocate(20);

try {

bb.putInt(totle);

bb.putInt(head.getCommand_id());

if (head.getSequence_one() != 0) {

bb.put(getSequece_one(head.getSequence_one()));

} else {

bb.put(getSequece_one());

}

bb.putInt(head.getSequence_two());

bb.putInt(head.getSequence_three());

} catch (Exception e) {

System.err.println(e.getMessage());

e.printStackTrace();

Loger.loger.writeLog(e);

}

return bb.array();

}

/**

* 返回状态报告响应(Report_resp)

*

* @param out DataOutputStream

* @param head PackageHead

* @param report_resp Report_Resp

* @return boolean

* @throws RunException

*/

public static boolean writeReport_Res(DataOutputStream out,

PackageHead head,

Report_Resp report_resp) throws

RunException {

boolean rs = true;

int totalSize = 29;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(report_resp.getResult());

bb.put(putString(report_resp.getReserve(), 8));

try {

out.write(bb.array());

out.flush();

} catch (IOException e) {

rs = false;

System.err.println(e.getMessage());

e.printStackTrace();

Loger.loger.writeLog(e);

}

return rs;

}

/**

* 返回绑定响应(Bind_resp)

*

* @param out DataOutputStream

* @param head PackageHead

* @param bind_resp Bind_Resp

* @return boolean

* @throws RunException

*/

public static boolean writeBind_Res(DataOutputStream out, PackageHead head,

Bind_Resp bind_resp) throws

RunException {

boolean rs = true;

int totalSize = 29;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(bind_resp.getResult());

bb.put(putString(bind_resp.getReserve(), 8));

try {

out.write(bb.array());

out.flush();

} catch (IOException e) {

rs = false;

System.err.println(e.toString());

e.printStackTrace();

Loger.loger.writeLog(e);

}

return rs;

}

/**

* 发起解除连接请求(Unbind)

*

* @param out DataOutputStream

* @param head PackageHead

* @return boolean

* @throws RunException

*/

public static boolean writeUnbind(DataOutputStream out, PackageHead head) throws

RunException {

boolean rs = true;

int totalSize = 20;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

try {

out.write(bb.array());

out.flush();

} catch (IOException e) {

rs = false;

Loger.loger.writeLog(e);

throw new RunException("writeUnbind is error:" +

e.getMessage());

}

return rs;

}

/**

* 返回接收响应(Deliver_resp)

*

* @param out DataOutputStream

* @param head PackageHead

* @param deliver_resp Deliver_Resp

* @return boolean

* @throws RunException

*/

public static boolean writeDeliver_Res(DataOutputStream out,

PackageHead head,

Deliver_Resp deliver_resp) throws

RunException {

boolean rs = true;

int totalSize = 29;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(deliver_resp.getResult());

bb.put(putString(deliver_resp.getReserve(), 8));

try {

out.write(bb.array());

out.flush();

} catch (IOException e) {

rs = false;

Loger.loger.writeLog(e);

throw new RunException("writeDeliver_Res is error:" +

e.getMessage());

}

return rs;

}

/**

* 发起建立连接请求(Bind)

*

* @param out DataOutputStream

* @param head PackageHead

* @param bind Bind

* @return boolean

* @throws RunException

*/

public static boolean writeBind(DataOutputStream out, PackageHead head,

Bind bind) throws RunException {

boolean rs = true;

int totalSize = 61;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(bind.getLogin_Type());

bb.put(putString(bind.getLogin_Name(), 16));

bb.put(putString(bind.getLogin_Password(), 16));

bb.put(putString(bind.getReseve(), 8));

try {

out.write(bb.array());

out.flush();

} catch (IOException e) {

rs = false;

Loger.loger.writeLog(e);

throw new RunException("writeBind is error:" + e.getMessage());

}

return rs;

}

/**

* 发送消息(Submit)

*

* @param out DataOutputStream

* @param head PackageHead

* @param submit Submit

* @return boolean

* @throws RunException

*/

public static boolean writeSubmit(DataOutputStream out, PackageHead head,

Submit submit) throws RunException {

boolean rs = true;

int totalSize = 164 + submit.getMessageLength();

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(putString(submit.getSPNumber(), 21));

bb.put(putString(submit.getChargerNumber(), 21));

bb.put(submit.getUserCount());

bb.put(putString(submit.getUserNumber(), 21));

bb.put(putString(submit.getCorpId(), 5));

bb.put(putString(submit.getServiceType(), 10));

bb.put(submit.getFeeType());

bb.put(putString(submit.getFeeValue(), 6));

bb.put(putString(submit.getGivenValue(), 6));

bb.put(submit.getAgenFlag());

bb.put(submit.getMorelatetoMTFlag());

bb.put(submit.getPriority());

bb.put(putString(submit.getExpireTime(), 16));

bb.put(putString(submit.getScheduleTime(), 16));

bb.put(submit.getReportFlag());

bb.put(submit.getTP_pid());

bb.put(submit.getTP_udhi());

bb.put(submit.getMessageCoding());

bb.put(submit.getMessageType());

bb.putInt(submit.getMessageLength());

if (submit.getMessageCoding() == 4) {

bb.put(submit.getBinMessageContent());

} else {

bb.put(putString(submit.getMessageContent(),

submit.getMessageLength()));

}

bb.put(putString(submit.getReserve(), 8));

try {

out.write(bb.array());

out.flush();} catch (IOException e) {

rs = false;

throw new RuntimeException(e.getMessage());

}

return rs;

}

/**

* 读取SGIP协议包头

*

* @param bb byte[]

* @return PackageHead

* @throws RunException

*/

public static PackageHead readPackageHead(byte bb[]) throws RunException {

int Command_ID = -1;

int Sequence_one = 0;

int Sequence_two = 0;

int Sequence_three = 0;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

Command_ID = input.readInt();

Sequence_one = input.readInt();

Sequence_two = input.readInt();

Sequence_three = input.readInt();

} catch (Exception e) {

Loger.loger.writeLog(e);

throw new RunException("readPackageHead is error:" +

e.getMessage());

}

PackageHead head = new PackageHead(Command_ID, Sequence_one,

Sequence_two,

Sequence_three);

return head;

}

/**

* 读取状态报告

*

* @param bb byte[]

* @return Report

* @throws RunException

*/

public static Report readReport(byte bb[]) throws RunException {

String submitequenceNumber = "";

String UserNumber = "";

byte ReportType;

byte State;

byte ErrorCode;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

//submitequenceNumber = readString(input, 12, 0);

submitequenceNumber = Long.toString(Long.parseLong(Integer.

toHexString(input.readInt()), 16));

submitequenceNumber = submitequenceNumber +

Long.toString(Long.parseLong(Integer.

toHexString(input.readInt()), 16));

submitequenceNumber = submitequenceNumber +

Long.toString(Long.parseLong(Integer.

toHexString(input.readInt()), 16));

ReportType = readByteFromStream(input, 12);

UserNumber = readString(input, 21, 13);

State = readByteFromStream(input, 34);

ErrorCode = readByteFromStream(input, 35);

} catch (Exception e) {

e.printStackTrace();

Loger.loger.writeLog(e);

throw new RunException("readReport is error:" +

e.getMessage());

}

Report report = new Report(submitequenceNumber, ReportType, UserNumber,

State, ErrorCode);

return report;

}

/**

* 读取绑定信息

*

* @param bb byte[]

* @return Bind

* @throws RunException

*/

public static Bind readBind(byte bb[]) throws RunException {

String Login_Name = "";

byte Login_Type;

String Login_Password;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

Login_Type = input.readByte();

Login_Name = readString(input, 16, 1);

Login_Password = readString(input, 16, 17);

} catch (Exception e) {

Loger.loger.writeLog(e);

throw new RunException("readBind is error:" + e.getMessage());

}

Bind bind = new Bind(Login_Type, Login_Name, Login_Password);

return bind;

}

/**

* 读取MO上行信息

*

* @param bb byte[]

* @return Deliver

* @throws RunException

*/

public static Deliver readDiliver(byte bb[]) throws RunException {

String UserNumber = "";

String SPNumber = "";

int MessageLength = 0;

byte TP_pid;

byte TP_udhi;

byte MessageCoding;

String MessageContent;

String Reserve = null;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

UserNumber = readString(input, 21, 0);

SPNumber = readString(input, 21, 21);

TP_pid = readByteFromStream(input, 42);

TP_udhi = readByteFromStream(input, 43);

MessageCoding = readByteFromStream(input, 44);

MessageLength = readIntFromStream(input, 45);

if (MessageCoding == 8) {

byte b[] = new byte[MessageLength];

int i = 0;

try {

while (i < MessageLength) {

b = input.readByte();

i++;

}

} catch (Exception e) {

e.printStackTrace();

Loger.loger.writeLog(e);

}

Charset gbk = Charset.forName("UTF-16BE");

CharsetDecoder decorder = gbk.newDecoder();

ByteBuffer byteBuffer = ByteBuffer.wrap(b);

CharBuffer charBuffer = null;

charBuffer = decorder.decode(byteBuffer);

MessageContent = new String(charBuffer.array());

} else {

MessageContent = readString(input, MessageLength, 49);

}

try {

Reserve = readString(input, 8, 49 + MessageLength);

int index = Reserve.indexOf('\0');

Reserve = Reserve.substring(0, index);

} catch (Exception e) {

}

} catch (Exception e) {

e.printStackTrace();

Loger.loger.writeLog(e);

throw new RunException("readDiliver is error:" +

e.getMessage());

}

Deliver deliver = new Deliver(UserNumber, SPNumber, TP_pid, TP_udhi,

MessageCoding, MessageLength,

MessageContent,

Reserve);

return deliver;

}

/**

* 读取下行响应(Submit_resp)

*

* @param bb byte[]

* @return Submit_Resp

* @throws RunException

*/

public static Submit_Resp readSubmit_Res(byte bb[]) throws RunException {

byte result;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

result = input.readByte();

} catch (Exception e) {

Loger.loger.writeLog(e);

throw new RunException("readSubmit_Res is error:" +

e.getMessage());

}

Submit_Resp submit_resp = new Submit_Resp(result);

return submit_resp;

}

/**

* 读取绑定响应

*

* @param bb byte[]

* @return Bind_Resp

* @throws RunException

*/

public static Bind_Resp readBind_Res(byte bb[]) throws RunException {

byte result;

try {

DataInputStream input = new DataInputStream(new

ByteArrayInputStream(bb));

result = input.readByte();

} catch (Exception e) {

Loger.loger.writeLog(e);

throw new RunException("readBind_Res is error:" +

e.getMessage());

}

Bind_Resp bind_resp = new Bind_Resp(result);

return bind_resp;

}

/**

* 从流中读取整数(4个字节)

*

* @param in DataInputStream

* @param start int

* @return int

*/

public static int readIntFromStream(DataInputStream in, int start) {

int bb = -1;

try {

bb = in.readInt();

} catch (Exception e) {

Loger.loger.writeLog(e);

System.err.println(e.toString());

}

return bb;

}

/**

* 从流中读取一个字节

*

* @param in DataInputStream

* @param start int

* @return byte

*/

public static byte readByteFromStream(DataInputStream in, int start) {

byte bb = -1;

try {

bb = in.readByte();

} catch (Exception e) {

Loger.loger.writeLog(e);

System.err.println(e.toString());

}

return bb;

}

/**

* 从流中读取字符串(按字节多次读取)

*

* @param in DataInputStream

* @param num int

* @param start int

* @return String

*/

public static String readString(DataInputStream in, int num, int start) {

byte b[] = new byte[200];

int i = 0;

try {

while (i < num) {

b = in.readByte();

i++;

}

} catch (Exception e) {

System.err.println(e.toString());

Loger.loger.writeLog(e);

}

String str = new String(b, 0, i);

return str;

}

/**

* 读取消息包,忽略指定字节数

*

* @param in DataInputStream

* @param num int

* @return byte[]

* @throws RunException

*/

public static byte[] readInfoForFour(DataInputStream in, int num) throws

RunException {

byte receivedData[] = null;

try {

if (in.available() >= 20) {

int dataSize = in.readInt();

receivedData = new byte[dataSize - num];

int dataTotalSizeToRead = dataSize - num;

int dataThisTimeRead;

for (int dataToReadLeft = dataSize - num; dataToReadLeft > 0;

dataToReadLeft -= dataThisTimeRead) {

dataThisTimeRead = in.read(receivedData,

dataTotalSizeToRead -

dataToReadLeft,

dataToReadLeft);

}

}

} catch (Exception e) {

System.err.println(e.toString());

Loger.loger.writeLog(e);

new RunException(e.getMessage());

}

return receivedData;

}

/**

* 读取消息包,忽略指定字节数

*

* @param in DataInputStream

* @param num int

* @param totalLenth int

* @return byte[]

* @throws RunException

*/

public static byte[] readInfoForFour(DataInputStream in, int num,

int totalLenth) throws RunException {

byte receivedData[] = null;

try {

int dataSize = totalLenth;

receivedData = new byte[dataSize - num];

int dataTotalSizeToRead = dataSize - num;

int dataThisTimeRead;

for (int dataToReadLeft = dataSize - num; dataToReadLeft > 0;

dataToReadLeft -= dataThisTimeRead) {

dataThisTimeRead = in.read(receivedData,

dataTotalSizeToRead - dataToReadLeft,

dataToReadLeft);

}

} catch (Exception e) {

System.err.println(e.toString());

Loger.loger.writeLog(e);

new RunException(e.getMessage());

}

return receivedData;

}

/**

* 长整型转为字节流

*

* @param n long

* @return byte[]

*/

public static byte[] long2byte(long n) {

byte b[] = new byte[8];

b[0] = (byte) (int) (n >> 56);

b[1] = (byte) (int) (n >> 48);

b[2] = (byte) (int) (n >> 40);

b[3] = (byte) (int) (n >> 32);

b[4] = (byte) (int) (n >> 24);

b[5] = (byte) (int) (n >> 16);

b[6] = (byte) (int) (n >> 8);

b[7] = (byte) (int) n;

return b;

}

/**

* 取得第一段序列号

*

* @return byte[]

*/

public static byte[] getSequece_one() {

long one = Long.parseLong(CommandNum.sp_serial);

byte bb[] = long2byte(one);

byte rs[] = new byte[4];

System.arraycopy(bb, 4, rs, 0, 4);

return rs;

}

public static byte[] getSequece_one(long sequence) {

byte bb[] = long2byte(sequence);

byte rs[] = new byte[4];

System.arraycopy(bb, 4, rs, 0, 4);

return rs;

}

/**

* jbInit

*

* @throws Exception

*/

private void jbInit() throws Exception {

}

}

[20989 byte] By [yanzy1981a] at [2007-10-1 1:06:35]
# 1

public static boolean writeReport_Res(DataOutputStream out,

PackageHead head,

Report_Resp report_resp) throws

RunException {

boolean rs = true;

int totalSize = 29;

ByteBuffer bb = MappedByteBuffer.allocate(totalSize);

bb.put(putHead(head, totalSize));

bb.put(report_resp.getResult());

bb.put(putString(report_resp.getReserve(), 8));

try {

/*****************not return*******************/

out.write(bb.array());

out.flush();

/*****************not return end*******************/

} catch (IOException e) {

rs = false;

System.err.println(e.getMessage());

e.printStackTrace();

Loger.loger.writeLog(e);

}

return rs;

}

yanzy1981a at 2007-7-8 1:24:23 > top of Java-index,Archived Forums,Socket Programming...
# 2
The write will block if the client does not read the data. Once the buffered on the client and server have filled up, write must block or it lose data.
Peter-Lawreya at 2007-7-8 1:24:23 > top of Java-index,Archived Forums,Socket Programming...
# 3
thank you. May I set up for one overtime when writing?
yanzy1981a at 2007-7-8 1:24:23 > top of Java-index,Archived Forums,Socket Programming...