File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending message via Java socket" Watch "Sending message via Java socket" New topic

Sending message via Java socket

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196

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

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

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,

Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8944

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


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


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
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.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

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.

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,
I agree. Here's the link:
subject: Sending message via Java socket
It's not a secret anymore!