黑马程序员-用UDP编写网络聊天程序
2011-06-22 01:30
399 查看
/**
* AWT Sample application
*
* @author
* @version 1.00 11/06/21
*/
public class Chat {
public static void main(String[] args) {
// Create application frame.
ChatFrame frame = new ChatFrame();
// Show frame
frame.setVisible(true);
frame.setResizable(false);
}
}
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 11/06/21
*/
public class ChatFrame extends Frame {
/**
* The constructor.
*/
List lst=new List(6);
TextField tfIP=new TextField(15);
TextField tfData=new TextField(20);
DatagramSocket ds=null;
public ChatFrame() {
Panel p=new Panel();
add(p,"South");
add(lst,"Center");
try{
ds=new DatagramSocket(3000);
}catch(Exception e)
{
e.printStackTrace();
}
p.setLayout(new BorderLayout());
p.add(tfIP,"West");
p.add(tfData,"East");
new Thread(new Runnable(){
public void run(){
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
while(true){
try{
ds.receive(dp);
lst.add(new String(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()),3000);
ds.send(dp);
}catch(Exception er) {
er.printStackTrace();
}
tfData.setText("");
}
}
);
setTitle("Chat");
setSize(new Dimension(300, 300));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ChatFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
ds.close();
dispose();
System.exit(0);
}
}
* AWT Sample application
*
* @author
* @version 1.00 11/06/21
*/
public class Chat {
public static void main(String[] args) {
// Create application frame.
ChatFrame frame = new ChatFrame();
// Show frame
frame.setVisible(true);
frame.setResizable(false);
}
}
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 11/06/21
*/
public class ChatFrame extends Frame {
/**
* The constructor.
*/
List lst=new List(6);
TextField tfIP=new TextField(15);
TextField tfData=new TextField(20);
DatagramSocket ds=null;
public ChatFrame() {
Panel p=new Panel();
add(p,"South");
add(lst,"Center");
try{
ds=new DatagramSocket(3000);
}catch(Exception e)
{
e.printStackTrace();
}
p.setLayout(new BorderLayout());
p.add(tfIP,"West");
p.add(tfData,"East");
new Thread(new Runnable(){
public void run(){
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
while(true){
try{
ds.receive(dp);
lst.add(new String(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()),3000);
ds.send(dp);
}catch(Exception er) {
er.printStackTrace();
}
tfData.setText("");
}
}
);
setTitle("Chat");
setSize(new Dimension(300, 300));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ChatFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
ds.close();
dispose();
System.exit(0);
}
}
相关文章推荐
- 黑马程序员_Java基础_网络编程_UDP传输协议编程,控制台聊天程序
- 用UDP编写网络聊天程序
- java 网络编程【6】 用UDP编写网络聊天程序 以及 TCP编程实例
- java学习之利用UDP编写网络聊天程序
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- 黑马程序员——网络编程(一)--概述、UDP网络程序
- 利用C#编写的网络聊天程序
- Delphi-网络编程-UDP聊天程序(转)
- 用java编写简单UDP网络通信程序
- 使用UDP协议的JAVA网络聊天程序
- 使用UDPClient 编写聊天程序
- 黑马程序员 用UDP协议在dos命令行里模拟一个聊天程序
- 基于Udp的Socket网络编程聊天程序
- 使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”。
- 使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”
- udp网络聊天控制台程序
- 使用UDP协议编写一个网络程序
- 使用 acl 编写 UDP 网络程序
- 【Java网络编程】UDP协议实例——简单的在线聊天程序
- 黑马程序员-UDP网络程序