Java语言程序设计-进阶篇(十一)客户端/服务器程序实例

package lianxi;


import java.io.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.*;
import java.net.*;
import java.util.*;

public class Client extends Application{
	   public static void main(String[] args) {
	       launch(args);
	    }
	DataOutputStream tosever= null;
	DataInputStream fromsever = null;
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane paneForTextField = new BorderPane();
		paneForTextField.setPadding(new Insets(5,5,5,5));
		paneForTextField.setStyle("-fx-border-color:green");
		paneForTextField.setLeft(new Label("Enter a radius:"));
		
		TextField tf = new TextField();
		tf.setAlignment(Pos.BASELINE_RIGHT);
		paneForTextField.setCenter(tf);
		
		BorderPane mainPane = new BorderPane();
		TextArea ta = new TextArea();
		mainPane.setCenter(new ScrollPane(ta));
		mainPane.setTop(paneForTextField);
		
		Scene scene = new Scene(mainPane,450,200);
		primaryStage.setScene(scene);
		primaryStage.setTitle("Client");
		primaryStage.show();
		
		tf.setOnAction(e->{
			try{
			double radius = Double.parseDouble(tf.getText().trim());
			tosever.writeDouble(radius);
			tosever.flush();
			
			double area = fromsever.readDouble();
			ta.appendText("Radius is " + radius + '\n');
			ta.appendText("Area received from the sever is "
					+ area + '\n');
			}
			catch(IOException ex){
				System.err.println(ex);
			}
		});
		
		try{
			Socket socket = new Socket("localhost",8001);
			fromsever = new DataInputStream(socket.getInputStream());
			tosever = new DataOutputStream(socket.getOutputStream());
		}catch(IOException ex){
			ta.appendText(ex.toString() + '\n');
		}
	}

}
package lianxi;

import java.io.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.*;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.*;
import java.net.*;
import java.util.*;

public class Sever extends Application{
	   public static void main(String[] args) {
	       launch(args);
	    }

	@Override
	public void start(Stage primaryStage) {
		TextArea ta = new TextArea();
		
		Scene scene = new Scene(new ScrollPane(ta),450,450);
		primaryStage.setTitle("Server");
		primaryStage.setScene(scene);
		primaryStage.show();
		
	

	new Thread(()->{
		try{
			ServerSocket serverSocket = new ServerSocket(8001);
			Platform.runLater(()->
			ta.appendText("Sever atarted at" + new Date() +'\n'));
			
			Socket socket = serverSocket.accept();
			
			DataInputStream inputFromClient = new DataInputStream(
					socket.getInputStream());
			DataOutputStream outputToClient = new DataOutputStream(
					socket.getOutputStream());
			
			while(true){
				double radius = inputFromClient.readDouble();
				double area = radius * radius * Math.PI;
				outputToClient.writeDouble(area);
				
				Platform.runLater(()->{
					ta.appendText("Radius received from client:"
							+ radius +'\n');
					ta.appendText("Area is" + area + '\n');
				});
			}
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
	}).start();
	}
}
第一个是客户端,第二个是服务器。运行结果如下

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值