其他类已经测试过。没有问题了,server端可以接受到数据,并正常发送。问题好象出在SocketConnect类中的get方法中,输出流似乎被阻塞了。请高手帮忙看看。 public class OrderMessage {
private int total_Length = 32;
private String OrderID;
private String CardNum;
private int Fee;
private String responseCD;
private static OrderMessage orderObj = null;
public String getCardNum() {
return CardNum;
}
public void setCardNum(String cardNum) {
CardNum = cardNum;
}
public int getFee() {
return Fee;
}
public void setFee(int fee) {
Fee = fee;
}
public String getOrderID() {
return OrderID;
}
public void setOrderID(String orderID) {
OrderID = orderID;
}
public String getResponseCD() {
return responseCD;
}
public void setResponseCD(String responseCD) {
this.responseCD = responseCD;
}
public int getTotal_Length() {
return total_Length;
} public byte[] getByteArray(){
byte[] JPSByte = new byte[total_Length];
System.arraycopy(Tools.int2byte(total_Length),0,JPSByte,0,4);
System.arraycopy(OrderID.getBytes(),0,JPSByte,4,8);
System.arraycopy(CardNum.getBytes(),0,JPSByte,12,16);
System.arraycopy(Tools.int2byte(Fee),0,JPSByte,28,4);
return JPSByte;
}
}
==================================================
//执行客户端
import java.util.Iterator;
public class TestClient extends Thread
{
public static CheckInThread InThread ;
public static CheckOutThread outThread ;
private TestClient()
{
if(InThread==null)
{
InThread = new CheckInThread();
InThread.start();
}
if(outThread==null)
{
outThread = new CheckOutThread();
outThread.start();
}
}
@SuppressWarnings("unchecked")
public void run()
{
OrderMessage msgOrg = new OrderMessage();
OrderMessage msgOutObj= new OrderMessage(); msgOrg.setOrderID("80000000");
msgOrg.setCardNum("1600000000000000");
msgOrg.setFee(10);
ArrayListInOut.ArrayListIn().add(msgOrg); Iterator i = ArrayListInOut.ArrayListOut().iterator();
while(i.hasNext())
{
msgOutObj = (OrderMessage)i.next();
System.out.println("返回值是"+msgOutObj.getResponseCD());
i.remove();
ArrayListInOut.ArrayListOut().remove(msgOutObj);
}
}
public static void main(String [] args)
{
try {new TestClient().start();} catch (Exception e) {e.printStackTrace();}
}
} ===================================================
public class CheckOutThread extends Thread {
public CheckOutThread(){
System.out.println("启动监视线程OutThread.....");
}
//监视输出队列,当服务器发送Socket输出流后,就将结果保存到该队列中。
public static String backValues = null;
public void run(){
while(true)
{
try {
backValues = SocketConnect.getConnect().get();
System.out.println("返回值是"+backValues);
if(null == backValues)
{
continue;
}
OrderMessage msgOutObj = new OrderMessage();
//如果检测到输入流中有数据就将数据取出来并保存到队列
msgOutObj.setResponseCD(backValues);
ArrayListInOut.ArrayListOut().add(msgOutObj);
}catch (Exception e) {
e.printStackTrace();
}
}
}
} ================================================== import java.util.*; public class CheckInThread extends Thread /* implements Runnable*/{
public CheckInThread(){
System.out.println("启动监视线程InThread......");
}
//监视输入队列,当队列中有数据的时候就发送Socket
public void run()
{
while(true)
{
if(ArrayListInOut.ArrayListIn().isEmpty()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
Iterator i = ArrayListInOut.ArrayListIn().iterator();
while(i.hasNext())
{
OrderMessage msgInObj = null;
//将对象的参数发送到服务器端
try {
msgInObj = (OrderMessage)i.next();
System.out.println("发送数据:"+msgInObj.getOrderID()+","+msgInObj.getCardNum()+","+msgInObj.getFee());
SocketConnect.getConnect().send(msgInObj);
} catch (Exception e) {
e.printStackTrace();
}
i.remove();//删除迭代器
ArrayListInOut.ArrayListIn().remove(msgInObj);//删除队列记录
}
}
}
}
private int total_Length = 32;
private String OrderID;
private String CardNum;
private int Fee;
private String responseCD;
private static OrderMessage orderObj = null;
public String getCardNum() {
return CardNum;
}
public void setCardNum(String cardNum) {
CardNum = cardNum;
}
public int getFee() {
return Fee;
}
public void setFee(int fee) {
Fee = fee;
}
public String getOrderID() {
return OrderID;
}
public void setOrderID(String orderID) {
OrderID = orderID;
}
public String getResponseCD() {
return responseCD;
}
public void setResponseCD(String responseCD) {
this.responseCD = responseCD;
}
public int getTotal_Length() {
return total_Length;
} public byte[] getByteArray(){
byte[] JPSByte = new byte[total_Length];
System.arraycopy(Tools.int2byte(total_Length),0,JPSByte,0,4);
System.arraycopy(OrderID.getBytes(),0,JPSByte,4,8);
System.arraycopy(CardNum.getBytes(),0,JPSByte,12,16);
System.arraycopy(Tools.int2byte(Fee),0,JPSByte,28,4);
return JPSByte;
}
}
==================================================
//执行客户端
import java.util.Iterator;
public class TestClient extends Thread
{
public static CheckInThread InThread ;
public static CheckOutThread outThread ;
private TestClient()
{
if(InThread==null)
{
InThread = new CheckInThread();
InThread.start();
}
if(outThread==null)
{
outThread = new CheckOutThread();
outThread.start();
}
}
@SuppressWarnings("unchecked")
public void run()
{
OrderMessage msgOrg = new OrderMessage();
OrderMessage msgOutObj= new OrderMessage(); msgOrg.setOrderID("80000000");
msgOrg.setCardNum("1600000000000000");
msgOrg.setFee(10);
ArrayListInOut.ArrayListIn().add(msgOrg); Iterator i = ArrayListInOut.ArrayListOut().iterator();
while(i.hasNext())
{
msgOutObj = (OrderMessage)i.next();
System.out.println("返回值是"+msgOutObj.getResponseCD());
i.remove();
ArrayListInOut.ArrayListOut().remove(msgOutObj);
}
}
public static void main(String [] args)
{
try {new TestClient().start();} catch (Exception e) {e.printStackTrace();}
}
} ===================================================
public class CheckOutThread extends Thread {
public CheckOutThread(){
System.out.println("启动监视线程OutThread.....");
}
//监视输出队列,当服务器发送Socket输出流后,就将结果保存到该队列中。
public static String backValues = null;
public void run(){
while(true)
{
try {
backValues = SocketConnect.getConnect().get();
System.out.println("返回值是"+backValues);
if(null == backValues)
{
continue;
}
OrderMessage msgOutObj = new OrderMessage();
//如果检测到输入流中有数据就将数据取出来并保存到队列
msgOutObj.setResponseCD(backValues);
ArrayListInOut.ArrayListOut().add(msgOutObj);
}catch (Exception e) {
e.printStackTrace();
}
}
}
} ================================================== import java.util.*; public class CheckInThread extends Thread /* implements Runnable*/{
public CheckInThread(){
System.out.println("启动监视线程InThread......");
}
//监视输入队列,当队列中有数据的时候就发送Socket
public void run()
{
while(true)
{
if(ArrayListInOut.ArrayListIn().isEmpty()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
Iterator i = ArrayListInOut.ArrayListIn().iterator();
while(i.hasNext())
{
OrderMessage msgInObj = null;
//将对象的参数发送到服务器端
try {
msgInObj = (OrderMessage)i.next();
System.out.println("发送数据:"+msgInObj.getOrderID()+","+msgInObj.getCardNum()+","+msgInObj.getFee());
SocketConnect.getConnect().send(msgInObj);
} catch (Exception e) {
e.printStackTrace();
}
i.remove();//删除迭代器
ArrayListInOut.ArrayListIn().remove(msgInObj);//删除队列记录
}
}
}
}
续
package com.vesta.jpayment.msg.handler.socket;
import java.util.ArrayList;
public class ArrayListInOut {
private static ArrayList alIn = new ArrayList();
private static ArrayList alOut = new ArrayList();
//输入队列
public static ArrayList ArrayListIn()
{
return alIn;
}
//输出队列
public static ArrayList ArrayListOut()
{
return alOut;
}
}
=============================================
public class Tools { public static byte[] int2byte(int n)
{
byte b[] = new byte[4];
b[0] = (byte)(n >> 24);
b[1] = (byte)(n >> 16);
b[2] = (byte)(n >> 8);
b[3] = (byte)n;
return b;
} public static int byte2int(byte b[], int offset){
return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8 | (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
} 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;
} public static long byte2long(byte b[]){
return (long)b[7] & (long)255 | ((long)b[6] & (long)255) << 8 | ((long)b[5] & (long)255) << 16 | ((long)b[4] & (long)255) << 24 | ((long)b[3] & (long)255) << 32 | ((long)b[2] & (long)255) << 40 | ((long)b[1] & (long)255) << 48 | (long)b[0] << 56;
}
} =================================================== import java.net.*;
import java.io.*;
public class SocketConnect
{
public final static String SERVER_IP="127.0.0.1";//服务器端IP地址
public final static int SERVER_PORT = 7890;//端口号
private Socket s = null;
private static SocketConnect ss = null;
private SocketConnect()
{
try {
s = new Socket(SocketConnect.SERVER_IP,SocketConnect.SERVER_PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static SocketConnect getConnect(){
if(ss == null){
ss = new SocketConnect();
}
return ss;
} public void send(OrderMessage msgObj)
{
try{
OutputStream ops = s.getOutputStream();
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ops));
DataOutputStream dos = new DataOutputStream(ops);
dos.write(msgObj.getByteArray());
dos.flush();
//dos.close();
//s.close();
}catch(Exception e){
e.printStackTrace();
}
}
public String get()
{
String backString ="";
try {
InputStream ips = s.getInputStream();
// DataInputStream dis = new DataInputStream(ips);
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
backString = br.readLine();
//br.close();
//s.close();
return backString;
} catch (Exception e) {
e.printStackTrace();
}
return backString;
}
} ======================================================
//服务器端程序
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class BankServer{ private ServerSocketChannel serverSC;
private Selector selector; public BankServer(){
try {
serverSC = ServerSocketChannel.open();
serverSC.configureBlocking(false);
// serverSC.socket().bind(new
// InetSocketAddress(InetAddress.getLocalHost(),7890));
serverSC.socket().bind(new
InetSocketAddress(SocketConnect.SERVER_IP,SocketConnect.SERVER_PORT));
} catch (IOException e) {
e.printStackTrace();
}
} public void StartServer() {
try {
selector = Selector.open();
serverSC.register(selector,SelectionKey.OP_ACCEPT); while(true){
//System.out.println("服务器在等待");
if(selector.select() == 0){
continue;
}
Iterator iterator = selector.selectedKeys().iterator();
while(iterator.hasNext()){
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
if((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT){
ServerSocketChannel readySSC = (ServerSocketChannel) key.channel();
SocketChannel sc = readySSC.accept().socket().getChannel();
sc.configureBlocking(false);
sc.register(selector,SelectionKey.OP_READ);
System.out.println("已经与服务器连接!");
}else if((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ){
SocketChannel sc = (SocketChannel)key.channel();
while (true) {
//读取总长度
ByteBuffer buffer = ByteBuffer.allocate( 4 );
buffer.clear();
int r = sc.read( buffer );
if (r<=0) {
break;
}
buffer.flip();
int byteLength = byte2int(buffer.array(),0);
//读取消息体
buffer = ByteBuffer.allocate( byteLength );
buffer.clear();
r = sc.read( buffer );
if (r<=0) {
break;
}
buffer.flip();
StringBuffer msg = new StringBuffer();
byte[] tmpByte = new byte[8];
System.arraycopy(buffer.array(),0,tmpByte,0,8);
msg.append(new String(tmpByte)).append(",");
tmpByte = new byte[16];
System.arraycopy(buffer.array(),8,tmpByte,0,16);
msg.append(new String(tmpByte)).append(",");
tmpByte = new byte[4];
System.arraycopy(buffer.array(),24,tmpByte,0,4);
msg.append(byte2int(tmpByte,0)); System.out.println(msg.toString());
//System.out.println("读取数据完成!");
ByteBuffer writeBuffer = ByteBuffer.allocate(4);
System.arraycopy(int2byte(1),0,writeBuffer.array(),0,4);
writeBuffer.flip();
sc.write(writeBuffer);
System.out.println(writeBuffer.array());
} }else if((key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE){
System.out.println("写入数据完成!");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} public static int byte2int(byte b[], int offset){
return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8 | (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
} public static byte[] int2byte(int n)
{
byte b[] = new byte[4];
b[0] = (byte)(n >> 24);
b[1] = (byte)(n >> 16);
b[2] = (byte)(n >> 8);
b[3] = (byte)n;
return b;
} public static void main(String[] args){
new BankServer().StartServer();
}
}