package com.rbt.janely.socket;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
public class ServerSocket {
public enum MyData {
B001, B002, B003, B004, B005, B006, B007
};
private ServerSocket ss;
private Socket socket;
private PrintWriter out;
int port = 5555;
int len = 0;
String msg = "";
public Socket1() {
try {
ss = new ServerSocket(port);
System.out.println("本地端口号为:" + ss.getLocalPort());
while (true) {
socket = ss.accept();
System.out.println("已建立连接!/nIP==>:" + socket.getInetAddress()
+ "端口號==>:" + socket.getPort());
OutputStream in = new DataOutputStream(socket.getOutputStream());
BufferedReader dout = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
int count = 0;
InputStream is = socket.getInputStream();
byte[] buff = new byte[1024];
while ((count = is.read(buff)) != -1) {
// System.out.println("count===" + count);
msg = new String(buff, 0, count).toString();
// System.out.println("msg.length = " + msg.length());
System.out.println("接收客戶端发送:" + msg);
if (msg.indexOf("B") != -1) {
String cut = msg.substring(0, 4);
if (MyData.B001.toString().equals(cut))
this.getMethod1();
if (MyData.B002.toString().equals(cut))
this.getMethod2();
if(MyData.B003.toString().equals(cut))
this.getMethod3();
}
// 如果客戶端发送的exit;就結束通話
if (msg.equals("exit") || msg.equals("end")) {
out.println("服务器已终止");
System.out.println("客户端已断开");
out.println();
break;
}
}
out.close();
in.close();
dout.close();
socket.close();
}
} catch (BindException b) {
System.out
.println("java.net-BindException.Address already in use:JVM_Bind?");
} catch (NullPointerException npe) {
System.out.println("java.lang-NullPointerException ?");
} catch (StringIndexOutOfBoundsException siof) {
System.out.println("java.lang-String index out of range?");
} catch (IOException ioe) {
System.out.println("io发生异常...");
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// try {
// ss.close();
// } catch (Exception e) {
// System.out.println("serversocket不能关闭...");
// e.printStackTrace();
// }
}
}
/*
* 开卡
*/
//B003000020100319153124numbCustomernumber16card 19PasswordInfo mation13028Info mation104LogNumberXiaocode
//B003YILB20091120105649270112345678901234566013822000515100207j瞇)U?'60138220005510020777=06055201000092500930477276801880217150=0990000500772708603894173010000006050100000000000000000000000000000000000000000001 妃珚 梮
public void getMethod3(){
int len=0;
byte[] by=new byte[227];
byte[] bytesMsg=msg.getBytes();
if(bytesMsg.length!=by.length){
out.print("数据发送错误!!!");
System.out.println("客户端数据发送错误!!!");
}else{
System.arraycopy(bytesMsg,len,by, 0,4);
len += 4;
String vns =new String(by).substring(0,4);// 交易码
System.arraycopy(bytesMsg,len,by, 0,4);
len += 4;
String counter =new String(by).substring(0,4);// 柜员号
System.arraycopy(bytesMsg,len,by, 0,8);
String date =new String(by).substring(0,8);// 交易日期
len += 8;
System.arraycopy(bytesMsg,len,by, 0,6);
String time =new String(by).substring(0,6);// 交易时间
len += 6;
System.arraycopy(bytesMsg,len,by, 0,4);
String hang =new String(by).substring(0,4);// 行号
len += 4;
System.arraycopy(bytesMsg,len,by, 0,16);
String clientno =new String(by).substring(0,16);// 客户号
len += 16;
System.arraycopy(bytesMsg,len,by, 0,19);
String card =new String(by).substring(0,19);//卡号
len += 19;
System.arraycopy(bytesMsg,len,by, 0,8);
String pass =new String(by).substring(0,8);//密码
len += 8;
System.arraycopy(bytesMsg,len,by, 0,37);
String twoinfo =new String(by).substring(0,37);//二磁道信息
len += 37;
System.arraycopy(bytesMsg,len,by, 0,104);
String threeinfo =new String(by).substring(0,104);//三磁道信息
len += 104;
System.arraycopy(bytesMsg,len,by, 0,9);
String endlogno =new String(by).substring(0,9);//末笔日志号
len += 9;
System.arraycopy(bytesMsg,len,by, 0,8);
String code =new String(by).substring(0,8);//校验码
len += 8;
System.out.println("/n/n");
System.out.println("交 易 码 =" + "[" + vns.length()+ "]" + "["+ vns + "]");
System.out.println("柜 员 号 =" + "[" + counter.length() + "]" + "["+ counter + "]");
System.out.println("交易日期 =" + "[" + date.length()+ "]"+ "[" + date + "]");
System.out.println("交易时间 =" + "[" + time.length()+ "]"+ "[" + time + "]");
System.out.println("行 号 ="+"["+hang.length()+"]"+"["+hang+"]");
System.out.println("客 户 号 =" + "[" + clientno.length() + "]" + "["+ clientno + "]");
System.out.println("卡 号 =" + "[" + card.length()+ "]" + "["+ card + "]");
System.out.println("密 码 =" + "[" + pass.length()+ "]" + "["+ pass + "]");
System.out.println("二磁道信息=" + "[" + twoinfo.length()+ "]" + "["+ twoinfo + "]");
System.out.println("三磁道信息=" + "[" + threeinfo.length()+ "]" + "["+ threeinfo + "]");
System.out.println("末笔日志号=" + "[" + endlogno.length()+ "]" + "["+ endlogno + "]");
System.out.println("校 验 码 =" + "[" + code.length()+ "]" + "["+ code + "]");
System.out.println(" ----------------返回数据--------------------- ");
System.out.println("交 易 码 =" + "[" + vns.length() + "]" + "["+ vns + "]");
System.out.println("交易日期 =" + "[" + date.length() + "]"+ "[" + date + "]");
System.out.println("交易时间 =" + "[" + time.length() + "]"+ "[" + time + "]");
System.out.println("返 回 码 =" + "[" + 4 + "]" + "[" + "ABCD" + "]");
System.out.println("客 户 号 =" + "[" + clientno.length() + "]" + "["+clientno+ "]");
System.out.println("客户名称 =" + "[" +60+ "]" + "["+"ClientName nameEnd"+ "]");
System.out.println("卡 号 =" + "[" +card.length() + "]" + "["+card+ "]");
System.out.println("校 验 码 =" + "[" +code.length()+ "]" + "["+code + "]");
out.println(vns+date+time+"ABCD"+clientno+"ClientName nameEnd"+card+code);
System.out.println("发送客户端数据="+vns+date+time+"ABCD"+"ClientName nameEnd"+card+code);//控制台显示server发送数据
}
}
/*
* 新增客户资料
*/
//B002000020100319145351numb110001znumber 40Clie张三儿 nameEndAddress addEnd13728744202210890411Xiaocode
public void getMethod2() {
byte[] bytesMsg = msg.getBytes();
// System.out.println("客户端发送数据长度为=" + bytesMsg.length);
System.out.println(Arrays.toString(bytesMsg));
byte[] by=new byte[220];
if(bytesMsg.length!=by.length){
out.print("数据发送错误!!!");
System.out.println("客户端数据发送错误!!!");
}else
{
System.arraycopy(bytesMsg,0, by,0,4);
// String vsn=Arrays.toString(by);
// System.out.println("---------vns--------"+vsn);
len+=4;
String vns=new String(by).substring(0,4);// 交易码
System.arraycopy(bytesMsg, 4, by,0,4);
len+=4;
// String gui=Arrays.toString(by);
// System.out.println(" gui ="+gui);
String counter=new String(by).substring(0,4);// 柜员号
System.arraycopy(bytesMsg, len, by,0,8);
len+=8;
String date=new String(by).substring(0,8);// 交易日期
System.arraycopy(bytesMsg, len, by,0,6);
len+=6;
String time=new String(by).substring(0,6);// 交易时间
System.arraycopy(bytesMsg, len, by,0,4);
len+=4;
String hang=new String(by).substring(0,4);// 行号
System.arraycopy(bytesMsg, len, by,0,6);
len+=6;
String ztype=new String(by).substring(0,6);// 证件类型
System.arraycopy(bytesMsg, len, by,0,40);
len+=40;
String zno=new String(by).substring(0,40);// 证件号码
System.arraycopy(bytesMsg, len, by,0,60);
len+=60;
String name=new String(by).substring(0,60);// 客户名称
System.arraycopy(bytesMsg, len, by,0,60);
len+=60;
String address=new String(by).substring(0,60);// 住址
System.arraycopy(bytesMsg, len, by,0,11);
len+=11;
String telephone=new String(by).substring(0,11);// 联系电话
System.arraycopy(bytesMsg, len, by,0,1);
len+=1;
String sex=new String(by).substring(0,1);// 性别
System.arraycopy(bytesMsg, len, by,0,8);
len+=8;
String birthday=new String(by).substring(0,8);//生日
System.arraycopy(bytesMsg, len, by,0,8);
len+=8;
String code=new String(by).substring(0,8);//校验码
System.out.println("/n/n");
System.out.println("交 易 码 =" + "[" + vns.length() + "]" + "["+ vns + "]");
System.out.println("柜 员 号 =" + "[" + counter.length() + "]" + "["+ counter+ "]");
System.out.println("交易日期 =" + "[" + date.length() + "]" + "["+ date + "]");
System.out.println("交易时间 =" + "[" + time.length() + "]" + "["+ time + "]");
System.out.println("行 号 =" + "[" + hang.length() + "]" + "["+ hang+ "]");
System.out.println("证件类型 =" + "[" + ztype.length() + "]" + "["+ ztype + "]");
System.out.println("证件号码 =" + "[" + zno.length() + "]" + "["+zno + "]");
System.out.println("客户名称 =" + "[" + name.length() + "]" + "["+ name + "]");
System.out.println("住 址 =" + "[" + address.length() + "]" + "["+ address + "]");
System.out.println("联系电话 =" + "[" + telephone.length() + "]" + "["+ telephone+ "]");
System.out.println("性 别 =" + "[" + sex.length() + "]" + "["+ sex + "]");
System.out.println("生 日 =" + "[" + birthday.length() + "]" + "["+birthday + "]");
System.out.println("校 验 码 =" + "[" + code.length() + "]" + "["+ code + "]");
System.out.println(" ----------------返回数据--------------------- ");
System.out.println("交 易 码 =" + "[" + vns.length() + "]" + "["+ vns+ "]");
System.out.println("交易日期 =" + "[" + date.length() + "]" + "["+ date + "]");
System.out.println("交易时间 =" + "[" + time.length() + "]" + "["+ time + "]");
System.out.println("返回号码 =" + "[" + 4 + "]" + "[" + "0001" + "]");
System.out.println("客户号码 =" + "[" + 16 + "]" + "[" + "1234567891234567"+ "]");
System.out.println("校 验 码 =" + "[" + code.length() + "]" + "["+code + "]");
out.println(vns +date +time + "0001"+ "1234567891234567" +code);// client接收server数据
System.out.println("发送客户端数据=" + vns +date +time + "0001"+ "1234567891234567" +code);// 控制台显示server发送数据
}
}
/*查询客户资料
请求报文格式:
B00120091120092811YILB2701110001429006197802105135 飯v`誑
80 send: B00120100317144528101011116666664768114210 40BLCS9DE2
*/
public void getMethod1() {
int len=0;
String vns = msg.substring(len, len + 4);//交易码
len += 4;
String date = msg.substring(len, len + 8);//交易日期
len += 8;
String time = msg.substring(len, len + 6);//交易时间
len += 6;
String no = msg.substring(len, len + 4);//柜员号
len += 4;
String number = msg.substring(len, len + 4);//行号
len += 4;
String ztype = msg.substring(len, len + 6);//证件类型
len += 6;
String zno = msg.substring(len, len + 40);//证件号码
len += 40;
String code = msg.substring(len,msg.length());//校验码
len += 8;
// Integer.toBinaryString(len);
System.out.println("交 易 码 =" + "[" + vns.length() + "]" + "["+ vns + "]");
System.out.println("交易日期 =" + "[" + date.length() + "]"+ "[" + date + "]");
System.out.println("交易时间 =" + "[" + time.length() + "]"+ "[" + time + "]");
System.out.println("柜员号码 =" + "[" + no.length() + "]" + "["+ no + "]");
System.out.println("银行行号 =" + "[" + number.length() + "]"+ "[" + number + "]");
System.out.println("证件类型 =" + "[" + ztype.length() + "]"+ "[" + ztype + "]");
System.out.println("证件号码 =" + "[" + zno.length() + "]" + "["+ zno + "]");
System.out.println("校 验 码 =" + "[" +code.getBytes().length+ "]" + "["+ code + "]");
out.print("B001201003171553280000123456789123456760constrom 60FDLDK520");
System.out.println(" ---------------- ");
System.out.println("交 易 码 =" + "[" + vns.length() + "]" + "["+ vns + "]");
System.out.println("交易日期 =" + "[" + date.length() + "]"+ "[" + date + "]");
System.out.println("交易时间 =" + "[" + time.length() + "]"+ "[" + time + "]");
System.out.println("返回号码 =" + "[" + 4 + "]" + "[" + "0001" + "]");
System.out.println("客户号码 =" + "[" + 16 + "]" + "["+ "1234567891234567" + "]");
System.out.println("客户名称 ="+ "["+ 60+ "]"+ " ["+ "60constrom 60"+ "]");
System.out.println("校 验 码 =" + "[" + code.getBytes().length + "]" + "["+code+ "]");
out.println(vns+date+time+"0001"+"1234567891234567"+"60constrom 60"+code);
System.out.println("发送客户端数据="+vns+date+time+"0001"+"1234567891234567"+"60constrom 60"+code);
}
public static void main(String[] args) throws IOException {
// String str = "B002000020100319145351numb110001znumber 40Clie张三儿 nameEndAddress addEnd13728744202210890411Xiaocode";
// System.out.println(str.length());
// System.out.println(str.getBytes().length);
new Socket1();
}
}