Message1866

Author rluse
Recipients
Date 2007-12-24.10:29:44
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Okay, I did write what I think you are expecting in Java, and it does give the results that you want, I believe.  I am putting that program on the bottom of this comment.  

It is interesting that if you put a try except around the write on the Jython script and set blocking to False, both Python and Jython produce similar, (but wrong) results.  I think now that the problem is beyond simple error handling and I do not know the Socket.py code well enough.  I am willing to work on it, but I will need input from Alan or Mr. Envey ot someone else with more expertise than myself.

To me, its an unusual solution to require a UDP write from a bound UDP port.  However, there is no reason that I can think of why it should not be okay and obviously it works cleanly in Java, if not in Python.  It looks like the Python guys missed it too.  The documentation is vague on this.  Anyway, here is the Java code:

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

public class JavaTestUDP extends Thread {

	DatagramSocket socket;
	byte[] buffer = new byte[8000];
	DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

	public JavaTestUDP(DatagramSocket socket) {
		this.socket = socket;
	}

	public void run() {

		while (true) {
			try {
				Thread.sleep(2000);
				System.out.println("Before socket receive");
				socket.receive(packet);
			} catch (Exception e) {
				System.out.println(e);
			}
			String s = new String(packet.getData(), 0, packet.getLength());
			System.out.println(packet.getAddress() + " at port "
					+ packet.getPort() + " says " + s);
			packet.setLength(buffer.length);
		}
	}

	public static void main(String[] args) throws SocketException {

		DatagramSocket socket = new DatagramSocket(5555);

		Thread t = new JavaTestUDP(socket);
		t.start();

		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			String sendString = "Hi";
			byte[] data = sendString.getBytes();
			try {
				InetAddress server = InetAddress.getByName("localhost");
				DatagramPacket sendPacket = new DatagramPacket(
                                                     data, data.length, server, 5556);
				System.out.println("!!");
				socket.send(sendPacket);
			} catch (UnknownHostException e) {
				System.out.println(e);
			}
			catch (IOException e) {
				System.out.println(e);
			}
			System.out.println("   done");
		}
	}
}
History
Date User Action Args
2008-02-20 17:18:00adminlinkissue1782548 messages
2008-02-20 17:18:00admincreate