Solved

Java Datagram receiver, sender not working

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macbook Sierra OS OpenVPN issue 13 82
tomcat administrtor 12 47
Nimble Storage 3 69
How to simulate latency? 5 30
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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