首先我的想法是写一个聊天室的程序,一个服务器可以连接多个客户端,不同客户端可以自由聊天,并且在客户端有的信息显示框中可以显示每个客户所发的信息(通过服务器发到每个客户端上)!
我现在的问题是:服务器可以连接多个客户端,但是每个客户端的信息显示框中只能显示自己所发的信息,不能显示别人发的信息!下面是我写的代码,请问大虾们错在哪里!给与指正,不胜感激!
package com.chat;
import java.net.*;
import java.io.*;
/**
- @see 服务器端代码
-
@author 章黎
*@see 创建一个服务器端的端口,并接受客户端得端口,用一个线程来监听各个客户端
*/
public class Server {
private static int port=6788;
private static ServerSocket server;
private static Socket socket;
public Server()
{
try {
System.out.println("服务器已启动");
server=new ServerSocket(port);
while(true)
{
socket= server.accept();
System.out.println("连接上一个服务器");
MyThread myThread=new MyThread(socket);
myThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyThread extends Thread
{
Socket socket;
BufferedReader reader;
PrintStream ps;
String msg;
public MyThread(Socket s)
{
this.socket=s;
}
public void run()
{
try {
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()) );
PrintStream out=new PrintStream(socket.getOutputStream(),true);
while(true)
{
msg=reader.readLine();
if(msg==null)
{
return;
}
else
if(msg.equals("exit"))
break;
else
out.println(msg);
out.flush();
System.out.println(msg);
}server.close(); } catch (IOException e) { e.printStackTrace(); } }
}
public static void main(String []args)
{
new Server();
}
}
package com.chat;
import javax.swing.*;
//import com.chat.Server.MyThread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
/**
- @see客户端代码
-
@author 章黎
*@see客户端要创建一个套接字与服务器连上,不过在这之前得做好用户界面,
*客户端要做的事就是将信息发给服务器、同时接受服务器端传过来的信息
*/
public class Client extends JFrame implements ActionListener{
PrintStream ps;
InputStream in;
private int port=6788;
Socket socket;
JLabel label1,label2;
JTextField nameField;
JTextArea msgArea;
JTextArea showmsgArea;
JButton sendButton;
JPanel panel,panel1,panel2;
StringBuffer msg;
String s1;
Date datetime;
public Client()
{
super("Chating room");
panel=new JPanel();
panel1=new JPanel();
panel2=new JPanel();
showmsgArea=new JTextArea();
showmsgArea.setEditable(false);
sendButton=new JButton("发送");
sendButton.addActionListener(this);
label1=new JLabel("用户名:");
nameField=new JTextField(10);
label2=new JLabel("聊天信息:");
msgArea=new JTextArea(3,20);
JScrollPane p=new JScrollPane(msgArea);
panel1.setLayout(new GridLayout(2,1,3,3));
panel1.add(label1);
panel1.add(nameField);
panel2.add(label2);
panel2.add(p);
panel2.add(sendButton);
panel.add(panel1);
panel.add(panel2);
this.setLayout(new BorderLayout());
this.add(panel,BorderLayout.NORTH);
this.add(showmsgArea,BorderLayout.CENTER);
this.setSize(500,300);
this.setLocation(300, 280);
this.setVisible(true);}
public void actionPerformed(ActionEvent e) {
try
{
if(e.getSource()==sendButton)
{
if(socket!=null)
{
datetime=new Date();
s1=nameField.getText();
msg=new StringBuffer(s1+":");
msg.append(msgArea.getText());
msgArea.setText("");
ps.println(datetime);
ps.println(msg);
ps.flush();
}
if(socket==null)
{socket=new Socket("localhost",port); ps=new PrintStream(socket.getOutputStream(),true); s1=nameField.getText(); msg=new StringBuffer(s1+":"); msg.append(msgArea.getText()); ps.println(datetime); ps.println(msg); ps.flush(); } MyThread myThread=new MyThread(this,socket); myThread.start(); } }catch(Exception e1) { e1.getMessage(); }
}
class MyThread extends Thread
{
BufferedReader reader;
Socket socket;
Client client;
public MyThread(Client c,Socket s)
{
this.client=c;
this.socket=s;
}
public void run()
{try { in = socket.getInputStream(); reader=new BufferedReader(new InputStreamReader(in)); } catch (IOException e) { e.printStackTrace(); } while (socket!=null) { try { String msgin=reader.readLine(); if(msgin.equals("")) return; else showmsgArea.append(msgin); showmsgArea.append("\n"); } catch (IOException e) { e.printStackTrace(); } } }
}
public static void main(String[] args) {
new Client();
}
}