?? 例11-6.txt
字號:
① 客戶端程序(效果如圖11.6所示)
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClientFrame extends JFrame
implements Runnable,ActionListener{
JButton connection,computer;
JTextField inputA,inputB,inputC;
JTextArea showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
public ClientFrame(){
socket=new Socket(); //待連接的套接字
connection=new JButton("連接服務器");
computer=new JButton("求方程的根");
computer.setEnabled(false); //沒有和服務器連接之前,該按鈕不可用
inputA=new JTextField("0",12);
inputB=new JTextField("0",12);
inputC=new JTextField("0",12);
Box boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("輸入2次項系數"));
boxV1.add(new JLabel("輸入1次項系數"));
boxV1.add(new JLabel("輸入常數項"));
Box boxV2=Box.createVerticalBox();
boxV2.add(inputA);
boxV2.add(inputB);
boxV2.add(inputC);
Box baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(boxV2);
Container con=getContentPane();
con.setLayout(new FlowLayout());
showResult=new JTextArea(8,18);
con.add(connection);
con.add(baseBox);
con.add(computer);
con.add(new JScrollPane(showResult));
computer.addActionListener(this);
connection.addActionListener(this);
thread = new Thread(this);
setBounds(100,100,360,310);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
while(true){
try{ double root1=in.readDouble(); //堵塞狀態,除非讀取到信息
double root2=in.readDouble();
showResult.append("\n兩個根:\n"+root1+"\n"+root2);
showResult.setCaretPosition((showResult.getText()).length());
}
catch(IOException e){
showResult.setText("與服務器已斷開");
computer.setEnabled(false);
break;
}
}
}
public void actionPerformed(ActionEvent e){
if(e.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());
computer.setEnabled(true);
thread.start();
}
}
catch (IOException ee){
System.out.println(ee);
socket=new Socket();
}
}
if(e.getSource()==computer){
try{ double a=Double.parseDouble(inputA.getText()),
b=Double.parseDouble(inputB.getText()),
c=Double.parseDouble(inputC.getText());
double disk=b*b-4*a*c;
if(disk>=0){
out.writeDouble(a);
out.writeDouble(b);
out.writeDouble(c);
}
else{
inputA.setText("此2次方程無實根");
}
}
catch(Exception ee){
inputA.setText("請輸入數字字符");
}
}
}
public static void main(String args[]){
ClientFrame win=new ClientFrame();
}
}
② 服務器端程序(效果如圖11.7所示)
import java.io.*;
import java.net.*;
import java.util.*;
public class MutiServer{
public static void main(String args[]){
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true){
try{ server=new ServerSocket(4331);
}
catch(IOException e1){
System.out.println("正在監聽"); //ServerSocket對象不能重復創建
}
try{ you=server.accept();
System.out.println("客戶的地址:"+you.getInetAddress());
}
catch (IOException e){
System.out.println("正在等待客戶");
}
if(you!=null)
new ServerThread(you).start(); //為每個客戶啟動一個專門的線程
else
continue;
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
ServerThread(Socket t){
socket=t;
try{ in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
}
public void run(){
while(true){
double a=0,b=0,c=0,root1=0,root2=0;
try{ a=in.readDouble(); //堵塞狀態,除非讀取到信息
b=in.readDouble();
c=in.readDouble();
double disk=b*b-4*a*c;
root1=(-b+Math.sqrt(disk))/(2*a);
root2=(-b-Math.sqrt(disk))/(2*a);
out.writeDouble(root1);
out.writeDouble(root2);
}
catch (IOException e){
System.out.println("客戶離開");
break;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -