zcjgxjy
发贴: 1
积分: 0
|
于 2005-08-19 16:27
帮一下忙!!!请问怎么样让一个客户端的信息转发到另一个客户端? 运行多个客户端时,每个客户端只能接收自己发到服务器的信息,而其它客户端的信息没法接收到?这是怎么回事?怎么样才能解决,求求大家了~~我想了好久都想不出来了
我的源代码如下:服务器: import java.io.*; import java.net.*;
public class Servers { public static void main(String[] args) throws Exception { Server s = new Server(); s.start();
} }
class Server extends Thread { //响应客户端的线程 // ServerSocket server; public Server() throws Exception { // server=new ServerSocket(8888); // Socket socket=server.accept();
}
public void run() { try { ServerSocket server = new ServerSocket(1030); while (true) { Socket socket = server.accept(); Service serv = new Service(socket); serv.start(); } } catch (Exception e) { System.out.println; }
} }
class Service extends Thread { //创建接收客户机信息的线程 Socket socket; public Service(Socket socket) { this.socket = socket;
}
public void run() { try { PrintStream outs; BufferedReader ins; ins = new BufferedReader(new InputStreamReader(socket. getInputStream())); outs = new PrintStream(socket.getOutputStream()); String ss = "成功连接上服务器!"; outs.println(ss); while (true) { String Str = ins.readLine(); if (Str != null) {
String str = "服务器已收到你的信息!"; } outs.println(Str); System.out.println("信息已转发!"); }
} catch (Exception e) { System.out.println; }
} } 客户端: import java.net.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter;
public class Clients extends JFrame { BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextField jTextField1 = new JTextField(); JTextArea jTextArea1 = new JTextArea(); Socket socket; PrintStream outs; BufferedReader ins; JScrollPane jScrollPane1 = new JScrollPane(); public Clients() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } }
private void jbInit() throws Exception { getContentPane().setLayout(borderLayout1); jButton1.addActionListener(new Clients_jButton1_actionAdapter(this)); jTextField1.addActionListener(new Clients_jTextField1_actionAdapter(this)); jButton2.addActionListener(new Clients_jButton2_actionAdapter(this)); this.addWindowListener(new Clients_this_windowAdapter(this)); this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH); jButton2.setText("退出"); jTextField1.setColumns(16); jPanel1.add(jTextField1); jPanel1.add(jButton1); jPanel1.add(jButton2); this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1); jButton1.setText("发送"); }
public static void main(String[] args) { Clients clients = new Clients(); clients.loadServer(); clients.setSize(350, 300); clients.setVisible(true); }
class Cth extends Thread { //用于接收服务器的信息 // Socket se; // public Cth(Socket w) { // this.se = w; // }
public void run() { try { for (; ; ) { String str = ins.readLine();
jTextArea1.append(str + "\n"); } } catch (Exception e) { System.out.println; } } }
public void loadServer() { try { socket = new Socket("127.0.0.1", 1030); outs = new PrintStream(socket.getOutputStream()); ins = new BufferedReader(new InputStreamReader(socket. getInputStream() )); Cth cth = new Cth(); cth.start(); } catch (Exception ee) { System.out.println(ee); }
}
public void jButton1_actionPerformed(ActionEvent e) { try {
String str = jTextField1.getText(); outs.println(str); //while(ins.readLine()!=null) jTextArea1.append(str + "\n"); jTextField1.setText(""); // outs.close(); outs.flush();
} catch (Exception se) { System.out.println(se); }
}
public void jTextField1_actionPerformed(ActionEvent e) { jButton1_actionPerformed ; }
public void jButton2_actionPerformed(ActionEvent e) { System.exit(0); }
public void this_windowClosing(WindowEvent e) { System.exit(0); } }
class Clients_this_windowAdapter extends WindowAdapter { private Clients adaptee; Clients_this_windowAdapter(Clients adaptee) { this.adaptee = adaptee; }
public void windowClosing(WindowEvent e) { adaptee.this_windowClosing; } }
class Clients_jButton2_actionAdapter implements ActionListener { private Clients adaptee; Clients_jButton2_actionAdapter(Clients adaptee) { this.adaptee = adaptee; }
public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed; } }
class Clients_jTextField1_actionAdapter implements ActionListener { private Clients adaptee; Clients_jTextField1_actionAdapter(Clients adaptee) { this.adaptee = adaptee; }
public void actionPerformed(ActionEvent e) { adaptee.jTextField1_actionPerformed; } }
class Clients_jButton1_actionAdapter implements ActionListener { private Clients adaptee; Clients_jButton1_actionAdapter(Clients adaptee) { this.adaptee = adaptee; }
public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed; } }
|