工作需要:场景:
一个端口随时会给我转入 16进制的520个字节 我这边接收之后需要进行解析,存储和转发到前端。
第一次接触遇见很多问题 记录下
服务器
package fc.com.utils;
import java.io.*;
import java.net.*;
public class TestScoket {
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器端--开始监听");
while (true) {
Socket socket = serverSocket.accept();
SocketUtils hm = new SocketUtils(socket);
Thread t = new Thread(hm);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
线程
package fc.com.utils;
import java.io.*;
import java.net.Socket;
public class SocketUtils implements Runnable{
public static int count = 0;
Socket socket = null;
public SocketUtils(Socket socket) {
count++;
this.socket = socket;
System.out.println("用户" + count + "接入");
}
@Override
public void run() {
BufferedInputStream bis = null;
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
synchronized(this) {
try {
bis = new BufferedInputStream(socket.getInputStream());
while (true) {
byte[] data = new byte[1025];
bis.read(data);
String str = bytesToHexString(data);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
Thread.sleep(3000);
byte[] content =("server-"+"synchronized threadName=" + Thread.currentThread().getName()).getBytes();
out.flush();
out.write(content);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//TCP16进制转为String
public static String bytesToHexString(byte[] src) {
StringBuffer sb = new StringBuffer("");
if (src == null || src.length <= 0) {
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v).toUpperCase();
if (hv.length() < 2) {
sb.append(0);
}
sb.append(hv);
if (i != src.length - 1) {
sb.append(" ");
}
}
return sb.toString();
}
}
客户端
package fc.com.utils;
import java.io.*;
import java.net.*;
public class Client {
static Socket socket = null;
public static void main(String[] args) throws Exception {
BufferedInputStream bis = null;
try {
socket = new Socket("127.0.0.1",8888);
System.out.println("成功连接服务器");
// 监视控制台 readLine()是读写有回车的字符串
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true){
BufferedOutputStream bout = new BufferedOutputStream(socket.getOutputStream());
byte[] content = br.readLine().getBytes();
bout.flush();
bout.write(content);
bout.flush();
bis = new BufferedInputStream(socket.getInputStream());
byte[] data = new byte[1025];
bis.read(data);
System.out.println(new String(data).trim());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
粘包处理:
//粘包处理与数据转发
@Override
public void run() {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(_client_socket.getInputStream());
} catch (Exception e) {
System.out.println("字节流初始化异常");
}
byte [] _recvBuf = new byte[1024];//接受缓存区
int _recvCount = 0;
int _index = 0;
byte b;
_sendBuf[0] = (byte)0xEB; // 0xEB
_sendBuf[1] = (byte)0x90; // 0x90
String _buffer_data = null ;//处理之后数据
while(true)
{
//尝试接收数据
try {
_recvCount = bis.read(_recvBuf);//如果没有数据会阻塞在此
} catch (Exception e) {
System.out.println("接收数据异常关闭失败"+_client_socket.getRemoteSocketAddress().toString());
return;
}
if(_recvCount < 0 )
{
System.out.println("客户端TYPE退出2"+groundStationName + _client_socket.getRemoteSocketAddress().toString());
return;
}
for(i =0; i<_recvCount; i++)
{
b= _recvBuf[i];
if(_index==0)
{
if(b==(byte)0xEB)
_index = 1;
}
else if(_index == 1)
{
if (b == (byte)0x90)
_index = 2;
else if (b == (byte)0xEB)
_index = 1;
else
_index = 0;
}
else if(_index == RECV_BUFFER_SIZE-1)
{
_sendBuf[_index] = b;
_buffer_data = ByteForIntUtils.bytesToHexString(_sendBuf);
//发送
send(_buffer_data);
_index = 0;
}
else
{
_sendBuf[_index] = b;
_index++;
}
}
if(_buffer_data == null) {
System.out.println("客户端TYPE退出3"+groundStationName);
}
}
}