我当初写的那个为什么技术分只有1分,,,UDP聊天和TCP聊天的差距吗?
过去写过的UDP的东东,好久不看我都快自己看不懂了...= =复习基础才到多线程...
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.text.*;
- import java.util.*;
- class NewChattingFrameTest
- {
- public static void main(String[] args)throws Exception{
- DatagramSocket dsSend = new DatagramSocket();
- DatagramSocket dsReceive = new DatagramSocket(10000);
- new ChattingFrame(dsSend,dsReceive);
- }
- }
- class ChattingFrame
- {
- private Frame f;
- private TextArea taSend,taReceive;
- private Panel pSend,pReceive;
- private Button b;
- private DatagramSocket dsSend,dsReceive;
- //这个窗体是为了传送接收信息的,构造时需要发送和接收数据的包
- ChattingFrame(DatagramSocket dsSend,DatagramSocket dsReceive){
- this.dsSend = dsSend;
- this.dsReceive = dsReceive;
- init();
- Receive r = new Receive(dsReceive,taReceive);
- new Thread(r).start();
- }
- public void init(){
- f = new Frame("聊天程序");
-
- f.setBounds(400,200,500,420);
- f.setLayout(new BorderLayout(10,10));
- //建立上下主面板
- pSend = new Panel();
- pSend.setPreferredSize(new Dimension(300,100));
- pReceive = new Panel();
- pReceive.setPreferredSize(new Dimension(300,300));
-
- //布局下边的发送面板内的内容
- pSend.setLayout(new BorderLayout(10,10));
- taSend = new TextArea();
- taSend.setPreferredSize(new Dimension(250,60));
- b = new Button("发送");
- b.setPreferredSize(new Dimension(30,20));
- pSend.add(taSend,BorderLayout.CENTER);
- pSend.add(b,BorderLayout.EAST);
- //布局上边接收面板的内容
- pReceive.setLayout(new BorderLayout());
- taReceive = new TextArea();
- taReceive.setEditable(false);
- pReceive.add(taReceive);
- //把主面板添加进窗体内
- f.add(pSend,BorderLayout.SOUTH);
- f.add(pReceive,BorderLayout.CENTER);
-
- myEvent();
- f.setVisible(true);
- }
- public void myEvent(){
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- //添加按钮监听,点击按钮发送信息
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- new Thread(new Send(dsSend,taSend.getText())).start();
- //System.out.println("清空内容前的长度"+taSend.getText().length());
- taSend.setText("");
- //System.out.println("清空内容后的长度"+taSend.getText().length());
- }
- });
-
- //添加键盘监听,点击回车发送数据
- taSend.addKeyListener(new KeyAdapter(){
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode()==KeyEvent.VK_ENTER && !e.isControlDown()){
- e.consume();
- new Thread(new Send(dsSend,taSend.getText())).start();
- //System.out.println("清空内容前的长度"+taSend.getText().length());
- taSend.setText("");
- //System.out.println("清空内容后的长度"+taSend.getText().length());
- }
- //if(taSend.getText()=="\r\n")
- //taSend.setText("");
- }
- });
- }
- }
- class Send implements Runnable
- {
- private DatagramSocket ds;
- private String taSend;
-
- //构造时需要接收要发送的数据和发送数据的包
- Send(DatagramSocket ds,String taSend){
- this.ds = ds;
- this.taSend = taSend;
- }
- public void run(){
- try
- {
- if(taSend==null)
- return;
- byte[] buf = taSend.getBytes();
- //System.out.println("Send类的taSend的长度:"+taSend.length());
- //System.out.println("buf的长度:"+buf.length);
-
- if(buf.length!=0){
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("10.23.12.86"),10000);//发送给指定IP
- DatagramPacket owndp = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),10000);//发送给本机
- ds.send(dp);
- ds.send(owndp);
- }
- else
- return;
- }
- catch (UnknownHostException e)
- {
- throw new RuntimeException("未知主机");
- }
- catch(Exception e)
- {
- throw new RuntimeException("IO异常");
- }
- }
- }
- class Receive implements Runnable
- {
- private DatagramSocket ds;
- private TextArea taReceive;
-
- //构造时需要接收数据的包和输出接收数据的窗体组件TextArea
- Receive(DatagramSocket ds,TextArea taReceive){
- this.ds = ds;
- this.taReceive = taReceive;
- }
- public void run(){
- //为实现边输入边接收数据的功能,接收程序必须一直执行,不能停止,所以定义无限循环
- while(true){
- try
- {
- byte[] buf = new byte[1024];
-
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- int length = dp.getLength();
- //接收的数据以字符串形式存储
- String content = new String(dp.getData(),0,length);
- //获取现在的时间以字符串形式存储
- SimpleDateFormat sdf = new SimpleDateFormat("H:m:s");
- String date = sdf.format(new Date());
- //排列出要显示的信息样式保存在一个字符串当中
- String textReceive = dp.getAddress().getHostName()+" "+date+" \r\n"+content+"\r\n\r\n";
- //把定义好的字符串添加进taRecieve组件
- taReceive.append(textReceive);
- }
- catch (Exception e)
- {
- throw new RuntimeException("接收出现错误");
- }
- }
- }
- }
复制代码
|