关于使用java做一个模拟聊天程序qq的疑问?
我是用java swing做的聊天界面,但是问题出在了服务器端的程序上,使用servlet写的java 服务器端小程序,怎么与聊天界面取得连接,这一点想了好久没想到怎么做,网上查资料,也没有讲的特别清楚地。 --------------------编程问答-------------------- 通过socket开发比较好,客户端服务端都是应用程序。 --------------------编程问答--------------------
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
@SuppressWarnings("serial")
public class UDP1message extends JFrame implements Runnable, ActionListener {
private JTextArea text;// 显示聊天内容
private JTextField ipText;// 显示姓名
private JTextField sendText;// 发送内容
private JButton button;// 发送按钮
@SuppressWarnings("unused")
private JScrollBar vsBar;
public UDP1message() {
setTitle("UDP1聊天程序");
setBounds(100, 100, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(new ImageIcon("icon1.jpg").getImage());// 设置图标
setResizable(false);
init();
}
public void init() {
setLayout(new BorderLayout());
text = new JTextArea();
text.setEditable(false);
text.setForeground(Color.BLUE);// 设置字体颜色
text.setFont(new Font("楷体", Font.BOLD, 16));// 设置字体和大小
text.setBackground(Color.LIGHT_GRAY);
JScrollPane textPanel = new JScrollPane(text);
vsBar = textPanel.getVerticalScrollBar();
add(textPanel);
JLabel lable = new JLabel(new ImageIcon("icon.jpg"));
add(lable,BorderLayout.EAST);
JPanel panel = new JPanel();
BorderLayout panelLayout = new BorderLayout();
panelLayout.setHgap(5);
panel.setLayout(panelLayout);
ipText = new JTextField("Person2");
ipText.setForeground(Color.RED);
ipText.setFont(new Font("黑体", Font.BOLD, 14));
panel.add(ipText, BorderLayout.WEST);
sendText = new JTextField();
panel.add(sendText, BorderLayout.CENTER);
button = new JButton("发送");
panel.add(button, BorderLayout.EAST);
add(panel, BorderLayout.SOUTH);
setVisible(true);
// server();
button.addActionListener(this);
Thread thread = new Thread(this);
thread.start();
}
// 接受信息
public void run() {
DatagramSocket mail_data = null;
DatagramPacket pack = null;
byte[] data = new byte[10240];
try {
pack = new DatagramPacket(data, data.length);
mail_data = new DatagramSocket(20130);
} catch (Exception e) {
e.toString();
}
while (true) {
if (mail_data != null) {
try {
mail_data.receive(pack);
String message = new String(pack.getData(), 0,
pack.getLength());
text.append(message + "\n");
} catch (Exception e) {
e.printStackTrace();
}
} else
break;
}
}
// 发送信息
public void actionPerformed(ActionEvent ev) {
InetAddress myip = null;
Date data = new Date();
SimpleDateFormat matter = new SimpleDateFormat("H:m:s");
String time = matter.format(data);
byte[] buffer = (ipText.getText() + ": " + time + "\n" +" "+ sendText
.getText()).trim().getBytes();
try {
myip = InetAddress.getLocalHost();
InetAddress address = InetAddress.getByName(myip.getHostAddress());
DatagramPacket data_pack = new DatagramPacket(buffer,
buffer.length, address, 20120);
DatagramSocket mail_data1 = new DatagramSocket();
mail_data1.send(data_pack);
text.append(ipText.getText() + ": " + time + "\n" + " "
+ sendText.getText() + "\n");
sendText.setText(null);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
UDP1message frame = new UDP1message();
}
}
我写的这个你参考参考!呵呵....... --------------------编程问答-------------------- 我也想知道呃。
补充:Java , Java SE