2015-09-03 68 views
0

使用java socket編程,發送字節數組。字節數組大小爲3500.它不會作爲單個請求發送,而會在網絡捕獲時拆分爲3個請求。所以服務器無法處理分割的請求。我想單發發送請求。查找下面用於發送字節數組請求的代碼片段。字節數組消息在發送時被分割。我想單發發送請求消息

 byte[] bISOMsg = new byte[9000]; 
     bISOMsg = isoMsg.pack(); 


     int messageLength = (short)bISOMsg.length; 
     messageLength = messageLength - 16; 

     /* System.out.println("messageLength --> " + messageLength); 
    System.out.println("Header --> " + new String(isoMsg.getHeader()));*/ 

     byte[] bHeaderLen = new byte[2]; 
     ByteBuffer bbHeader = ByteBuffer.wrap(bHeaderLen); 
     bbHeader.putShort((short)messageLength); 
     isoMsg.setHeader(bbHeader.array()); 
     bISOMsg = isoMsg.pack(); 

     isoMsg.unpack(bISOMsg); 
     logISOMsg(isoMsg); 

     System.out.println("bISOMsg....."+new String(bISOMsg)); 


     byte[] BitmapBytVal= new byte[32]; 

     System.arraycopy(bISOMsg, 4,BitmapBytVal, 0, 32); 
     //System.out.println("BitmapBytVal..."+BitmapBytVal); 


     ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream(); 
     outputStream1.write(isoHeader.getBytes()); 
     outputStream1.write(bISOMsg, 0,4); 
     outputStream1.write(HexToByte1(new String(BitmapBytVal))); 
     outputStream1.write(bISOMsg, 36, bISOMsg.length-36); 
     TotalMsgBytVal =outputStream1.toByteArray(); 
     outputStream1.close(); 

     System.out.println("TotalMsgBytVal Hex value="+TotalMsgBytVal); 

     System.out.println("Msg Length ---- " + TotalMsgBytVal.length); 
     String msgLength= Integer.toHexString(TotalMsgBytVal.length); 
     msgLength = addZeros(msgLength,4); 
     System.out.println("Msg Length ----: " + msgLength); 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
     String MSGIndicater="03NPCI ONC"; 
     outputStream.write(MSGIndicater.getBytes()); 
     outputStream.write(HexToByte1(msgLength)); 
     outputStream.write(TotalMsgBytVal,0,TotalMsgBytVal.length); 
     outputStream.close(); 

     TotalMsgBytVal = outputStream.toByteArray(); 

     Socket soc = null; 
     byte []dataRes = new byte[9000]; 

     System.out.println("Gateway IP Address ="+ cbsipaddr); 
     System.out.println("Gateway Port ="+ cbsport); 

     soc= new Socket(cbsipaddr,cbsport); 

     in=soc.getInputStream(); 
     /* 
     /* Added by Syed on 03/09/15 */ 
     System.out.println("Total Length of Request is = "+ TotalMsgBytVal.length); 
     DataOutputStream dout = new DataOutputStream(soc.getOutputStream()); 
     dout.writeInt(TotalMsgBytVal.length); // write length of the message 
     dout.write(TotalMsgBytVal);  // write the message 
     Thread.sleep(1000); 
     dout.flush(); 
+0

沒有辦法:http://stackoverflow.com/a/3074427/360211 – weston

回答

0

那麼,以太網網絡的MTU大約是1500字節。

當您嘗試通過以太網寫入3500字節時,您會怎麼想?

你的代碼看起來很時髦,我想你應該看看現有的實現,看看你如何改進你的代碼。如果它不能處理拆分成多個數據包的消息,那麼這是一個非常糟糕的服務器。

+0

我們發送的是客戶端,服務器不是我們的,服務器支持團隊聲稱他們只會解析請求消息,只有當他們收到單發。請指導任何選項以發送3500字節作爲單個請求。 – Syed

+0

@Syed如果您的網絡的MTU小於3500(最有可能),那麼將數據作爲單個數據包發送實際上是不可能的。這並不重要,因爲TCP/IP是一個流協議,所以您不需要關心它使用多少個數據包來傳輸數據。如果您從上面的代碼中刪除了所有額外的垃圾,則可以更容易地看到您正在做什麼。你至少要寫一個包含消息長度的頭文件,這樣就足以讓服務器知道需要多少數據。 – Kayaman