Results 1 to 5 of 5
  1. #1
    cr0ck3t is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default DatagramChannel - UDP receiving OK, but unable to send data :(

    I am trying to connect to a local service over UDP - the simulator works fine, but my code does not... The sender is a separate thread and I have tried blocking, non-blocking, connecting, not connection, reading and re-reading the Oracle docs, and am pretty much google-e-eyed now Here's my latest refactoring:

    Java Code:
    package my.comm;
    
    import my.myMethods;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    /**
     *
     * @author dave
     */
    public class myConnection implements Runnable {
    	DatagramChannel datagramChannel;
    	
    	private final String HOST = "127.0.0.1";
    	private final int PAYMENT_PORT = 4444;
    	private final int MY_PORT = 8888;
    	
    	private final Queue<byte[]> incommingDataPackets = new LinkedList();
    	private final Queue<byte[]> outgoingDataPackets = new LinkedList();
    	
    	private final ExecutorService exec = Executors.newFixedThreadPool(1);
    	private final myMethods myMethods = new myMethods();
    	
    	private static final Logger log = LogManager.getLogger();
    	
    	public myConnection() throws IOException {
    		datagramChannel = DatagramChannel.open();
    		datagramChannel.bind(new InetSocketAddress(HOST, PAYMENT_PORT));
    		datagramChannel.connect(new InetSocketAddress(HOST, MY_PORT));
    		datagramChannel.configureBlocking(true);
    		
    		myReceiver myReceiver = new myReceiver(this, datagramChannel.socket());
    		Thread mtdrThread = new Thread(myReceiver);
    		exec.execute(mtdrThread);
    	}
    	
    	public synchronized byte[] receiveData() {
    		byte[] receivedBytes = new byte[0];
    		if(incommingDataPackets.size() > 0) {
    			receivedBytes = incommingDataPackets.remove();
    			log.debug("receiving {}", myMethods.hexRepresentationOfByteBuffer(receivedBytes));
    		}
    		return receivedBytes;
    	}
    	
    	public synchronized void transmitData(byte[] sendData) {
    		outgoingDataPackets.add(sendData);
    		notifyAll();
    	}
    	
    	@Override
    	public void run() {
    		try {
    			while(true){
    				while(outgoingDataPackets.size() > 0) {
    					byte[] outgoingBytes = outgoingDataPackets.remove();
    					
    					ByteBuffer outgoingByteBuffer = ByteBuffer.allocate(outgoingBytes.length);
    					outgoingByteBuffer.clear();
    					outgoingByteBuffer.put(outgoingBytes);
    					outgoingByteBuffer.flip();
    					
    					log.debug("sending	{}", myMethods.hexRepresentationOfByteBuffer(outgoingBytes));
    					int sentBytes = datagramChannel.write(outgoingByteBuffer);
    					log.debug("sent {} bytes of data ", sentBytes);
    				}
    			}
    		} catch (IOException ex) {
    			log.fatal("IOException!	{}", ex.getMessage());
    		}
    	}
    	
    	public synchronized void addIncommingDatagramGuts(byte[] receivedBytes) {
    		incommingDataPackets.add(receivedBytes);
    		notifyAll();
    	}
    }
    And the thread which receives the data:

    Java Code:
    package my.comm;
    
    import my.myMethods;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.nio.ByteBuffer;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    /**
     *
     * @author Dave
     */
    class myReceiver implements Runnable {
    	private final DatagramSocket datagramSocket;
    	private DatagramPacket datagramPacket;
    	
    	int MAX_PACKET_SIZE = 1500;
    	byte[] incommingBytes = new byte[MAX_PACKET_SIZE];
    	
    	private final myConnection myConnection;
    	private final myMethods myMethods = new myMethods();
    	private static final Logger log = LogManager.getLogger();
    	
    	myReceiver (myConnection myConnection, DatagramSocket datagramSocket) {
    		this.myConnection = myConnection;
    		this.datagramSocket = datagramSocket;
    	}
    
    	@Override
    	public void run() {
    		while(true) {
    			try {
    				datagramPacket = new DatagramPacket(incommingBytes, MAX_PACKET_SIZE);
    				while(true){
    					datagramSocket.receive(datagramPacket);
    					myConnection.addIncommingDatagramGuts(myMethods.trimFrame(datagramPacket.getData()));
    				}
    			} catch (IOException ex) {
    				log.error("IOException!	{}", ex.getMessage());
    			}
    		}
    	}
    }
    All assistance is much appreciate!!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: DatagramChannel - UDP receiving OK, but unable to send data :(

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    cr0ck3t is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: DatagramChannel - UDP receiving OK, but unable to send data :(

    Quote Originally Posted by Norm View Post
    Is it ok to post in two places?... is there a third I should know about? ;) Once I get an answer I will copy it to all forums so it will hopefully help someone by reducing their google search in the future...

  4. #4
    cr0ck3t is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: DatagramChannel - UDP receiving OK, but unable to send data :(

    I sorted it - 3am coding issue :S

    Missed a
    Java Code:
    try { Thread.sleep(500); } catch (InterruptedException ignore) { }
    in a while(true){...} loop.

    Here's the completed code in case it helps anyone with setting up a java UDP server
    Attached Files Attached Files

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: DatagramChannel - UDP receiving OK, but unable to send data :(

    Quote Originally Posted by cr0ck3t View Post
    Is it ok to post in two places?... is there a third I should know about? ;) Once I get an answer I will copy it to all forums so it will hopefully help someone by reducing their google search in the future...
    It's OK, it's just considered a good idea to inform people where you've asked the same question so we can avoid duplicating work. Some here post in several forums and it's a bit annoying to write a reply only to discover the self same answer has been given elsewhere.

    Thanks for posting the solution by the way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2013, 02:24 PM
  2. Receiving GPS data through Comm port
    By steven6282 in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 10:02 PM
  3. Event based serial data receiving GUI app
    By NiNja07 in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2011, 10:14 PM
  4. Replies: 7
    Last Post: 06-22-2010, 10:49 AM
  5. Unable to read data from inputstream
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 03:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •