【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-ReceiverThread.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package udpecho;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import java.net.*;
import java.io.*;
public class ReceiverThread extends Thread {
DatagramSocket socket;
private boolean stopped=false;
public ReceiverThread(DatagramSocket ds)throws SocketException {
this.socket =ds;
}
public void halt(){
this.stopped=true;
}
public void run(){
byte[] buffer=new byte[65507];
while(true){
if(stopped) return;
DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
try{
socket.receive(dp);
String s=new String(dp.getData(),0,dp.getLength());
System.out.println("服务器返回收到的信息是:");
System.out.println(s);
System.out.println("***************************");
Thread.yield();
}
catch(IOException e){
System.err.println(e);
}
}
}
}