2013-07-12 57 views
1

我正在尝试制作一个多客户端聊天应用程序,其中聊天是在客户端窗口中实现的。我已经尝试了相同的服务器和客户端代码。我有两个问题: 答:我相信代码应该可以工作,但是,服务器到客户端的连接很好,但信息不会在客户端之间传输。 B.我需要一种方法来实现私人一对一的聊天,如果有两个以上的客户端,我已经使用一个类来存储为每个正在建立的连接返回的Socket对象的信息,但是我不能弄清楚如何实现它。与多客户端聊天应用程序卡住

服务器代码是:

import java.io.*; 
import java.net.*; 

class ClientInfo { 

Socket socket; 
String name; 

public ClientInfo(Socket socket, String name) { 
    this.socket = socket; 
    this.name = name; 
} 
} 

public class server { 

private ObjectInputStream input[] = null; 
private ObjectOutputStream output[] = null; 
private String value = null; 
private static ServerSocket server; 
private Socket connection = null; 

private static int i = -1; 

public static void main(String args[]) { 
    try { 
     server = new ServerSocket(1500, 100); 
     while (true) { 
      System.out.println("Waiting for connection from client"); 
      Socket connection = server.accept(); 
      i++; 
      System.out.println("Connection received from " + (i + 1) + " source(s)"); 
      //System.out.println(i); 

      new ClientInfo(connection, "Client no:" + (i + 1)); 
      innerChat inc = new server().new innerChat(connection); 

     } 
    } catch (Exception e) { 
     System.out.println("Error in public static void main! >>>" + e); 
    } 
}// end of main!!! 

class innerChat implements Runnable { 

    private Socket connection = null; 

    public innerChat(Socket connection) { 
     this.connection = connection; 
     Thread t; 
     t = new Thread(this); 
     t.start(); 
    } 

    public void run() { 
     try { 
      output[i] = new ObjectOutputStream(connection.getOutputStream()); 
      output[i].flush(); 
      input[i] = new ObjectInputStream(connection.getInputStream()); 
     } catch (Exception e) { 
     } 
    } 
} 
} 

而且客户端代码

import java.net.*; 
import java.io.*; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.*; 
import java.awt.event.*; 

public class ChatappClient { 

private static int port = 1500; 
JFrame window = new JFrame("Chat"); 
JButton sendBox = new JButton("Send"); 
JTextField inputMsg = new JTextField(35); 
JTextArea outputMsg = new JTextArea(10, 35); 
private ObjectInputStream input; 
private ObjectOutputStream output; 

public static void main(String[] args) throws Exception { 
    ChatappClient c = new ChatappClient(); 
    c.window.setVisible(true); 
    c.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    c.run(); 
} 

    public ChatappClient() { 

    inputMsg.setSize(40, 20); 
    sendBox.setSize(5, 10); 
    outputMsg.setSize(35, 50); 
    inputMsg.setEditable(true); 
    outputMsg.setEditable(false); 
    window.getContentPane().add(inputMsg, "South"); 
    window.getContentPane().add(outputMsg, "East"); 
    window.getContentPane().add(sendBox, "West"); 
    window.pack(); 
    sendBox.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       output.writeObject(inputMsg.getText()); 
       outputMsg.append("\n" + "Client>>>" + inputMsg.getText()); 
       output.flush(); 
      } catch (IOException ie) { 
       outputMsg.append("Error encountered! " + ie); 
      } 
      inputMsg.setText(""); 
     } 
    }); 
    inputMsg.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       output.writeObject(inputMsg.getText()); 
       outputMsg.append("\n" + "Client>>>" + inputMsg.getText()); 
       output.flush(); 
      } catch (IOException ie) { 
       outputMsg.append("Error encountered! " + ie); 
      } 
      inputMsg.setText(""); 
     } 
    }); 
} 

private void run() throws IOException { 
    Socket clientSocket = new Socket("127.0.0.1", port); 
    output = new ObjectOutputStream(clientSocket.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(clientSocket.getInputStream()); 
    outputMsg.append("I/O Success"); 
    String value = null; 
    while (true) { 
     try { 
      value = (String) input.readObject(); 
     } catch (Exception e) { 
     } 
     outputMsg.append(value + "\n"); 
    } 
} 
} 
+0

请看看这个[示例](http://www.dailyfreecode.com/Code/socket-multi-client-chat-server-1252.aspx) –

回答

1

您的代码看起来像它可以改善不少。例如,你的主要方法应该没有任何代码。它应该启动你的服务器类,就是这样了(并且注意类名应该按照Java标准以大写字母开头,我强烈建议你遵循)。

我正在尝试制作一个多客户端聊天应用程序,其中服务器除了监听并创建连接之外什么都不做。

服务器将不得不做更多的事情。它需要创建客户端,它需要维护一个集合,例如客户端的ArrayList,例如ArrayList<ChatappClient>。否则,您如何期望服务器将两个客户端连接在一起?

我认为在开始编写代码之前,您需要更深入地思考此程序的结构和连接。

+0

如果你可以请解释一下需要的修改更多。谢谢。 – Ozil