| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 423 人关注过本帖
标题:这个聊天程序哪里有错
只看楼主 加入收藏
海角天涯
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:1 
这个聊天程序哪里有错

这个程序哪里有错,编译可以通过,但不能发送接受信息,请大虾们看看。
//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();
}
}

搜索更多相关主题的帖子: 聊天 
2006-06-22 15:46
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你不还是打个包吧,太长了,C&V是很难过的

可惜不是你,陪我到最后
2006-06-22 16:03
快速回复:这个聊天程序哪里有错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026348 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved