62,621
社区成员
发帖
与我相关
我的任务
分享import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.util.concurrent.*;
import java.awt.*;
public class MutilThreadServer extends JFrame{
private JTextArea jta=new JTextArea();
public MutilThreadServer(){
add(new JScrollPane(jta));
/*setTitle("MutilThreadServer");
setSize(500,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);*/
ExecutorService executor=Executors.newCachedThreadPool();
try{
ServerSocket serverSocket=new ServerSocket(8000);
jta.append("MutilThreadServer start at "+new Date()+'\n');
int clientNo=1;
while(true){
Socket socket=serverSocket.accept();
jta.append("Starting thread for client "+clientNo+"at "+new Date()+'\n');
InetAddress address=socket.getInetAddress();
jta.append("Client "+clientNo+"'s host name is "+address.getHostName()+'\n');
jta.append("Client "+clientNo+"'s IP Address is"+address.getHostAddress()+'\n');
executor.execute(new handleClient(socket));
clientNo++;
}
}
catch(IOException ex){
System.err.println(ex);
}
finally{
executor.shutdown();
}
}
public static void main(String[] args){
MutilThreadServer frame=new MutilThreadServer();
frame.setTitle("MutilThreadServer");
frame.setSize(500,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class handleClient implements Runnable{
private Socket socket;
DataInputStream in;
DataOutputStream out;
public handleClient(Socket socket){
this.socket=socket;
}
public void run(){
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
double radius=in.readDouble();
double area=radius*radius*Math.PI;
out.writeDouble(area);
jta.append("Radius recevie from client: "+radius+'\n');
jta.append("Area found: "+area+'\n');
}
catch(IOException ex){
System.out.println(ex);
}
}
}
}搞不清楚你要怎么样哇
这样也可以用啊
package test11;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.util.concurrent.*;
import java.awt.*;
public class MutilThreadServer extends JFrame{
private JTextArea jta=new JTextArea();
public MutilThreadServer(){
add(new JScrollPane(jta));
setTitle("MutilThreadServer");
setSize(500,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ExecutorService executor=Executors.newCachedThreadPool();
try{
ServerSocket serverSocket=new ServerSocket(8000);
jta.append("MutilThreadServer start at "+new Date()+'\n');
int clientNo=1;
while(true){
Socket socket=serverSocket.accept();
jta.append("Starting thread for client "+clientNo+"at "+new Date()+'\n');
InetAddress address=socket.getInetAddress();
jta.append("Client "+clientNo+"'s host name is "+address.getHostName()+'\n');
jta.append("Client "+clientNo+"'s IP Address is"+address.getHostAddress()+'\n');
executor.execute(new handleClient(socket));
clientNo++;
}
}
catch(IOException ex){
System.err.println(ex);
}
finally{
executor.shutdown();
}
}
public static void main(String[] args){
MutilThreadServer frame=new MutilThreadServer();
// frame.setTitle("MutilThreadServer");
// frame.setSize(500,300);
// frame.setLocationRelativeTo(null);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setVisible(true);
}
private class handleClient implements Runnable{
private Socket socket;
DataInputStream in;
DataOutputStream out;
public handleClient(Socket socket){
this.socket=socket;
}
public void run(){
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
double radius=in.readDouble();
double area=radius*radius*Math.PI;
out.writeDouble(area);
jta.append("Radius recevie from client: "+radius+'\n');
jta.append("Area found: "+area+'\n');
}
catch(IOException ex){
System.out.println(ex);
}
}
}
}