Sending pkts on a socket...

D

drew78

Hi all!

I am kind of new to the Java socket programming and used to handle things
the "c/c++-way"...
If I want to construct a message containing a number of bytes, shorts and
longs and perhaps a fiew strings, how do I combine these into a packet the
best way?

I C/C++ I would construct a struct/class and then just fill in the values
and then just send the mem-area, but that approach is not likley to happen
in java...

Can any one give me some hints and save me a few hours of hacking?

Sincerly,
Drew
 
D

Dario

drew78 said:
Hi all!

I am kind of new to the Java socket programming and used to handle things
the "c/c++-way"...
If I want to construct a message containing a number of bytes, shorts and
longs and perhaps a fiew strings, how do I combine these into a packet the
best way?

I C/C++ I would construct a struct/class and then just fill in the values
and then just send the mem-area, but that approach is not likley to happen
in java...

Can any one give me some hints and save me a few hours of hacking?

I never used socket in Java.
I spent 1 minute (53 seconds to be precise) reading javadoc
specification and my solution is:

Socket s = new Socket(....);
OutputStream os = s.getOutputStream();
os.write(....);

or:

Socket s = new Socket(....);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.print(...);

or:

Socket s = new Socket(....);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream (os);
os.write(...);

Why cannot you spent yourself your time reading the doc?

- Dario
 
B

Barry White

Hi Drew,

you should look at the DataOutputStream class which has methods such as
writeShort, writeUTF, etc. (corresponding DataInputStream has readShort,
etc).

If you wrap a SocketOutputStream in a DataOS it should do the trick,

Regards,
Barry
 
T

Tim Ward

drew78 said:
I C/C++ I would construct a struct/class and then just fill in the values
and then just send the mem-area, but that approach is not likley to happen
in java...

In general it doesn't work in C/C++ either.

It will only work if you are lucky enough to have control over the compilers
at both ends of the connection and ensure that they have the same lengths
for the various types you're sending and are both set to the same byte order
and packing regime.
 
T

Tim Ward

Dario said:
Why cannot you spent yourself your time reading the doc?

That wasn't what he asked. Why couldn't you spend time reading the
question? - it would have been quicker than the time you spent writing a
rude answer.
 
D

Dario

Tim said:
That wasn't what he asked. Why couldn't you spend time reading the
question? - it would have been quicker than the time you spent writing a
rude answer.

He ask:
If I want to construct a message containing a number of bytes,
shorts and longs and perhaps a fiew strings, how do I combine
these into a packet the best way?

and my answer is:
Socket s = new Socket(....);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream (os);
os.write(...);

Spending an additional 30 seconds reading the fucking documentation
I can be more precise (he ask about bytes, shorts, longs and strings):

Socket s = new Socket(....);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream (os);
os.writeByte(42);
os.writeShort(42);
os.writeLong(42);
os.writeBytes("42");

Repeating myself:
Why cannot you spent yourself your time reading the doc?

- Dario
 
D

drew78

Hi Barry!

I where actually looking at that class before...
A related question to this class: When do it send out the data? Will the
socket collect data untill it determines that it is "worth" to send a pkt or
is is sent upon destruction of the DataOutputStream?
If you wrap a SocketOutputStream in a DataOS it should do the trick,
Huh? SocketOutputStream?

Thanks for you kind answer!

Sincerly,
Drew
 
D

drew78

Hi Tim!

I do use gnu c/c++ in 99% of the cases and do not have to worry about these
problems so much. However, if the data is encoded in network byte order in
the packages the biggest problem should be eliminated.
When it comes to the size of the data I have limited experience... Do
compilers have different size on the standard types like int, short, char,
float and double?

Regards,
Drew
 
D

drew78

Why do you even answer if you think the question where so stupid?
Btw, you've still missed like 50% of the question!

//Drew
 
C

Chris Uppal

drew78 said:
When it comes to the size of the data I have limited experience... Do
compilers have different size on the standard types like int, short, char,
float and double?

Yes.

They also differ in how much space (if any) they leave between the elements of
a struct.

What's more, in many cases these things are controlled by compiler options (and
pragmas). So it's never safe to pass C/C++ structures in this way *unless* you
know what you are doing *and* have complete control over the compilation
environment at both ends of the connection.

Sad (for network programmers), but true.

-- chris
 
D

drew78

True...

I just recall a period when I almost became bold due to this... I couldn't
figure out why some structs where bigger than they should be somethimes and
I where yanking my hear of in the process... (Well, almost anyway...;))

I think I'm getting away from the original question, but do compilers put
stuff BETWEEN the elements too? (I know that ie MS VC puts stuff around the
data in a debug compilation in order to find access violations, but does
this happend in a non-debug compilation too?)

Cheers,
Drew
 
N

no-mail-here

Dario said:
He ask:


and my answer is:

Ok. So you freely admit that he asked how to construct a packet from values,
and you told him how to create a socket and send bytes. I didn't see him
ask anywhere how to create/open a socket. So you were so keen to flame
someone that you either didn't read or didn't understand the question.

The best option, as Barry pointed out, is to wrap the OutputStream received
from a Socket in a DataOutputStream and send values individually, with either
an implicit or explicit protocol for interpreting the datatypes received.

Postings on this group have a tendency to be friendly, or at least civil.
Please try to keep it that way. If you read (or mis-read) a question you
believe is stupid, don't answer it.

Regards,

Danny.
 
C

Chris Uppal

drew78 said:
I think I'm getting away from the original question, but do compilers put
stuff BETWEEN the elements too?

Yes, 'fraid so.

The reason is that some processors have faster access to field members that are
aligned on certain boundaries, and some processors don't even allow *any*
access to certain datatypes unless they are properly aligned. But in a sense
the "why" is irrelevant -- the compilers are allowed to insert as much space as
they like, and (unless there's a compiler-specific way of controlling it, *and*
you are using that to control it) you can't make assumptions about what it's
doing.

As an example, a struct like:

struct X {
char ch;
double db;
};

could, depending on the machine its running on, do any of:

- work fine without any padding between the two fields.
- work OK without, but significantly faster with, 3 (or 7) bytes of padding
between them.
- not work at all without padding.

(Pentiums fall into the second category)

If you are using MSVC then there's a #pragma that can be used to override the
compiler's default behaviour, but I can't remember what it is.

-- chris
 
S

Sudsy

drew78 said:
True...

I just recall a period when I almost became bold due to this... I couldn't
figure out why some structs where bigger than they should be somethimes and
I where yanking my hear of in the process... (Well, almost anyway...;))

I think I'm getting away from the original question, but do compilers put
stuff BETWEEN the elements too? (I know that ie MS VC puts stuff around the
data in a debug compilation in order to find access violations, but does
this happend in a non-debug compilation too?)

Padding for alignment purposes is not unusual. Suppose you had something
like this:
struct my_s {
char c;
int i;
};

If you do this:
printf( "size = %d\n", sizeof(struct my_s) );

Under gcc/Linux you'll get:
size = 8

Three padding bytes are inserted so that structure aligns properly.
But this is terribly off-topic in a Java ng...
 
J

Jon A. Cruz

Chris said:
What's more, in many cases these things are controlled by compiler options (and
pragmas). So it's never safe to pass C/C++ structures in this way *unless* you
know what you are doing *and* have complete control over the compilation
environment at both ends of the connection.

Sad (for network programmers), but true.

But what's *worse* is that you can never rev your compiler.

That is, one of the things you need to control is the compiler vendor
themselves. For example, at one point Microsoft went and changed one
basic primitive type from 80 bits to 64 bits in the newer version of
their compiler, with only a tiny line burried near the end of a readme
text file.

=-O
 
C

Chris Uppal

Jon said:
For example, at one point Microsoft went and changed one
basic primitive type from 80 bits to 64 bits in the newer version of
their compiler, with only a tiny line burried near the end of a readme
text file.

long double ?

-- chris
 
J

Jon A. Cruz

Chris said:
Jon A. Cruz wrote:




long double ?

Yup.

Changed at some point after MSVC 2.0, but then Microsoft's revisionist
documentation makes it sound as if Win32 was *always* 64-bit, when it
actually started 80-bit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

socket data sending problem 2
Insight on a coding project. 1
reading from a socket 56
Socket Server with MySQL 14
SSL/Socket 1
Timeout question on a socket thread 12
Sending file over a socket 2
socket 5

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top