Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

UDP Sender and Receiver.

843790Oct 20 2008 — edited Oct 20 2008
Hi! I want to know if it is possible to convert the file UDPSender.java into a method that can be called by clicking a button? I slightly modified the UDPReceiver.java for it to be listening indefinitely and UDPSender.java I found on the net. And I created a what is supposedly my main file called Button.java.

What I want to do is that for this Button.java which only has a button when clicked is supposed to do a UDPSender. But I have no idea how to do it. Hope someone can answer this.

====================================================================================================================
====================================================================================================================

The code below is for UDPReceiver.java

====================================================================================================================
====================================================================================================================
/**
 * @(#)UDPReceiver.java
 *
 *
 * @author 
 * @version 1.00 2008/10/19
 */
import java.net.*;
import java.util.*;
import java.io.*;

public class UDPReceiver {
        
    /**
     * Creates a new instance of <code>UDPReceiver</code>.
     */
     
    public UDPReceiver() {
    }
    
    /**
     * @param args the command line arguments
     */
     
    public static void main(String args[]){
        try{
        	//Bound to socket 7 for receive
        	DatagramSocket socket = new DatagramSocket(7);
        	System.out.println("Bound to local port " +socket.getLocalPort());
        	
        	while(true){
        		//Create packet of 512 byte length
	        	DatagramPacket packet = new DatagramPacket(new byte[512], 512);
	        	socket.receive(packet);
	        	System.out.println("Packet received at " +new Date());
	        	
	        	//Display packet information
	        	InetAddress remote_addr = packet.getAddress();
	        	System.out.println("Sender: " +remote_addr.getHostAddress());
	        	System.out.println("from Port: " +packet.getPort());
	        	
	        	//Display packet contents, by reading from byte array
	        	ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());
	        	
	        	//Display only up to the length of the original UDP packet
	      		for(int i=0; i<packet.getLength(); i++){
	        		int data = bin.read();
	        		if(data==-1)
	        			break;
	        		else
	        			System.out.println((char)data);
	        	}  	
	        		
        	}        	
        }
        catch (IOException e) {
			System.out.println ("Error - " + e);
		}
    }
}
====================================================================================================================
====================================================================================================================

This code is for UDPSender.java

====================================================================================================================
====================================================================================================================
/**
 * @(#)UDPSender.java
 *
 *
 * @author 
 * @version 1.00 2008/10/19
 */
import java.net.*;
import java.util.*;
import java.io.*;

public class UDPSender {
        
    /**
     * Creates a new instance of <code>UDPSender</code>.
     */
    public UDPSender() {    
    }
    /**
     * @param args the command line arguments
     */
	public static void main(String args[]){
 	
    	//Set hostname and message
        String hostname = "localhost";
        String message = "Hello";
        
        try{
        	//Create a datagram socket and look for the first available port
        	DatagramSocket socket = new DatagramSocket();
        	System.out.println("Using local port: " +socket.getLocalPort());
        	
        	ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        	PrintStream pOut = new PrintStream(bOut);
        	pOut.print(message);
        	
        	//Convert printstream to byte array
        	byte[] bArray = bOut.toByteArray();
        	
        	//Create a datagram packet containing a maximum buffer of 512 bytes
        	DatagramPacket packet = new DatagramPacket(bArray, bArray.length);
        	
        	System.out.println("Looking for hostname " +hostname);
        	
        	//Get the InetAddress object
        	InetAddress remote_addr = InetAddress.getByName(hostname);
        	
        	//Check host IP
        	System.out.println("Hostname has IP address = " +remote_addr.getHostAddress());
        	
        	//Configure DatagramPacket
        	packet.setAddress(remote_addr);
        	packet.setPort(7);
        	
        	//Send packet
        	socket.send(packet);
        	System.out.println("Packet sent at " +new Date());
        	
        	//Display packet information
        	System.out.println("Sent by: " +remote_addr.getHostAddress());
        	System.out.println("Send from: " +packet.getPort()); 	
        }
	    catch (UnknownHostException ue){
			System.out.println("Unknown host "+hostname);
		}
		catch (IOException e) {
			System.out.println ("Error - " + e);
		}
    }
}
====================================================================================================================
====================================================================================================================

This is supposedly my main class file:

====================================================================================================================
====================================================================================================================
/**
 * @(#)Button.java
 *
 *
 * @author 
 * @version 1.00 2008/10/16
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet.*;

public class Button extends JApplet implements ActionListener{

	public static void main(String[] args){
	JFrame frame = new JFrame();
	frame.setVisible(true);
	
	JPanel panel = new JPanel();
	
	JButton button = new JButton("send text");
	panel.add(button);
	frame.add(panel);
	frame.pack();
	}
	
	public void actionPerformed(ActionEvent evt) {
		//Button.EchoClient();
	}
    
}
Edited by: Eastsheen on Oct 20, 2008 12:33 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2008
Added on Oct 20 2008
1 comment
452 views