客户端/服务器代码实例

客户端发送圆的半径给服务器,服务器生成面积,再发送给客户端

先启动Server,再启动Client

1.Server.java

import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;

import javax.swing.*;

public class Server extends JFrame{

	private JTextArea jta=new JTextArea();
	
	
	public Server(){
		setLayout(new BorderLayout());
		add(new JScrollPane(jta),BorderLayout.CENTER);
		
		setTitle("Server");
		setSize(500,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		try {
			ServerSocket serverSocket=new ServerSocket(8000);
			jta.append("Server started 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);
				jta.append("Radius received from client:"+radius+'\n');
				jta.append("Area found:"+area+'\n');
				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Server();
	}

}
2.Client.java

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class Client extends JFrame {
	private JTextField jtf = new JTextField();
	private JTextArea jta = new JTextArea();

	private DataOutputStream toServer;
	private DataInputStream fromServer;

	public Client(){
		JPanel panel=new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(new JLabel("Enter radius:"),BorderLayout.WEST);
		panel.add(jtf,BorderLayout.CENTER);
		jtf.setHorizontalAlignment(JTextField.RIGHT);
		
		setLayout(new BorderLayout());
		add(panel,BorderLayout.NORTH);
		add(new JScrollPane(jta),BorderLayout.CENTER);
		
		jtf.addActionListener(new TextFieldListener());
		
		setTitle("Client");
		setSize(500,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		try{Socket socket=new Socket("localhost",8000);
		System.out.println(socket.getLocalPort());
		fromServer=new DataInputStream(socket.getInputStream());
		toServer=new DataOutputStream(socket.getOutputStream());
		
		}
		catch(IOException e){
			e.printStackTrace();
		}
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Client();
	}

	private class TextFieldListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try{
				double radius=Double.parseDouble(jtf.getText().trim());
					
				toServer.writeDouble(radius);
				toServer.flush();
				
				double area=fromServer.readDouble();
				jta.append("Radius is "+radius+"\n");
				jta.append("Area received from the server is "+area+"\n");
				
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
		}
		
	}
}


3.修改server,应用多线程以服务多个客户

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class MultiThreadServer extends JFrame {

	private JTextArea jta = new JTextArea();

	public MultiThreadServer() {
		setLayout(new BorderLayout());
		add(new JScrollPane(jta), BorderLayout.CENTER);

		setTitle("MultiThreadServer");
		setSize(500, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

		try {
			ServerSocket serverSocket = new ServerSocket(8000);

			jta.append("MultiThreadServer started at" + new Date() + "\n");

			int clientNo = 1;

			while (true) {
				Socket socket = serverSocket.accept();
				
				jta.append("Starting thread for client " + new Date() + "\n");
				
				InetAddress inetAddress = socket.getInetAddress();
				
				jta.append("Client " + clientNo + "'s host name is"
						+ inetAddress.getHostName() + "\n");
				jta.append("Client " + clientNo + "'s host address is "
						+ inetAddress.getHostAddress() + '\n');
				
				HandleAClient task=new HandleAClient(socket);
				
				new Thread(task).start();
				clientNo++;
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MultiThreadServer();
	}

	class HandleAClient implements Runnable {
		private Socket socket;

		public HandleAClient(Socket socket) {
			this.socket = socket;

		}
		
		

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try{
				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);
					
					jta.append("radius received from  client: "+radius+'\n');
					jta.append("Area found: "+area +'\n');
					
				}
			}
			catch(IOException e){
				e.printStackTrace();
			}
		}

	}
}

笔记:关键代码

while(true){

Socket socket=serverSocket.accept();

Thread thread =new ThreadClass(socket);

thread.start();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值