public class FiveClient extends JApplet implements Runnable{
JButton b1=new JButton("Send");
JTextArea t1=new JTextArea("",10,40);
JTextArea t2=new JTextArea("",10,40);
JScrollPane pane1,pane2;
Container ctp=getContentPane();
Thread rtd;
public void init(){
try{
ctp.setLayout(new FlowLayout());
ctp.add(b1); t1.setLineWrap(true);
t1.setFont(new Font("宋体",Font.PLAIN,16));
pane1=new JScrollPane(t1);
ctp.add(pane1); t2.setLineWrap(true);
t2.setFont(new Font("宋体",Font.PLAIN,16));
pane2=new JScrollPane(t2);
ctp.add(pane2); b1.addActionListener(new kolistener());
rtd=new Thread(this);
rtd.start(); //★★★启动线程,不能接收,甚至连发送都不行了
}
catch(Exception e){}
} public void run(){
while(true){
//接收
try{
Socket c1=new Socket("10.5.1.4",8888);
BufferedReader input=new BufferedReader(new InputStreamReader(c1.getInputStream()));
char []b=new char[200];
int n=input.read(b,0,200);
String str=new String(b,0,n);
System.out.println(str);
t1.setText(str);
input.close();
c1.close();
Thread.sleep(3000);
}
catch(Exception e){
System.out.print("Client Thread:"+e.toString());
}
}
} class kolistener implements ActionListener{
public void actionPerformed(ActionEvent ae){
//发送
try{Socket c=new Socket("10.5.1.4",8888);
PrintWriter output=new PrintWriter(c.getOutputStream(),true);
output.println(t2.getText());
output.close();
c.close();
}
catch(Exception ie){
t1.setText("Client2:"+ie.toString());
}
}
}
}
JButton b1=new JButton("Send");
JTextArea t1=new JTextArea("",10,40);
JTextArea t2=new JTextArea("",10,40);
JScrollPane pane1,pane2;
Container ctp=getContentPane();
Thread rtd;
public void init(){
try{
ctp.setLayout(new FlowLayout());
ctp.add(b1); t1.setLineWrap(true);
t1.setFont(new Font("宋体",Font.PLAIN,16));
pane1=new JScrollPane(t1);
ctp.add(pane1); t2.setLineWrap(true);
t2.setFont(new Font("宋体",Font.PLAIN,16));
pane2=new JScrollPane(t2);
ctp.add(pane2); b1.addActionListener(new kolistener());
rtd=new Thread(this);
rtd.start(); //★★★启动线程,不能接收,甚至连发送都不行了
}
catch(Exception e){}
} public void run(){
while(true){
//接收
try{
Socket c1=new Socket("10.5.1.4",8888);
BufferedReader input=new BufferedReader(new InputStreamReader(c1.getInputStream()));
char []b=new char[200];
int n=input.read(b,0,200);
String str=new String(b,0,n);
System.out.println(str);
t1.setText(str);
input.close();
c1.close();
Thread.sleep(3000);
}
catch(Exception e){
System.out.print("Client Thread:"+e.toString());
}
}
} class kolistener implements ActionListener{
public void actionPerformed(ActionEvent ae){
//发送
try{Socket c=new Socket("10.5.1.4",8888);
PrintWriter output=new PrintWriter(c.getOutputStream(),true);
output.println(t2.getText());
output.close();
c.close();
}
catch(Exception ie){
t1.setText("Client2:"+ie.toString());
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货