Hi Dustin Lang,
 

    I have edited your code.  It is working fine, except for few changes.

    The sender to the same port you are listening to else you will get a time out error.
    Instead of socket.getLocalAddress().getHostAddress() use InetAddress. getLocalhost(). The former gives 0.0.0.0.
 

    These make the program work successfully.

Regards

TJP.
 

Dustin Lang wrote:

Hi again,

Further to my previous message.  It seems that the sending thread is also
bothered by the receiving thread.  The first send() succeeds, but once the
receive thread touches the socket, all further send()s fail with
"IOException: Connection refused".  This is a DatagramSocket!  I was under
the impression that this exception should never be thrown by
DatagramSocket.send() - packets are just sent off into the big blue
yonder; if nobody is listening on the destination machine, the packet
should just get tossed into the bit bucket.

Any suggestions would be appreciated.

Cheers,
dstn.

----------------------------------------------------
--     Dustin Lang, [EMAIL PROTECTED]    --
User, n.: a particularly  slow and unreliable input/
output  device  that  is  attached by default to the
standard input and output streams.
----------------------------------------------------

----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]


-- -----------------------------------------------
Tony J. Paul
AdventNet Development Center India (P) Ltd.,
13-A, Velachery Main Road
Velachery Chennai - 600 042
Ph: +91-044-2432414/2748/2749/3484 Ext : 419
Email :[EMAIL PROTECTED]
http://www.adventnet.com
-----------------------------------------------
When you aim for perfection, you discover it's a moving target. -George Fisher
 

import java.net.*;
import java.io.*;

class SocketTest implements Runnable {

    DatagramSocket socket;
    Thread receiver;
    static long time;

    public SocketTest() throws IOException {
        socket = new DatagramSocket(2000,InetAddress.getLocalHost());
        socket.setSoTimeout(5000);
        output("Datagram socket open on " + InetAddress.getLocalHost() 
                           + " port " + socket.getLocalPort() + ".");

        receiver = new Thread(this, "Receiver");
        receiver.start();
    }

    public void sendPacket() {
        try {
            byte[] packetData = new byte[1024];
            DatagramPacket packet = new DatagramPacket(packetData, packetData.length,
                                                   InetAddress.getLocalHost(), 2000);
            output("Sending packet...");
            socket.send(packet);
            output("Sent packet.");
        } catch (Exception e) {
            output("Send failed: " + e);
        }
    }

    public void run() {
        output("Thread started.");

        int packetSize = 1024;
        byte[] packetData = new byte[packetSize];

        DatagramPacket packet = new DatagramPacket(packetData, packetSize);

        for (;;) {
            output("Receiving packet...");
            try {
                socket.receive(packet);
            } catch (InterruptedIOException ie) {
                output("Receive timed out.");
            } catch (IOException ioe) {
                output("IO Exception: " + ioe);
                output("Stack trace:");
                output("---------------------");
                ioe.printStackTrace();
                output("---------------------");
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ie) {
            }
        }
    }

    static void output(String s) {
        System.out.println("[" + Thread.currentThread().getName() + " "
        + ((System.currentTimeMillis()-time)/1000) + "]: " + s);
    }

    public static void main(String[] args) {
        Thread.currentThread().setName("Sender  ");
        time = System.currentTimeMillis();
        try {
            SocketTest st = new SocketTest();
            for (;;) {
                st.sendPacket();
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException ie) {
                }
            }
        } catch (IOException ioe) {
            output("IO Exception: " + ioe);
            ioe.printStackTrace();
        }
    }

}

Reply via email to