两人聊天室
要求:1.主人可以根据输入的IP寻找到要聊天的对象.
2.主人和客人之间可以互相发送消息进行聊天.
有谁帮下我咯!!!
============写东西要自己写哦 不能找别人代替哦============
[此贴子已经被野蛮女人于2007-6-20 19:54:15编辑过]
我正在筹划,哪位写出代码给小弟参考以下.feimeng375@163.com
这个和我要的有点不同,要是能改成我的那种那就太感谢了!!!
package myprojects.chat;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class Chat extends Frame {
List lst = new List(8);
TextField tfIP = new TextField(15);
TextField tfData = new TextField(20);
DatagramSocket ds = null;
public Chat(){
try
{
ds = new DatagramSocket(8080);
}
catch(Exception e)
{
e.printStackTrace();
}
this.add(lst,"Center");
Panel p = new Panel();
this.add(p,"south");
p.setLayout(new BorderLayout());
p.add(tfIP,"West");
p.add(tfData,"East");
new Thread(new Runnable()
{
public void run()
{
byte [] buf = new byte[1024];
DataramPacket dp = new DatagramPacket(buf,1024);
while(true)
{
try
{
ds.receive(dp);
lst.add(new String(/*dp.getData()*/buf,0,dp.getLength()) +
" from " + dp.getAddress().getHostAddress() + ":" + dp.getPort(),0);
}
catch(Exception e)
{
if(!ds.isClosed())
{
e.printStackTrace();
}
}
} start();
tfData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
byte [] buf;
buf = tfData.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,
InetAddress.getByName(tfIP.getText()),8080);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
tfData.setText("");
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ds.close();
dispose();
System.exit(0);
}
});
}
public static void main (String args[]) {
System.out.println("Starting Chat...");
Chat mainFrame = new Chat();
mainFrame.setSize(300,400);
mainFrame.setTitle("Chat");
mainFrame.setVisible(true);
mainFrame.setResizable(flase);
}
}
[此贴子已经被作者于2007-6-20 13:52:13编辑过]