📄 inputthread.java
字号:
/**
* Email: taorundong@126.com
*
* @author taorundong
* @version 1.00 07/02/06
*/
import javax.swing.*;
import java.net.*;
import java.io.*;
public class InputThread implements Runnable{
private
DataInputStream input = null;
DataOutputStream output = null;
String message = null;
Socket own = null;
JTextArea chatingContent = null;
JTextField chatingMessage = null;
Thread inputThread = null;
Thread outputThread = null;
InputThread(JTextArea text,JTextField text1){
try{//here connect to server
// byte []addr = Byte.valueOf(new String("58.212.139.218"));
// System.out.println(addr);
own = new Socket((InetAddress.getByName("127.0.0.1")),6000);
}
catch(Exception e){
e.printStackTrace();
new State("Warning");//here will show the state window when unconnect to server
}
try{
input = new DataInputStream(own.getInputStream());
output = new DataOutputStream(own.getOutputStream());
}
catch(Exception e){
e.printStackTrace();
}
chatingContent = text;
chatingMessage = text1;
initChating();
inputThread = new Thread(this);
outputThread = new Thread(this);
inputThread.start();
outputThread.start();
}
public void initChating(){
if(own.isConnected()){
try{
output.writeUTF(NameRequest.getClientName());
message = input.readUTF();
//System.out.println(getMessage());
chatingContent.append(message+"\n");
}
catch(Exception e){
e.printStackTrace();
initError();
}
}
}
public void run(){
//distinguish the two of the datastream
while(!own.isClosed()){
if(Thread.currentThread()==inputThread){
try{
message = input.readUTF();
System.out.println(message);
chatingContent.append(getMessage()+"\n");
}
catch(Exception e){
e.printStackTrace();
// Thread.currentThread().destroy();
//show the window that server maybe closed now
new State("Warning");
System.exit(0);
}
}
if(Thread.currentThread()==outputThread){
try{ //The outputData is provide by the ActionEvent of JTextFIeld
Thread.sleep(25); //to make the best performance of the datatransmision
// output.writeUTF();
}
catch(Exception e){
e.printStackTrace();
// Thread.currentThread().destroy();
//show the window that server maybe closed now
System.exit(0);
}
}
}
}
public DataOutputStream getOutput(){
return output;
}
public String getMessage(){
return message;
}
public String initError(){//To show the chating information
return "Net init error!";
}
public String chatError(){
return "Error when chating!";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -