Solved

Java DatagramSocket problem

Posted on 2010-09-06
5
638 Views
Last Modified: 2012-05-10
I have an application which sends a DatagramPacket to another by using a DatagramSocket. The sending application has a loop which gets a data from user (2 bytes) and then sends it to the receiving application. The receiving application has a loop which gets data from the DatagramSocket and prints it to console.

First packet is sended ok and showed to console by the receiver. However, after that, when receiver repeats the loop, the instruction dg.receive(p); doesn't seems to keep wainting until new data is received.

This is the code on the receiver:

                  dg = new DatagramSocket(port);
                  while(true)
                  {
                        p = new DatagramPacket(buf,2);
                        dg.receive(p);
                        buf = p.getData();
                        nvia = buf[0];
                        if ((nvia <= nvies) && (nvia>0))
                              vies[nvia-1] = buf[1];
                        
                        // Mostro l'estat de les vies
                        System.out.println("\n\n\n");
                        System.out.println("Estat de les vies");
                        System.out.println("-----------------");
                        for (nvia=1; nvia<=nvies; nvia++)
                              System.out.println("Via " + nvia + ":" + vies[nvia-1]);
                  }
0
Comment
Question by:gplana
[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
  • 2
5 Comments
 
LVL 15

Author Comment

by:gplana
ID: 33612450
The error message that appears on the client is:
No buffer space available (maximum connections reached?): Cannot bind

The server seems to enter to an infinite looping without waiting for data to arrive.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33624992
        Hi!

Take a look at this example on how to read/write a DataGramPacket through DataGramSocket.
http://www.java2s.com/Code/Java/Network-Protocol/ReadandwritewithDatagramPacket.htm

Remember to close the socket after sending/receiving the data to allow other connections to
be established.

Regards,
    Tomas Helgi
0
 
LVL 15

Author Comment

by:gplana
ID: 33626402
Thank you. Just a question: if I want a loop which sends a message on every iteration on the loop,  should I close and open again or can I maintain the datagramsocket open for all messages and close at the end of the program ?
0
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 33629248
        Hi!

Yes you could maintain the datagramsocket open while the application starts and close it
when the program quits. See this example
http://www.java2s.com/Code/Java/Network-Protocol/UseDatagramSockettosendoutandreceiveDatagramPacket.htm


Other links to look at
http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html
http://www.codeguru.com/java/tij/tij0166.shtml

Regards,
    Tomas Helgi
0
 
LVL 15

Author Closing Comment

by:gplana
ID: 33629669
Thank you very much.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

631 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