import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
public class cilent_p313 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
new ComputerCilent();
}
}
class ComputerCilent extends Frame implements Runnable,ActionListener{
Button connection,send;
TextField inputText,showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
ComputerCilent(){
socket=new Socket();
setLayout(new FlowLayout());
Box box=Box.createVerticalBox();
connection=new Button("连接");
send=new Button("send");
inputText=new TextField(12);
showResult=new TextField(12);
box.add(connection);
box.add(new Label("输入三角形的长度,逗号分隔"));
box.add(inputText);
box.add(new Label("show Result"));
box.add(send);
box.add(showResult);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
add(box);
setBounds(12,12,444,444);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void actionPerformed(WindowEvent we){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==connection){
try{
if(socket.isConnected()){}
else{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
thread.start();
}
}
catch(IOException IOE){}
}
if(ae.getSource()==send){
String s=inputText.getText();
if(s!=null){
try{
out.writeUTF(s);
}
catch(IOException IOE2){}
}
}
}
public void run(){
String s=null;
while(true){
try{s=in.readUTF();
showResult.setText(s);
}
catch(IOException IOE3){
showResult.setText("Lost Connection");
}
}
}
}
java程序设计基础教程第二版: cilent_p313
最新推荐文章于 2024-03-26 10:17:55 发布