This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sending message via Java socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending message via Java socket" Watch "Sending message via Java socket" New topic
Author

Sending message via Java socket

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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

Joined: Mar 17, 2011
Posts: 7553
    
  18

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

Jehan,

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

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending message via Java socket
 
Similar Threads
Breaking loop or any other alternatives?
Local variable and Thread issue
Threads with static variables
recursive method
Displaying a histogram using input from an array