Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 不解决这个问题我睡不着!帮一下忙!!!
zcjgxjy





发贴: 1
积分: 0
于 2005-08-19 16:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
帮一下忙!!!请问怎么样让一个客户端的信息转发到另一个客户端?
运行多个客户端时,每个客户端只能接收自己发到服务器的信息,而其它客户端的信息没法接收到?这是怎么回事?怎么样才能解决,求求大家了~~我想了好久都想不出来了

我的源代码如下:服务器:
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.printlnEnvelope;
}

}
}

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.printlnEnvelope;
}

}
}
客户端:
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.printlnEnvelope;
}
}
}

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_actionPerformedEnvelope ;
}

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_windowClosingEnvelope;
}
}

class Clients_jButton2_actionAdapter implements ActionListener {
private Clients adaptee;
Clients_jButton2_actionAdapter(Clients adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformedEnvelope;
}
}

class Clients_jTextField1_actionAdapter implements ActionListener {
private Clients adaptee;
Clients_jTextField1_actionAdapter(Clients adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jTextField1_actionPerformedEnvelope;
}
}

class Clients_jButton1_actionAdapter implements ActionListener {
private Clients adaptee;
Clients_jButton1_actionAdapter(Clients adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformedEnvelope;
}
}




话题树型展开
人气 标题 作者 字数 发贴时间
3099 不解决这个问题我睡不着!帮一下忙!!! zcjgxjy 6236 2005-08-19 16:27
2580 Re:不解决这个问题我睡不着!帮一下忙!!! wonderfuture 62 2005-09-01 15:14

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923