91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java中處理socket通信過程中粘包的情況

發布時間:2020-09-02 19:44:12 來源:腳本之家 閱讀:124 作者:二十七路公交車 欄目:編程語言

這兩天學習了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(); 
          } 
        } 
      } 
    } 
  } 
 
} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

昌平区| 陵川县| 枣阳市| 马边| 合水县| 天气| 石首市| 山东| 阳新县| 旌德县| 淳安县| 曲阜市| 九龙城区| 宜丰县| 新泰市| 安康市| 莱州市| 清镇市| 从江县| 天峨县| 宜君县| 崇信县| 中阳县| 龙游县| 临泉县| 昔阳县| 喀喇| 贺州市| 太康县| 虹口区| 疏勒县| 应城市| 临猗县| 琼海市| 营口市| 辰溪县| 西乌珠穆沁旗| 太和县| 子长县| 龙泉市| 梁平县|