Solved

Java Datagram receiver, sender not working

Posted on 2011-09-12
3
322 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
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 35

Assisted Solution

by:mccarl
mccarl earned 250 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

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 39
Website checklist for browser compatibility? 2 39
JDBC performance 8 29
TLS 1.0 & Windows 7 - How to disable? 16 129
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

730 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