⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udplistener.java

📁 基于网络的AIBO机器狗遥操作控制程序代码
💻 JAVA
字号:
import java.net.DatagramSocket;import java.net.DatagramPacket;import java.net.InetAddress;public abstract class UDPListener extends Listener {  protected abstract void connected(DatagramSocket socket, DatagramPacket firstPacket);  byte[] incomingbuf = new byte[1<<16];  DatagramPacket incoming = new DatagramPacket(incomingbuf, incomingbuf.length);  byte[] buf = (new String("connection request")).getBytes();	int _lastPort=-1; // keep track of previously used port number so we can resume connections  protected void runServer() {		System.out.println("ERROR: UDP server listener is not implemented");    try {      //_socket=new DatagramSocket(_port);      try {        //_socket.connect(InetAddress.getByName(_host), _port);        //_socket.setSoTimeout(10000); // block for 10 seconds at most (if infinite, we can't automatically detect a closed connection & then reconnect      } catch (Exception ex) { }      // send a dummy message so that the AIBO can see what      // address to connect it's UDP socket to			/*      DatagramPacket message = new DatagramPacket(buf, buf.length,                                                  InetAddress.getByName(_host),                                                  _port);      _socket.send(message);			*/      //connected(_socket);    } catch (Exception ex) {      System.out.println("port "+_port+": "+ex);    }  }  protected void runConnect() {		int attempts=0;		Thread me = Thread.currentThread();		while (me==_listenerThread && !destroy) {			if(attempts==0) {				System.out.println("["+_port+"] connecting ...");			}			try {				if(_lastPort==-1)					_socket=new DatagramSocket();				else					_socket=new DatagramSocket(_lastPort);				_lastPort=_socket.getLocalPort();				_socket.connect(InetAddress.getByName(_host), _port);				// send a dummy message so that the AIBO can see what				// address to connect it's UDP socket to				DatagramPacket message = new DatagramPacket(buf, buf.length);				_socket.send(message);				_socket.setSoTimeout(500);				_socket.receive(incoming);				_socket.setSoTimeout(0); //set to be blocking				System.out.println("["+_port+"] connected ");				attempts=0;				_isConnected=true;			} catch (Exception ex) {}			if(_isConnected) {				connected(_socket,incoming);				if(!destroy)					System.out.println("["+_port+"] disconnected, attempting to reestablish ..");			}			attempts++;			if(destroy) {				System.out.println("["+_port+"] connection closed");				break;			}			try {				Thread.sleep(500);			} catch (Exception ex) {}		}  }  public void close() {	_listenerThread=null;	_isConnected=false;		try{_socket.close();}	catch(Exception e){}  }  public UDPListener() { super(); }  public UDPListener(int port) { super(port); }  public UDPListener(String host, int port) { super(host, port); }  DatagramSocket _socket;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -