Re: Socket Client , abnormal behavior when sending byte

From: Jon A. Cruz (jon_at_joncruz.org)
Date: 02/28/04


Date: Sat, 28 Feb 2004 10:39:54 -0800

Max wrote:
>>1) Are you using UDP instead of TCP?
>
>
> // get the socket !
> Socket s= new Socket("127.0.0.1",5000);
>
> // get the outputstream
> OutputStream myO = s.getOutputStream();
>
> // send the first 10 KByte and wait 1second
> myO.write(50_kb_total_stream,0,10*1024);
> myThread.sleep(1000);

Ooohhh. That's bad.

sleep() is a static method that operates on whatever the current thread
happens to be.

Instead of
myThread.sleep(1000);
use
Thread.sleep(1000);

Otherwise you're liable to get confused about your threading.

>
> // send the next 10KByte and wait 1 second
> myO.write(50_kb_total_stream,10*1024,20*1024);

Aha. That's the main problem that Joe pointed out.

Check the Java docs
http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStream.html

> void write(byte[] b, int off, int len)
> Writes len bytes from the specified byte array starting at offset off
> to this output stream.

So as you're increasing the offset, you're also increasing the size.



Relevant Pages

  • Re: Rationale for CS0536?
    ... >> current thread to sleep since Sleep is a static method. ... > design or even naming problem, nothing that would have to be ... Weren't you just asking why the compiler didn't let you call static methods ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java Code Convention Guidelines question...
    ... The issue is invoking a static method while ... a different thread to sleep. ... confusion, by implying that the method "sleep" acted on a specific other ... about whether a developer should intentionally pretend to act on some ...
    (comp.lang.java.programmer)
  • Re: Help with Thread Sleep Calls causing lack of SLEEP
    ... sleepis a static method. ... Java allows static members to be accessed via references, ... *always* the one which is set to sleep. ... > waiting a random set amount of time for something else to finish - you ...
    (comp.lang.java.help)
  • Re: form object
    ... early on I thought about using a timer since a counter does not run ... concerned about the differing speeds on the various local workstations. ... any static method should probably be thread-aware; ... You don't want to Sleep(), as this will hang the UI thread (unless you spin ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: form object
    ... any static method should probably be thread-aware; when dealing with ... and push the method onto the owning UI's thread; ... You don't want to Sleep(), as this will hang the UI thread (unless you spin ...
    (microsoft.public.dotnet.languages.csharp)