socket通信 客户端与服务端

时间:2022-01-04 23:58:45

客户端代码

public byte[] sendBuff(Socket socket,byte[] by){
PrintStream output = null;
DataInputStream input = null;
runLog.info("--->sendBuff--socket:"+socket);
runLog.info("--->sendBuff--byte_by:"+by.length);
try {
output = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
runLog.info("--->sendBuff--output:"+output.toString());
output.write(by);//将数组by写入到套接字中
output.flush();
byte[] recBuf = new byte[65535];//定义缓冲区
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
runLog.info("--->sendBuff--input:"+input.toString());
int len = input.read(recBuf);//从socket中读取数据放到缓冲区中,len为成功读取的字节数
runLog.info("--->sendBuff--len:"+len);
if(len>0)
{
byte[] data = new byte[len];
System.arraycopy(recBuf, 0, data, 0, len);//将recBuf中的值放到data中
runLog.info("---sendBuff--data:"+data.length);
output.close();
input.close();
return data;
}else
{
output.close();
input.close();
runLog.info("---sendBuff--socket读取出错!");
return null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
runLog.info("---sendBuff---io:"+e.getMessage());
runLog.info("----sendBuff---socket出错!");
try {
output.close();
input.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
runLog.info("---sendBuff--ioClose:"+e1.getMessage());
}
return null;
}

}


服务端代码:

package com.upcome.test;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class TestServer {
public static void main(String args[]) {
while(true){
try {
ServerSocket server = null;
try {
server = new ServerSocket(4700); // 创建一个ServerSocket在端口4700监听客户请求
} catch (Exception e) {
System.out.println("can not listen to:" + e); // 出错,打印出错信息
}
Socket socket = null;
try {
socket = server.accept();
// 使用accept()阻塞等待客户请求,有客户
// 请求到来则产生一个Socket对象,并继续执行
} catch (Exception e) {
System.out.println("Error." + e); // 出错,打印出错信息
}
PrintStream output = null;
DataInputStream input = null;
byte[] by = null;
byte[] recBuf = new byte[65535];
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
int len = input.read(recBuf);
byte[] data = new byte[len];
System.arraycopy(recBuf, 0, data, 0, len);
String strCode = null;
String reqbody = new String(data);
strCode = reqbody.substring(0, 5);
   System.out.println("---交易码---->"+strCode);
   System.out.println("请求包体:"+reqbody);
output = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
String str = "9000400000000000000000000007020000999\n100001\tlisi\taaa\t110\t11111111111\tbbb\t1,2,3\t6,5,9\tyes\tone\t99\t55\t23.00\t65.00";
output.write(str.getBytes());//将数组by写入到套接字中
output.flush();
   if(strCode.equals("90001"))
   {    
   }    
socket.close(); // 关闭Socket
server.close(); // 关闭ServerSocket
output.close();
input.close();
} catch (Exception e) {
System.out.println("Error:" + e); // 出错,打印出错信息
}
}
}
}