Solved

Java Datagram receiver, sender not working

Posted on 2011-09-12
3
314 Views
Last Modified: 2012-05-12
Hi,
The attached code has my listener.java and sender.java

and I have no idea why they don't connect.

I'm trying to send a message, datagram from one port, to itself, in essence.

This is necessary debugging, because it will eventually be to another machine's port.

It shouldn't take someone who really knows java.net long to see the problem, I'm sure.
Clicking the Frame sends the message.
I make a JFrame with a button to click, to send the message to the receive loop.
Any suggestions? Thanks - - Rewrite, maybe?
// SENDER


import java.awt.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.net.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

import javax.swing.*;
public class sender extends JFrame implements WindowListener, MouseListener {
	
	
	
	public DatagramSocket sendSocket; 
	JButton button;
	
	
	public void windowActivated(WindowEvent w) {
		
	}
	public void windowClosed(WindowEvent w) {
		
	}
	public void windowClosing(WindowEvent w) {
		
	}
	public void windowDeiconified(WindowEvent w) {
	}
	
		
	public void windowDeactivated(WindowEvent w) {
		
	}
	public void windowOpened(WindowEvent w) {
		
	}
	public void windowIconified(WindowEvent w) {
	
}
	int thisport;
	public void mouseClicked(MouseEvent me){
		
		System.out.println("Clicked");
		try {
		byte[] outbuf = new byte[256];


		InetAddress dst = InetAddress.getLocalHost();
		
		DatagramPacket request = new DatagramPacket(outbuf,outbuf.length,dst,8970);
		
		sendSocket.send(request);
		} catch (Exception e) {
			System.out.println("mouseclicked Test : "+e);
		}
		
	}
public void mouseEntered(MouseEvent me){
		
	}
public void mousePressed(MouseEvent me){
	
}
public void mouseExited(MouseEvent me){
	
}
public void mouseReleased(MouseEvent me){
	
}
	public sender() {
			try {
			InetAddress dst = InetAddress.getLocalHost();
			byte[] outbuf = new byte[256];
			this.setBackground(Color.yellow);
			button = new JButton();
			button.setSize(200,100);
			button.setLocation(90,90);
			button.setText("send msg");
			addMouseListener(this);
			
			outbuf[0]=69;
			int len = 1024;
			this.setSize(800,800);
			this.setVisible(true);
			
			
			//DatagramPacket request = new DatagramPacket(outbuf,len,dst,listener.PORT);
			sendSocket =new DatagramSocket();
			System.out.println("send socket made");
			} catch (Exception e) {System.out.println("sender() excep : "+e);
							
			}
			
			
	}

}


//RECEIVER

import java.net.*;


public class listener extends Thread{
	
	public DatagramSocket listenDGSocket;
	public static int PORT=8970;
	DatagramPacket dgp;
	byte[] buf;

	
	public void run() {
		System.out.println("Listen Thread run() method(listening Thread)");
		
		buf = new byte[256];
		
		try {
		System.out.println("Starting (1==1) listen loop in run()");
	    while (1==1) {
	    	System.out.println("Listen loop");
	    	DatagramPacket packet = new DatagramPacket(buf, buf.length);
	    	
	    	listenDGSocket.receive(packet);
	    	System.out.println("Received");
	    } 	
	    } catch (Exception e) { System.out.println("listener run excep : "+e);}
		
	}
	public listener() {
		
		try {
		byte[] inbuf;
		inbuf = new byte[256];

		sender sndr = new sender(); //sender is a client

		listenDGSocket = new DatagramSocket();
		
	    start();
		

		} catch (Exception e) {
			System.out.println("listener() excep: "+e);
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("listener main()");

		new listener(); //start listening
		System.out.println("Back in main() after listener() built");
		
		
		
		
		
		
		
	}

}

Open in new window

0
Comment
Question by:beavoid
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
Comment Utility
I can't say that I'm a specialis in java.net but shouldn't you reference the smae port on the listener side
I see
public static int PORT=8970;

but don't see where PORT is used on the listener side
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
I agree with for_yan, you need to bind the listening socket to the port that you want to send to. Therefore, line 140 of the above code should be....

listenDGSocket = new DatagramSocket(PORT);

0
 

Author Comment

by:beavoid
Comment Utility
How embarrassing.

Thank you

Nowhere in the java examples had I seen the constructor take an integer for the port!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now