• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending message via Java socket

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have to send a message over TCP/IP from my program to a server. Below is message format..

struct .recv^msg FIELDALIGN (SHARED2);
BEGIN
INT(32) msg^len;
STRING acct^num[0:8];
STRING rpt^id[0:9];
STRING seq^num[0:6];
STRING send^cnt[0:1];
END

I am doing the following...



The method padwithSpaces just adds extra whitespace at the end so that the field size matches what the server needs.

But the above does not seem to be working. The server says message is in wrong format. So any suggestions on what could be wrong?

One concern I had is the first field is " INT(32)" (I think it means it is expecting 32 bit integer). The reason I did not use int is because I thought when Java writes to the output stream it converts to char array. So I just used 2 chars which I hoped would become the 32 bits server is expecting. Am I wrong in this assumption?

Thanks in advance for any help and best regards,
Jahan

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jehan Jaleel wrote:I am doing the following...


Jehan,

Please DontWriteLongLines. I've broken yours up this time.

Winston
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston for cleaning up my post.

I just realized that the first field is 32 bytes and not bits. So I was guessing passing a value like "5555" should translate into that (4 characters is 32 bytes). But it still does not seem to be working.

Thanks.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt that you'd have 32 byte integers. That's 8 times as big as a regular int, and 4 times as big as a long. I think you were right about the 32 bit before.

The problem here is how you try to fill that integer. Just writing a String value of it will not send the integer itself, but its String representation. You send 2 bytes instead of the 4 required. You could check out DataOutputStream:
This may present one problem though - this code writes the integer with the high-byte first (little endian). If the server expects the integer as big endian, you will need to use some bitwise computing to write the integer in reverse order.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for getting back to me on this, I think you are right in that DataOutputStream is the way to go.

Just one question, if I use the writeBytes method as you suggested when writing Strings to the stream then will it come out on the other side as a String (meaning as text that can be read)?

Thanks again,
Jehan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic