监听无线设备JAVA代码 监听事件java
关于java的监听器
1、public void addWindowListener(WindowListener l)添加指定的窗口侦听器,以从此窗口接收窗口事件。如果 l 为 null,则不抛出任何异常,且不执行任何操作。
创新互联公司主营银州网站建设的网络公司,主营网站建设方案,成都app软件开发,银州h5微信小程序搭建,银州网站营销推广欢迎银州等地区企业咨询
这个是API中的方法定义,此方法参数为接口WindowListener,任何实现该接口的类都可以作为参数。
2、public abstract class WindowAdapter implements WindowListener, WindowStateListener, WindowFocusListener
接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。
扩展此类可创建 WindowEvent 侦听器并为所需事件重写该方法。(如果要实现
WindowListener 接口,则必须定义该接口内的所有方法。此抽象类将所有方法都定义为
null,所以只需针对关心的事件定义方法。)
使用扩展的类可以创建侦听器对象,然后使用窗口的 addWindowListener
方法向该窗口注册侦听器。当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将
WindowEvent 传递给该方法。
3、如果我想在代码中一次性使用某个类(抽象类或具体类)或接口,可以使用匿名类的方式,这样不需自己定义一个My***类,然后再使用,比较方便。用法就是直接在new WindowAdapter()后面加入类定义,在其中实现或覆盖方法就可以了。
匿名类不是返回值,而是相当于new String(“hello”)这种的扩展形式。我觉得匿名类的最多用处就是加监听器时。
附上WindowAdapter源代码:
public abstract class WindowAdapter
implements WindowListener, WindowStateListener, WindowFocusListener
{
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowStateChanged(WindowEvent e) {}
public void windowGainedFocus(WindowEvent e) {}
public void windowLostFocus(WindowEvent e) {}
}
如何用JAVA代码监听某个开放端口
比如我要监听1234这个端口,代码如下:
String ip = "127.0.0.1";
int port = 1234;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();
byte[] date = "hello world".getBytes();
out.write(data);
out.flush();
byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.txt");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
java.io.InputStream in = socket.getInputStream();
while ((len = in.read(buffer, 0, buffer.length)) 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();
byte[] rdata = bout.toByteArray();
System.out.println(new String(rdata));
fout.write(rdata);
fout.flush();
fout.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
服务器端的
ServerSocket ss = new ServerSocket(1234);
Socket socket=null;
BufferedReader in;
PrintWriter out;
while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}
求JAVA的监听5038端口的代码。。。
服务端:
package com.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(5038);
while(true){
System.out.println("正在监听...");
if((socket = server.accept()) != null){
System.out.println("接收到一个请求"+ socket.getRemoteSocketAddress());
new Thread(new OperThread(socket)).start();
}
}
} catch (IOException e) {
System.out.println("waiting");
}
if(!socket.isConnected()){
socket.close();
server.close();
}
}
}
class OperThread implements Runnable{
Socket socket = null;
BufferedReader br = null;
public OperThread(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run() {
try {
String s = null;
while( (s = br.readLine()) != null){
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客户端:
package com.socket;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter pw = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",5038);
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String s = null;
while((s = scanner.nextLine()) != null){
pw.println(s);
pw.flush();
}
} catch (Exception e) {
System.out.println("\nconnect error");
}finally{
if(pw != null){
pw.close();
}
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
先开服务端,再开客户端。
当前文章:监听无线设备JAVA代码 监听事件java
网站路径:http://azwzsj.com/article/dopeppg.html