Coin163

首页 > 课本上一个很好的java多线程服务器程序

课本上一个很好的java多线程服务器程序

相关标签: java

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

服务端: import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Server {

public static void main(String args[]){

ServerSocket server = null;

Socket you = null;

try{

server = new ServerSocket(4331);

}catch (IOException el){

//serversocket对象不能重复创建

System.out.println("正在监听");

}

while (true){

try{

System.out.println("等待客户呼叫");

you = server.accept();

System.out.println("客户的地址:" + you.getInetAddress());

}catch (IOException e){

System.out.println("正在等待客户");

}

if (you != null){

//为每个客户创建一个专门的线程

new ServerThread(you).start();

}

}

}}class ServerThread extends Thread{

Socket socket;

DataOutputStream out = null;

DataInputStream in = null;

String s = null;

ServerThread (Socket t){

socket = t;

try{

out = new DataOutputStream(socket.getOutputStream());

in = new DataInputStream(socket.getInputStream());

}catch (IOException e){}

}

public void run(){

while (true){

try{

//阻塞读

double r = in.readDouble();

double area = Math.PI * r * r;

out.writeUTF("半径是:" + r + "的圆的面积:" + area);

}catch (IOException e){

System.out.println("客户离开");

return;

}

}

}} 客户端:

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class Client {

public static void main(String args[]){

new WindowClient();

}}class WindowClient extends JFrame implements Runnable, ActionListener{

JButton connection, send;

JTextField inputText;

JTextArea showResult;

Socket socket = null;

DataInputStream in = null;

DataOutputStream out = null;

Thread thread;

WindowClient(){

socket = new Socket();

connection = new JButton("连接服务器");

send = new JButton("发送");

send.setEnabled(false);

inputText = new JTextField(6);

showResult = new JTextArea();

add(connection, BorderLayout.NORTH);

JPanel pSouth = new JPanel();

pSouth.add(new JLabel("输入圆的半径:"));

pSouth.add(inputText);

pSouth.add(send);

add(new JScrollPane(showResult), BorderLayout.CENTER);

add(pSouth, BorderLayout.SOUTH);

connection.addActionListener(this);

send.addActionListener(this);

thread = new Thread(this);

setBounds(10, 30, 460, 400);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override

public void run() {

String s = null;

while (true){

try{

s = in.readUTF();

showResult.append("\n" + s);

}catch (IOException e){

showResult.setText("与服务器已断开" + e);

socket = new Socket();

break;

}

}

}

@Override

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());

send.setEnabled(true);

if (!(thread.isAlive()))

thread = new Thread(this);

thread.start();

}

}catch(IOException ee){

System.out.println(ee);

socket = new Socket();

}

}//if

if (e.getSource() == send){

String s = inputText.getText();

double r = Double.parseDouble(s);

try{

out.writeDouble(r);

}catch (IOException el){}

}

}}

原文

服务端: import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Server { public static void main(String ar

------分隔线----------------------------