CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    9

    Msg Receiving Through Socket some problem

    <p>
    Dear Sir,
    Actually I'm Reading Msg from Socket Sent by C++ Program(Server),It will send 2 msgs/sec.the msg format is like is
    <br>
    [B]
    |1118|0074|01|0023 |1234 |MIG|11FTR01|0eT0000023000318|000050|R|sampl eT msg Ref 0023000318;#STEPSD45C4F1E13A23CFB2DEAC4CBB6F4DD42E44BAB47F8944CF8F77ACB035F4D0652F2695D0A995240124CFEB539773551648F3A0118620DD293BD8EA139AB3BCE2ACDC4454B437E7C57618F97AB7E177C99B03D1D37D9119A16D8428025FEE1E4C93BEC9AE4275E86A22594CB739FFAFDB97EFFC37937DF871B9189577838686490A37247BCECF5382B96CF234E64B731E6B83DBBF9CB9B089245FCAC8E8D9A36D2DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134B32946CCAB9BE7ACC5FCBFBA1A36405EC79D8846A5F8A522D190403BA57ED6D069935ED00284D1F695BC02F9503DC696DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB61345CD256C8777695CC57AE9826FC72E9B6524348C53A2205AC3D8C6D6F66416CC28B302458B8E25C5754E695AD7D7E116979EC131B40904FCEDF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134DF415E0336AB6134E2E858FCED961E53#| ]
    [B]
    ie., Starting with pipe(|)Total Msg Length ends with |.Based on the total msg length only I'm receiving msg.Actually the program is thread based.The Problem is that some time the receiving msg length is wrong format(it is the part of the msg)
    my ErrorLog showing like this.
    [ 09:57:34 ] => [ Message is Wrong Format(MSG LEN)=0336A ]
    Suggest me what may be the problem whether socket or receiving side(java program),I'm receiving msg without blocking.Receiving part code as follows,

    [code]
    if(soc != null)
    {
    BufferedInputStream recdMsg = new BufferedInputStream(soc.getInputStream());
    stringbuffer = new StringBuffer();
    byte abyte0[] = new byte[5];
    int j = 0;
    int i;
    if((i = recdMsg.read(abyte0, 0, 5)) != -1)
    {
    String chkNum = new String(abyte0);
    stringbuffer.append(new String(abyte0));
    int k = MsgExtractor.isNumber((new String(abyte0)).substring(1));
    if(k != 1)
    {
    j = Integer.parseInt((new String(abyte0)).substring(1));
    j -= 5;
    }
    else if (k == 1 || !chkNum.startsWith("|"))
    {
    sc.ErrorLog("Message is Wrong Format(MSG LEN)="+chkNum, logcode, "A");
    }

    byte ch[]=new byte[j];
    recdMsg.read(ch,0,j);
    stringbuffer.append(new String(ch));
    }//End of if((i = recdMsg.read(abyte0, 0, 5)) != -1)
    }// End of if(naradasoc != null)
    else
    {
    return;
    }
    naradamsg = stringbuffer.toString();
    [code]
    Plz help me.,<br>
    -Raja.

  2. #2
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    You could try something like:

    Code:
    BufferedInputStream recdMsg = new BufferedInputStream(soc.getInputStream());
    stringbuffer = new StringBuffer();
    
    int i = -1;
    while((i = recdMsg.read()) != -1)   {
        stringbuffer.append( (char) i);
    }
    Then parse the StringBuffer for the length of the msg.

    ie

    Code:
    int msgLength = Integer.parseInt(stringbuffer.substring(1,4));
    That way you'll have the whole message before you do any other processing

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    plus.. 5 bytes is a bit of a poor buffer size.. even your mouse port has more than that!
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    also, maybe im thick or something but i see:

    int j = 0;
    ...
    j-=5;
    ...
    byte ch[]=new byte[j];

    hmmm
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured