I/O blocks threads?

Stewart Allen stewart at neuron.com
Fri Apr 18 13:08:53 PDT 1997


 * kaffe 0.8.4
 * Linux 2.0.27 i586

 I'm having a problem with I/O calls blocking threads. Is this a
 well-known problem with kaffe or is it specific to the Linux which
 I'm running it on?

 I've attached bit of sample code which shows the problem. After
 compiling, invoke it with:

 kaffe udptest

 The Java VM completes the test, but kaffe blocks after the
 first call to receive(). If I run the client and server as
 separate processes then all is well. To do this:

 kaffe udpclient 7777
 kaffe udpserver localhost 7777

 in separate windows. Make sure you start the client first. They both
 send text to STDOUT.

 -stewart-
-------------- next part --------------
/**

 * UDP client/server test

 * Stewart Allen (stewart at neuron.com)

 */ 



import java.io.*;

import java.net.*;



class udpclient extends Thread {

    int port;



    public udpclient(int port)

    {

        this.port = port;

    }



    public void run()

    {

        DatagramSocket d;

        DatagramPacket p;

        byte data[] = new byte[1024];

        String sdata;

        p = new DatagramPacket(data,data.length);



        try {

            d = new DatagramSocket(port);

            int i=0;

            while (i<30) {

                d.receive(p);

                System.err.println("got packet "+(i++));



                Thread.yield();

            }

        } catch (IOException e) {

            System.err.println("client exception: "+e);

        }

    }



    public static void main(String args[])

    {

        udpclient me = new udpclient(7777);

        me.start();

    }

}



class udpserver extends Thread {

    String host;

    int port;



    public udpserver(String host, int port)

    {

        this.host = host;

        this.port = port;

    }



    public void run()

    {

        DatagramSocket d;

        DatagramPacket p;



        try {

            d = new DatagramSocket();

            String predata;

            byte data[];

            int i=0;



            while (i<40) {

                InetAddress a = InetAddress.getByName(host);

                predata = new String("test "+i+" ...");

                data = new byte[predata.length()];

                predata.getBytes(0,data.length,data,0);

                p = new DatagramPacket(data,data.length,a,port);



                System.err.println("sending packet "+(i++));

                d.send(p);



                Thread.yield();

            }

        } catch (IOException e) {

            System.err.println("server exception: "+e);

        }

    }



    public static void main(String args[])

    {

        udpserver me = new udpserver(args[0],7777);

        me.start();

    }

}



class udptest {

    public udptest() {

    }



    public static void main(String args[]) {

        udpclient client = new udpclient(7777);

        udpserver server = new udpserver("localhost",7777);

        System.err.println("starting client");

        client.start();

        System.err.println("starting server");

        server.start();

    }

}





More information about the kaffe mailing list