Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending message via Java socket

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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

 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic