这个程序哪里有错,编译可以通过,但不能发送接受信息,请大虾们看看。
//ChatServer.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class ChatServer extends JFrame
{
JTextArea ta = new JTextArea(12,28);
JTextField tf1 = new JTextField(10);
JTextField tf2 = new JTextField(29);
JButton b1 = new JButton("登录");
JButton b2 = new JButton("退出");
JButton b3 = new JButton("发送");
JLabel lab1 = new JLabel("用户名:");
JLabel lab2 = new JLabel("信息: ");
List list = new List(10);
JRadioButton rb1 = new JRadioButton("广播发送",true);
JRadioButton rb2 = new JRadioButton("选择发送");
ButtonGroup bg = new ButtonGroup();
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
Socket socket;
Socket socket1;
PrintStream ps;
PrintStream pd;
String name;
String messagestr;
String ip;
ServerSocket serversocket;
JFrame frame;
public ChatServer()
{
super("聊天室");
p1.add(lab1);
p1.add(tf1);
p1.add(b1);
p1.add(b2);
p2.add(lab2);
p2.add(tf2);
p2.add(b3);
p3.setLayout(new BorderLayout());
p3.add(list,BorderLayout.NORTH);
p3.add(rb1,BorderLayout.CENTER);
p3.add(rb2,BorderLayout.SOUTH);
bg.add(rb1);
bg.add(rb2);
sp.getViewport().add(ta);
p4.add(p3);
p4.add(sp);
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(p4,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
//设置聊天室界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(460,330);
setVisible(true);
setResizable(false);
b2.setEnabled(false);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
name = tf1.getText();
if(name.equals(""))
{
ta.append("用户名不能为空,请重新输入!\n");
}
else
{
try
{
b1.setEnabled(false);
b2.setEnabled(true);
serversocket = new ServerSocket(7000);
socket1 = serversocket.accept();
StringBuffer msg = new StringBuffer("CONNECT:");
ip = InetAddress.getLocalHost().toString();
msg.append(name + ":" + ip + "上线了!!!");
ps = new PrintStream(socket.getOutputStream());
ps.println(msg);
ps.flush();
//启动客户端线程,监听服务器端传来的信息
Thread t = new ClientListener(socket);
t.start();
}
catch (IOException ex)
{
ta.append("Error: " + ex + "\n");
}
}
}
});
frame=this;
}
class ClientListener extends Thread
{
Socket socket;
BufferedReader br;
PrintStream ps;
String message;
public ClientListener(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps = new PrintStream(socket.getOutputStream());
}
catch(IOException ex)
{
ta.append("Error: "+ex+"\n");
}
}
public void run()
{
while(true)
{
try
{
messagestr = br.readLine();
if(message == null)
break;
StringTokenizer st = new StringTokenizer(messagestr,":");
String keyword = st.nextToken();
if (keyword.equals("message"))
{
String broadcast = st.nextToken();
String user = st.nextToken();
StringBuffer sb = new StringBuffer(user+st.nextToken("\0"));
ta.append(sb+"\n");
}
if (keyword.equals("CONNECT"))
{
name = st.nextToken();
ip = st.nextToken();
StringBuffer sb = new StringBuffer(ip+st.nextToken("\0"));
ta.append(sb+"\n");
}
}
catch(IOException ex)
{
ta.append("Error-debug: "+ex+"\n");
}
}
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("发送"))
{
messagestr = tf2.getText();
if(!messagestr.equals(""))
{
StringBuffer msg = new StringBuffer("message:");
String info1 = name+":"+tf2.getText();
msg.append(name + ":" +messagestr);
// pd = new PrintStream(socket.getOutputStream());
pd.println(msg);
pd.flush();
tf2.setText("");
}
}
}
}
public static void main(String[] args)
{
ChatServer app = new ChatServer();
}
}
////////////////////Chatlient.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class ChatClient extends JFrame
{
JTextArea ta = new JTextArea(12,28);
JTextField tf1 = new JTextField(10);
JTextField tf2 = new JTextField(29);
JButton b1 = new JButton("登录");
JButton b2 = new JButton("退出");
JButton b3 = new JButton("发送");
JLabel lab1 = new JLabel("用户名:");
JLabel lab2 = new JLabel("信息: ");
List list = new List(10);
JRadioButton rb1 = new JRadioButton("广播发送",true);
JRadioButton rb2 = new JRadioButton("选择发送");
ButtonGroup bg = new ButtonGroup();
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
Socket socket;
PrintStream ps;
PrintStream pd;
String name;
String messagestr;
String ip;
JFrame frame;
public ChatClient()
{
super("聊天室");
p1.add(lab1);
p1.add(tf1);
p1.add(b1);
p1.add(b2);
p2.add(lab2);
p2.add(tf2);
p2.add(b3);
p3.setLayout(new BorderLayout());
p3.add(list,BorderLayout.NORTH);
p3.add(rb1,BorderLayout.CENTER);
p3.add(rb2,BorderLayout.SOUTH);
bg.add(rb1);
bg.add(rb2);
sp.getViewport().add(ta);
p4.add(p3);
p4.add(sp);
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(p4,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
//设置聊天室界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(460,330);
setVisible(true);
setResizable(false);
b2.setEnabled(false);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
name = tf1.getText();
if(name.equals(""))
{
ta.append("用户名不能为空,请重新输入!\n");
}
else
{
try
{
b1.setEnabled(false);
b2.setEnabled(true);
socket = new Socket(InetAddress.getLocalHost(), 7000);
if (socket != null)
{
StringBuffer msg = new StringBuffer("CONNECT:");
ip = InetAddress.getLocalHost().toString();
msg.append(name + ":" + ip + "上线了!!!");
ps = new PrintStream(socket.getOutputStream());
ps.println(msg);
ps.flush();
//启动客户端线程,监听服务器端传来的信息
Thread t = new ClientListener(socket);
t.start();
}
}
catch (IOException ex)
{
ta.append("Error: " + ex + "\n");
}
}
}
});
frame=this;
}
class ClientListener extends Thread
{
Socket socket;
BufferedReader br;
PrintStream ps;
String message;
public ClientListener(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps = new PrintStream(socket.getOutputStream());
}
catch(IOException ex)
{
ta.append("Error: "+ex+"\n");
}
}
public void run()
{
while(true)
{
try
{
messagestr = br.readLine();
if(message == null)
break;
StringTokenizer st = new StringTokenizer(messagestr,":");
String keyword = st.nextToken();
if (keyword.equals("message"))
{
String broadcast = st.nextToken();
String user = st.nextToken();
StringBuffer sb = new StringBuffer(user+st.nextToken("\0"));
ta.append(sb+"\n");
}
}
catch(IOException ex)
{
ta.append("Error-debug: "+ex+"\n");
}
}
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("发送"))
{
messagestr = tf2.getText();
if(!messagestr.equals(""))
{
StringBuffer msg = new StringBuffer("message:");
String info1 = name+":"+tf2.getText();
msg.append(name + ":" +messagestr);
// pd = new PrintStream(socket.getOutputStream());
pd.println(msg);
pd.flush();
tf2.setText("");
}
}
}
}
public static void main(String[] args)
{
ChatClient app = new ChatClient();
}
}