当前位置: 代码迷 >> Eclipse >> java 多客户端有关问题
  详细解决方案

java 多客户端有关问题

热度:48   发布时间:2016-04-23 13:36:00.0
java 多客户端问题
从书上看到一个服务器与多个客户端通信,启1个客户端没有问题,为什么启第2个客户端,服务器不提示是否连接上,也不能与第2个客户端通信,但是把第1个客户端断了,第2个就可以了,是不是服务器代码 处理多线程部分写的不多,小弟初学,希望各位帮忙!!!

//服务器端
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Reader extends JFrame{
private final int ServerPort = 2222;
private JTextArea jta = new JTextArea();

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Reader();

}

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

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

try{
ServerSocket serverSocket = new ServerSocket(ServerPort);
jta.append("Reader Server started at " + new Date() + '\n');
int clientNo = 1;

while(true){
Socket socket = serverSocket.accept();
System.out.println("***********");
jta.append("Starting thread for client" + clientNo + " at " + new Date() + '\n');

InetAddress inetAddress = socket.getInetAddress();
jta.append("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + '\n');
jta.append("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + '\n');

HandleAClient task = new HandleAClient(socket);
task.run();
}
}
catch(IOException ex){
System.err.println(ex);
}
}

class HandleAClient implements Runnable{
private Socket socket;
public HandleAClient(Socket socket){
this.socket = socket;
}

public void run(){
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);
outputToClient.flush();

jta.append("radius received from client: " + radius + '\n');
jta.append("Area found: " + area + '\n');

outputToClient.writeInt(4);
}
}
catch(IOException e){
System.err.println(e);
}
}
}

}

//客户端
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Label extends JFrame{
private final int ServerPort = 2222;
private JTextField jtf = new JTextField();
  private JTextArea jta = new JTextArea();
   
  private DataOutputStream toServer;
  private DataInputStream fromServer;
   
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Label();
}

public Label(){
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter radius"), BorderLayout.WEST);
p.add(jtf, BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);

setLayout(new BorderLayout());
add(p,BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);

jtf.addActionListener(new ButtonListener());

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

try{
Socket socket = new Socket("localhost", ServerPort);
fromServer = new DataInputStream(socket.getInputStream());
  相关解决方案