?
Solved

Java Datagram receiver, sender not working

Posted on 2011-09-12
3
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 36527247
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 36

Assisted Solution

by:mccarl
mccarl earned 1000 total points
ID: 36527398
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
ID: 36529438
How embarrassing.

Thank you

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
Make the most of your online learning experience.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

770 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