📄 printerclientconninfo.java
字号:
package PrinterClient;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class PrinterClientConnInfo extends Frame implements Runnable {
Panel panel1, panel2;
Button button1, button2,button3,button4;
TextField textField1, textField2;
Label label1;
TextArea textArea;
ScrollPane sPanel;
PrintWriter out;
BufferedReader in = null;
Socket sock;
public final static int DEFAULT_PORT = 6666;
private boolean isSendAlive=false;
Thread readThread;
TimerThread timerThread;
boolean isTrue = true;
String localIP="127.0.0.1";
public synchronized boolean getAliveFlag() { return isSendAlive; }
public synchronized void setAliveFlag(boolean value) {
sendInformation("@ON#"+localIP);
}
public PrinterClientConnInfo() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PrinterClientConnInfo pcc=new PrinterClientConnInfo();
pcc.setVisible(true);
}
private void jbInit() {// should set size,position and Font of every
// component
button1 = new Button("connect");// set Font
button2 = new Button("send");
button3 = new Button("startThread");
button4 = new Button("stopthread");
textField1 = new TextField("192.168.11.13");
textField2 = new TextField("input Message here and send to server");
label1 = new Label("message:");
panel1 = new Panel();
panel2 = new Panel();
sPanel = new ScrollPane();
textArea = new TextArea();
// panel1
// press button1: to connect the client to server
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
// textField1:for input the address of server;be registered to
// KeyListener.
// press key:Enter to connect the client to server
textField1.addKeyListener(new textField1_KeyAdapter(this));// java.awt.event.KeyAdapter()
panel1.add(button1);
panel1.add(textField1);
panel1.add(button3);
panel1.add(button4);
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button3_actionPerformed(e);
}
});
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button4_actionPerformed(e);
}
});
// sPanel ScrollPane
sPanel.add(textArea);
// panel2
// press button2: to send message
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
// textField2:for input message;be registered to KeyListener.
// press key:Enter to send message
textField2.addKeyListener(new textField2_KeyAdapter(this));
panel2.add(label1);
panel2.add(textField2);
panel2.add(button2);
// frame
this.addWindowListener(new FFrame_WindowAdapter(this));// frame is
// registered
// to
// WindowListener
this.setLayout(new BorderLayout());
this.setSize(500, 400);
this.add(panel1, BorderLayout.NORTH);
this.add(sPanel, BorderLayout.CENTER);
this.add(panel2, BorderLayout.SOUTH);
}
public void startConnect() {
try {
sock = new Socket(textField1.getText(), DEFAULT_PORT);
if (sock != null) {
processMsg("Connect successfully!");
}
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream());
sendInformation("@START#"+localIP);
} catch (IOException ex) {
processMsg(ex.toString());
processMsg("Connect failed!");
}
readThread = new Thread(this);
readThread.start();
timerThread = new TimerThread(this);
timerThread.start();
}
public void sendInformation() {
out.println(textField2.getText());
out.flush();
}
public void sendInformation(String value) {
out.println(value);
out.flush();
}
private void button1_actionPerformed(ActionEvent e) {
startConnect();
}
private void button3_actionPerformed(ActionEvent e) {
try{
timerThread = new TimerThread(this);
timerThread.start();
}catch(Exception ex)
{}
}
private void button4_actionPerformed(ActionEvent e) {
try {
//timerThread.interrupt();
timerThread.setRunState(false);
}catch(Exception ex)
{}
}
private void button2_actionPerformed(ActionEvent e) {
sendInformation();
}
public void stopRun() {
isTrue = false;
}
public void processMsg(String msg) {
textArea.append(msg);
textArea.append("\n");
}
public void run() {
String msg;
isTrue = true;
while (isTrue) {
try {
msg = in.readLine();
if(msg==null){
continue;
}
if (msg.equals("Server exit!")) {
processMsg(msg);
stopRun();
} else if (msg != null) {
processMsg(msg);
}
Thread.sleep(100);
} catch (IOException e) {
processMsg(e.toString());
} catch (InterruptedException ei) {
processMsg(ei.toString());
}
}
try {
in.close();
out.close();
} catch (IOException ioe) {
}
}
public void exit() {
stopRun();
try {
out.println("@EXIT#"+localIP);
out.flush();
} catch (Exception exc) {}
try {
//thread close
readThread.interrupt();
readThread=null;
timerThread.interrupt();
timerThread=null;
// close IOstream
in.close();
out.close();
sock.close();
} catch (IOException ioe) {
} finally {
System.exit(0);
}
}
}
class textField1_KeyAdapter extends java.awt.event.KeyAdapter {
PrinterClientConnInfo cFrame;
public textField1_KeyAdapter(PrinterClientConnInfo chatFrame) {
this.cFrame = chatFrame;
}
public void keyPressed(KeyEvent e) {
int j = e.getKeyCode();
if (j == e.VK_ENTER) {
cFrame.startConnect();
}
}
}
class textField2_KeyAdapter extends java.awt.event.KeyAdapter {
PrinterClientConnInfo clientFrame;
public textField2_KeyAdapter(PrinterClientConnInfo frame) {
this.clientFrame = frame;
}
public void keyPressed(KeyEvent e) {
int j = e.getKeyCode();
if (j == e.VK_ENTER) {
clientFrame.sendInformation();
}
}
}
class FFrame_WindowAdapter extends java.awt.event.WindowAdapter {
PrinterClientConnInfo clientFrame;
public FFrame_WindowAdapter(PrinterClientConnInfo frame) {
this.clientFrame = frame;
}
public void windowClosing(WindowEvent e) {
clientFrame.exit();
}
}
class TimerThread extends Thread {
private boolean isRun=true;
PrinterClientConnInfo clientframe;
TimerThread(PrinterClientConnInfo frame)
{
clientframe=frame;
}
public void setRunState(boolean value)
{
isRun=value;
}
public void run()
{
long startTime=System.currentTimeMillis();
long endTime;
while (isRun) {
endTime=System.currentTimeMillis();
if((endTime-startTime)>=60000){
startTime=System.currentTimeMillis();
clientframe.setAliveFlag(true);
}
try{
Thread.sleep(100);
} catch (Exception e) {}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -