求java高手前來幫忙..急!!! 有關多線程

ChanKelvin 发布于 2012/12/09 15:40
阅读 224
收藏 0
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
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.ServerSocket;
import java.net.Socket;
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 ThreadClient extends JFrame{

private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;

public static void main(String[] args) {
new ThreadClient();
}

public ThreadClient() {
JPanel p = new JPanel();
JLabel JL[] = new JLabel[6];
p.setLayout(new GridLayout(1,6));
jtf.setHorizontalAlignment(JTextField.RIGHT);
p.add(new Label("Time"));
p.add(new Label("Flight"));
p.add(new Label("where"));
p.add(new Label("status"));
p.add(new Label("status time"));
setLayout(new BorderLayout());
add(p, 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",8001);
DataInputStream inputfromClient;
DataOutputStream outputToClient;

inputfromClient = new DataInputStream(socket.getInputStream());
outputToClient = new DataOutputStream(socket.getOutputStream());
//socket.close();
while (true){
String b = inputfromClient.readUTF();
jta.append("B:"+ b + "\n");
}

}
catch (IOException e){
jta.append(e.toString() + '\n');

}


}

}

-----------------------------------------------------------------------------------------------以上是客戶端..

import java.awt.BorderLayout;
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.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MultithreadServer extends JFrame {
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;
HandleAClient task;
HandleAClient task1 ;
public static void main(String[] args) {
new MultithreadServer();

}

public MultithreadServer() { //constructor
   
    ServerSocket serversocket ;
    setLayout(new BorderLayout());
    add(new JScrollPane(jta), BorderLayout.CENTER);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(jtf, BorderLayout.CENTER);
    jtf.setHorizontalAlignment(JTextField.RIGHT);
 
    add(p, BorderLayout.SOUTH);
    jtf.addActionListener(new TextFieldListener());
    setTitle("Server");
    setSize(500,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    try {
    jta.append("MultiThread Started at "+ new Date() + '\n');
    int Clientno = 1;
    serversocket = new ServerSocket(8001);
    int i=1;
    while(true){
    Socket socket = serversocket.accept();//SoServerSocket serversocket = new ServerSocket(8001);cket socket = new Socket("localhost", 8001);
    fromServer = new DataInputStream(socket.getInputStream());
    toServer = new DataOutputStream(socket.getOutputStream());
    task = new HandleAClient(socket);
    new Thread (task).start();
 
    jta.append("Client no. is"+i);
    i++;
//    Thread.sleep(1000);   
 }
}   
catch (Exception e) {
jta.append(e.toString() + '\n');
}
}
 private class TextFieldListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
    try {
    String a = (jtf.getText().trim());
    toServer.writeUTF(a);
    toServer.flush();
    jta.append("A: "+ a +"\n");
    }
    catch(IOException ex) {
    System.err.println(ex);
    }
    }
    }
class HandleAClient implements Runnable {
private Socket socket1;
public HandleAClient(Socket socket){
socket1 = socket;
}
public void run() {
DataInputStream inputfromClient;
DataOutputStream outputToClient;
jtf.addActionListener(new TextFieldListener());
}
}

}

------------------------------------------------------------以上是服務器


想編寫一個多線程的程序, 服務器輸入, 然後兩個或更多的客戶端同時出現所輸入的資料

問題:
現在的情況是 第一個連入後 第一個可以輸出, 第二個連入後,第二個可以輸出.. 但第一個就沒有再輸出.. 十分糾結

加载中
返回顶部
顶部