⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myclient.java

📁 socket实现客户端和服务器端的通信 计算一个点到原点的距离
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

/**
 * 这个类用于显示客户端的的界面
 * @author 刘之岗
 *
 */
public class MyClient extends Frame{
	
	Socket socket;
	DataInputStream dis;
	DataOutputStream dos;
	TextField tf1; 
	TextField tf2; 
	TextField tf3; 
	boolean flag = true;
	static final int SERVER_PORT = 6666; 
	double x = 0.0;
	double y = 0.0;
	
	
	/**
	 * 本方法用来显示客户端的主窗口
	 * 设置窗口的组件的布局
	 * 在Button上添加监听器
	 * 当事件发生时候调用相应的方法
	 *
	 */

	public void launchFrame(){
		
		this.setLocation(200, 300);
		this.setTitle("MyClient");
		this.setLayout(new GridLayout(4,2));
		
		Label lb1 = new Label("输入x坐标的值:");
		Label lb2 = new Label("输入y坐标的值:");
		Label lb3 = new Label("此点与原点的距离:");
		
		tf1 = new TextField(15);
		tf2 = new TextField(15);
		tf3 = new TextField(15);
		tf3.setEditable(false);
		
		Button b1 = new Button("重置");
		Button b2 = new Button("提交");
		
		Panel p1 = new Panel(new FlowLayout());
		Panel p2 = new Panel(new FlowLayout());
		Panel p3 = new Panel(new FlowLayout());
		Panel p4 = new Panel(new FlowLayout());
		Panel p5 = new Panel(new FlowLayout());
		Panel p6 = new Panel(new FlowLayout());
		Panel p7 = new Panel(new FlowLayout());
		Panel p8 = new Panel(new FlowLayout());
		
		p1.add(lb1);
		p2.add(tf1);
		p3.add(lb2);
		p4.add(tf2);
		p5.add(lb3);
		p6.add(tf3);
		p7.add(b1);
		p8.add(b2);
	
		
		this.add(p1);
		this.add(p2);
		this.add(p3);
		this.add(p4);
		this.add(p5);
		this.add(p6);
		this.add(p7);
		this.add(p8);
		
		
		this.addWindowListener(new WindowAdapter(){

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
		
		b1.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				clear();
				
			}
			
		});
		
		
		b2.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				getContent();
				if(flag == true){
					connect();
				}
				
				
			}
			
		});
		
		this.pack();
		this.setVisible(true);
		
		
	}
	
	/**
	 * 本方法用来把客户端填写的数据和服务器端写回的数据清空
	 *
	 */
	public void clear(){
		tf1.setText("");
		tf2.setText("");
		tf3.setText("");
	}
	
	/**
	 * 本方法用来把客户端写入的点的横纵坐标值
	 * 调用writeDouble方法发给服务器端
	 *
	 */
	public void send(){
			
		try {
				
			dos.writeDouble(x);
			dos.writeDouble(y);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 本方法用来获取输入的数据,
	 * 并验证数据的合法性
	 */
	public void getContent(){
		
		String str1 = tf1.getText();
		String str2 = tf2.getText();
		
		try {
			x = Double.parseDouble(str1);
			y = Double.parseDouble(str2);
			flag = true;
		} catch (NumberFormatException e) {
			flag = false;
			tf3.setText("输入的是非法的字符!");
		} 
	}
	
	/**
	 * 本方法用来接受服务器端返回的值,
	 * 并将结果显示在客户端窗中
	 * 
	 */
	public void receive(){
		try {
			double db = dis.readDouble();
			String str = String.valueOf(db);
			tf3.setText(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 本方法用来请求服务器端的连接
	 * 进行和服务器间的交互
	 *
	 */
	public void connect(){
		
		try {
			socket = new Socket("127.0.0.1",SERVER_PORT );
			
			dis = new DataInputStream(socket.getInputStream());
		    dos = new DataOutputStream(socket.getOutputStream());
			
			send();
			receive();
		  
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(dos != null){
				try {
					dos.flush();
					dos.close();
					dos = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			if(dis != null){
				try {
					dis.close();
					dis = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(socket != null){
				try {
					socket.close();
					socket = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		
			}
		}
	
	
		
		
	}
	
	public static void main(String[] args) {
		new MyClient().launchFrame();
		
	}	
		
	
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -