Solved

Java DatagramSocket problem

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

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
javap bin 2 40
servlet example 17 51
maven not picking latest jar instead picking old jar from .m2 12 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

839 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