我的程序挂起文件我在Udp程序中调用接收方法,如何在没有线程使用的情况下停止/避免这种情况 [英] My Program Hangs Wen I Call Receive Method In Udp Program, How Can I Stop/Avoid This Without Thread Usage

查看:107
本文介绍了我的程序挂起文件我在Udp程序中调用接收方法,如何在没有线程使用的情况下停止/避免这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个UDP客户端程序,这是我收到消息的代码段。我无法弄清楚什么是错的,因为该方法一直挂起,直到计时器启动,它永远不会收到任何东西。如果有任何错误,请查看此代码。







public void receive()抛出IOException,ClassNotFoundException {



//文件接收

byte receive_buffer [] =新字节[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); //测试



//转换之前收到的字节信息对象

Object o = new Object();

ByteArrayInputStream bo = new ByteArrayInputStream(receive_buffer);

ObjectInputStream oo = new ObjectInputStream(bo);

o = oo.readObject();



}

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();

}

推荐答案

请参阅我对此问题的评论。错误的问题,真的。它不是挂。你的线程在某些电话中被阻止,比如接听电话。这就是它应该如何工作。 (并且,如果你告诉我我的代码没有工作线程:没有没有线程使用这样的东西;你的代码总是在某个线程中执行,这个线程被阻止 - 我的意思是。)现在,线程进入一个特殊的等待状态(如果这是一个真正正确的阻塞调用,而不是一个愚蠢的旋转等待)并且没有花费任何CPU时间,直到它是唤醒。在这种情况下,它可以通过超时唤醒, Thread.Interrupt(),Thread.Abort()或实际接收预期的数据量。一般来说,你不应该停止而不是避免它。您应该从不同的流程发送预期数量的数据。



这就是全部。在不了解您的目标和进一步细节的情况下,很难就此提供更多细节。



-SA
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

这篇关于我的程序挂起文件我在Udp程序中调用接收方法,如何在没有线程使用的情况下停止/避免这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆