import java.io.*;
import java.net.*;
import java.util.*;
public class SecurityChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new SecurityChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
//System.out.println(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
//e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
//System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
System.out.println(str);
//System.out.println(" a string send !");
}
}
} catch(SocketException e){
System.out.println("客户端退出了!");
}catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
//以上是服务器端
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class SecurityChatClient extends Frame {
private TextArea ta = new TextArea(10,50);
private TextField tf = new TextField();
private TextField tf2 = new TextField();
private Button b = new Button("输入ip");
private Panel p1 = new Panel();
Socket s = null;
InetAddress address = null;
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bConnected = false;
public static void main(String[] args){
new SecurityChatClient().launchFrame();
}
public void launchFrame(){
p1.setLayout(new BorderLayout());
p1.add(tf,BorderLayout.WEST);
p1.add(tf2,BorderLayout.CENTER);
p1.add(b,BorderLayout.EAST);
this.add(ta,BorderLayout.NORTH);
this.add(p1,BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addActionListener(new ButtonEvent1());
new Thread(new Reclient()).start();
tf.addActionListener(new TFListener());
}
public void connect(){
try {
s = new Socket(address,8888);
bConnected = true;
System.out.println("connected!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
class ButtonEvent1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str = tf2.getText();
try {
address = InetAddress.getByName(str);
System.out.println(address);
connect();
tf2.setText("");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
}
class Reclient implements Runnable{
@Override
public void run() {
while(bConnected == true){
try {
String str = dis.readUTF();
System.out.println(str);
ta.setText(ta.getText() + str + '\n');
} catch(SocketException e){
System.out.println("对方退出了!");
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
class TFListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
try {
String str = tf.getText().trim();
tf.setText("");
ta.setText(str);
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//以上是客户端
我起了服务器端,然后再在我的电脑起了两个客户端然后连个客户端都输入的是127.0.0.1这个ip地址但是服务器端能接收到信息但是不返回给另外一个客户端。求说下这个问题怎么解决,我找了好半天都没有找到啊。我现在发现的问题是客户端不能收到,而且在客户端发一个信息的话在服务器端回打两边但是就是不返回给另一个客户端。但是我不知道怎么解决这个问题。