Received: from SOUTH-STATION-ANNEX.MIT.EDU by po7.MIT.EDU (5.61/4.7) id AA17319; Fri, 1 Dec 95 14:02:41 EST
Received: from liquor.cabi.net by MIT.EDU with SMTP
	id AA08725; Fri, 1 Dec 95 14:01:28 EST
Received: (from listadm@localhost) by liquor.cabi.net (8.6.12/8.6.12) id NAA07414; Fri, 1 Dec 1995 13:27:19 -0500
Resent-Date: Fri, 1 Dec 1995 13:27:19 -0500
From: Thomas Boutell <boutell@boutell.com>
Message-Id: <199512011840.AA29020@boutell.com>
Subject: Datagram Test Code, etc.
To: java-linux@java.blackdown.org
Date: Fri, 1 Dec 1995 10:40:18 -0800 (PST)
X-Mailer: ELM [version 2.4 PL24]
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Length:       2062
Resent-Message-Id: <"ImfF73.0.co1.OVqlm"@liquor>
Resent-From: java-linux@java.blackdown.org
X-Mailing-List: <java-linux@java.blackdown.org> archive/latest/590
X-Loop: java-linux@java.blackdown.org
Precedence: list
Resent-Sender: java-linux-request@java.blackdown.org

The datagram test code I originally posted courtesy of Sun's
engineers, and which recently appeared on this mailing list,
is definitely for UDP Internet datagrams. Note the need for
a host and port to send the datagrams to and so forth. It 
has nothing to do with Unix domain sockets.

Alas, I am still waiting for even one other person to actually
test the code and report their results, operating system and
Java JDK version to me so we can try to get datagram
support fixed. Here it is again.

Save this code as demo.java, then do:

javac demo.java
java DatagramServer &
(note the port number output by the above)
java DatagramClient localhost portnumber

Please tell me what results you obtain. Thanks!

-T

import java.io.*;
import java.net.*;
import java.util.*;
import sun.net.*;

class DatagramServer {
    public static void main(String argv[]) {
	try {
	    DatagramSocket s = new DatagramSocket();
	    System.out.print("Datagram Server listening on port:");
	    System.out.println(s.getLocalPort());
	    byte buf[] = new byte[10];
	    DatagramPacket dp = new DatagramPacket(buf, 10);
	    s.receive(dp);
	    System.out.print("Received from inet address: "
			     + dp.getAddress().toString());
	    System.out.print(" port number: ");
	    System.out.print(dp.getPort());
	    for (int i=0; i<buf.length; ++i)  System.out.println(buf[i]);
	    s.close();
	} catch (Exception e) {
	}
    }
}

class DatagramClient {
    public static void main(String argv[]) {
	if (argv.length!=2) {
	     System.out.println("java DatagramClient <hostname> <port#>");
	     return;
	}
	try {
	    int port = Integer.parseInt(argv[1]);
	    InetAddress in = InetAddress.getByName(argv[0]);
	    // InetTuple tup = new InetTuple(in, atoi(argv[1]);
	    DatagramSocket s = new DatagramSocket();
	    byte buf[] = new byte[10];
	    for (int i=0; i<buf.length; ++i) buf[i] = (byte)i;
	    DatagramPacket dp = new DatagramPacket(buf, 10, in, port);
	    s.send(dp);
	    s.close();
	} catch (Exception e) {
		System.out.println("Something bad happened.");
	}
    }
}

