1

Using java socket programming, sending byte array. Byte array size is 3500. It is not send as single request, splitted into 3 request while network capture. so server couldn't able to process the splitted request. I want to send the request in single shot. Find below the code snippet which am used for sending byte array request.

        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();
lukelazarovic
  • 1,510
  • 11
  • 19
Syed
  • 11
  • 1

1 Answers1

0

Well, the MTU for an ethernet network is about 1500 bytes.

What do you think happens when you try to write 3500 bytes over ethernet?

Your code also looks very funky, I think you should look at an existing implementation to see how you can improve your code. If it can't handle messages that are split into multiple packets, it's a pretty bad server.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • We are sending as a client, server is not ours, server support team is claiming that they will parse the request message only if they receive in single shot. Please guide any option to send the 3500 bytes as single request. – Syed Sep 05 '15 at 07:14
  • @Syed If the MTU of your network is less than 3500 (which it most likely is), it's physically impossible to send the data as a single packet. Not that it matters, since TCP/IP is a streaming protocol, you don't need to care how many packets it uses to transfer the data. If you removed all the extra garbage from the code above, it would be easier to see what you're doing. You're at least writing a header with the message's length, so that's enough for the server to know how much data to expect. – Kayaman Sep 08 '15 at 06:40