java中处理socket通信过程中粘包的情况
程序员文章站
2023-12-22 09:23:28
这两天学习了java中处理socket通信过程中粘包的情况,而且很重要,所以,今天添加一点小笔记。
处理粘包程序是客户端的接受消息线程:
客户端:
imp...
这两天学习了java中处理socket通信过程中粘包的情况,而且很重要,所以,今天添加一点小笔记。
处理粘包程序是客户端的接受消息线程:
客户端:
import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.printwriter; import java.io.reader; import java.net.socket; import java.nio.charbuffer; public class testsocketclient { public static void main(string[] args) { // todo auto-generated method stub new testsocketclient().start(); } class sendthread extends thread{ private socket socket; public sendthread(socket socket){ this.socket=socket; } @override public void run(){ while(true){ try{ thread.sleep(1000); string send="<soap-env:envelope>"+system.currenttimemillis()+"</soap-env:envelope>"; printwriter pw=new printwriter(new outputstreamwriter(socket.getoutputstream())); pw.write(send); pw.flush(); }catch(exception e){ e.printstacktrace(); } } } } class receivethread extends thread{ private socket socket; private volatile byte[] bytes=new byte[0]; public receivethread(socket socket){ this.socket=socket; } public byte[] mergebyte(byte[] a,byte[] b,int begin,int end){ byte[] add=new byte[a.length+end-begin]; int i=0; for(i=0;i<a.length;i++){ add[i]=a[i]; } for(int k=begin;k<end;k++,i++){ add[i]=b[k]; } return add; } @override public void run(){ while(true){ try{ inputstream reader=socket.getinputstream(); if(bytes.length<2){ byte[] head=new byte[2-bytes.length]; int couter=reader.read(head); if(couter<0){ continue; } bytes=mergebyte(bytes,head,0,couter); if(couter<2){ continue; } } //下面这个值请注意,一定要取2长度的字节子数组作为报文长度,你懂得 byte[] temp=new byte[0]; temp=mergebyte(temp,bytes,0,2); string templength=new string(temp); int bodylength=integer.parseint(templength); if(bytes.length-2<bodylength){ byte[] body=new byte[bodylength+2-bytes.length]; int couter=reader.read(body); if(couter<0){ continue; } bytes=mergebyte(bytes,body,0,couter); if(couter<body.length){ continue; } } byte[] body=new byte[0]; body=mergebyte(body, bytes, 2, bytes.length); system.out.println("client receive body: "+new string(body)); bytes=new byte[0]; }catch(exception e){ e.printstacktrace(); } } } } public void start(){ try{ socket socket=new socket("127.0.0.1",18889); new sendthread(socket).start(); new receivethread(socket).start(); }catch(exception e){ e.printstacktrace(); } } }
服务端:
package com.meituan.service.bankgate.gateway; /** * created by cqx on 16/7/19. */ import java.io.*; import java.net.serversocket; import java.net.socket; import java.nio.charbuffer; import java.util.date; public class testahahha { private final static string soap_begin = "<soap-env:envelope"; private final static string soap_end = "</soap-env:envelope>"; public static void main(string[] args) { // todo auto-generated method stub testahahha testserver=new testahahha(); testserver.start(); } public void start(){ try{ serversocket serversocket=new serversocket(18889); while(true){ socket socket=serversocket.accept(); new socketthread(socket).start(); } }catch(exception e){ e.printstacktrace(); } } class socketthread extends thread{ private socket socket; private string temp; public socketthread(socket socket){ this.socket=socket; } public socket getsocket(){ return this.socket; } public void setsocjet(socket socket){ this.socket=socket; } @override public void run(){ try{ reader reader=new inputstreamreader(socket.getinputstream()); // writer writer=new printwriter(new outputstreamwriter(socket.getoutputstream(),"utf-8")); outputstream writer=socket.getoutputstream(); charbuffer charbuffer=charbuffer.allocate(8192); int readindex=-1; while((readindex=reader.read(charbuffer))!=-1){ charbuffer.flip(); temp+=charbuffer.tostring(); if(temp.indexof(soap_begin)!=-1 && temp.indexof(soap_end)!=-1){ //system.out.println(new date().tolocalestring()+"server:"+temp); temp=""; string str="receive the soap message hahahah"; byte[] headbytes=str.getbytes(); int length=headbytes.length; string l=string.valueof(length); byte[] lengthbytes=l.getbytes(); byte[] bytes=new byte[length+lengthbytes.length]; int i=0; for(i=0;i<lengthbytes.length;i++){ bytes[i]=lengthbytes[i]; } for(int j=i,k=0;k<length;k++,j++){ bytes[j]=headbytes[k]; } system.out.println("server send:"+new string(bytes)); writer.write(bytes); writer.flush(); }else if(temp.indexof(soap_begin)!=-1){ temp=temp.substring(temp.indexof(soap_begin)); } if(temp.length()>1024*16){ break; } } }catch(exception e){ e.printstacktrace(); }finally{ if(socket!=null){ try{ if(!socket.isclosed()){ socket.close(); } }catch(exception e){ e.printstacktrace(); } } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。