[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java Datagram receiver, sender not working

Posted on 2011-09-12
3
Medium Priority
?
331 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Suggested Courses

656 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