Click here to Skip to main content
15,885,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a UDP client program and this is my piece of code where i receive a message. I cant figure out whats wrong because the method hangs until timer is up and it never receives anything. please review this code for any wrongs.



public void receive() throws IOException, ClassNotFoundException{

//file receiving
byte receive_buffer[] = new byte[10000];
DatagramPacket receive_packet = new DatagramPacket(receive_buffer, receive_buffer.length);
receive_packet.setAddress(IP);
receive_packet.setPort(13);
socket.receive(receive_packet);
System.out.print("inside2"); //testing

//converting received bytes info object it was before
Object o = new Object();
ByteArrayInputStream bo = new ByteArrayInputStream(receive_buffer);
ObjectInputStream oo = new ObjectInputStream(bo);
o = oo.readObject();

}
Posted
Comments
Sergey Alexandrovich Kryukov 25-Aug-14 21:42pm    
Using network communications without threads would be a real madness, even when it's possible in principle. Why?
—SA

1 solution

Please see my comment to this question. Wrong question, really. It's not "hanging". Your thread gets blocked at some calls, like receive calls. This is how it is supposed to work. (And please, in case you tell me "my code does not work threads": there is no such thing as "without thread usage"; your code is always executed in some thread, and this thread gets blocked — I mean it.) Now, the thread gets to a special wait state (if this is a real correct blocking call, not a stupid spin wait) and is not spending any CPU time until it is awaken. In this case, it can be awaken by timeout, Thread.Interrupt(), Thread.Abort() or actually receiving expected amount of data. Generally, you should neither "stop" not "avoid" it. You should send expected amount of data from a different process.

That's all. Without knowing your goals and further detail, it's hard to advise any more detail on this.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900