Solved

Java DatagramSocket problem

Posted on 2010-09-06
5
624 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
Comment Utility
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 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
        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
Comment Utility
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 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
Comment Utility
        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
Comment Utility
Thank you very much.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now