Java socket programming

S

stathis gotsis

Hello to everyone,
I have a question for you: is there a way to count the number of packets
going through a stream? I know data is sent byte by byte through a stream
socket, but how can i count the actual number of packets?
Thank you
 
S

slippymississippi

Hello to everyone,
I have a question for you: is there a way to count the number of packets
going through a stream? I know data is sent byte by byte through a stream
socket, but how can i count the actual number of packets?
Thank you

OSI 7 Layer Communications Model:

Sockets are a session library, which means that sockets reside in Layer
5.

The data is broken into packets via TCP in the Transport Layer (Layer
4) or via IP in the Network Layer (Layer 3), which are hidden from you
by sockets.

In other words, if I'm not mistaken, Java by itself can't give you this
information ... you would probably have to retrieve it from a log or a
tool somewhere.
 
S

slippymississippi

FWIW: Java appears to support a connectionless communication protocol
via DatagramPackets over DatagramSockets. This will allow you to count
the number of packets sent, but then you have to provide all the
functionality of TCP/IP yourself.
 
G

Gordon Beaton

I have a question for you: is there a way to count the number of
packets going through a stream? I know data is sent byte by byte
through a stream socket, but how can i count the actual number of
packets?

"streams" are conceptually not "packets", which exist at a lower layer
in the protocol stack.

At any rate Java doesn't provide any way to determine this.

However you can make a rough guess if you know the MTU for the network
(usually 1500 bytes for ethernet, but ifconfig can tell you for sure).

Determine the lower bound by counting the total bytes sent, and
dividing by the MTU. This assumes that every IP datagram is completely
filled.

Determine the upper bound by doing the same calculation individually
for every call to OutputStream.write(n bytes), rounding *up* whenever
n is not an exact multiple of MTU, since the extra bytes will require
an additional packet.

Depending on sender timing and other issues, the real answer will lie
somewhere between the two bounds, but likely closer to the upper
bound.

/gordon
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top