Solved

Java DatagramSocket problem

Posted on 2010-09-06
5
635 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
  • 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

Technology Partners: 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
configure dependency in POM for new database 3 44
Running JavaFX on JDeveloper 12C 1 76
ejb entity bean example 2 34
Selenium docs api java index 3 65
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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