Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 udp 聊天程序 为什么发数据不能收到呀
xizhenyin





发贴: 4
积分: 0
于 2008-10-29 15:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.*;
import java.io.*;

public class UDPClient extends Frame {
  DatagramPacket dp = null;
  TextArea ta = null;
  Button b1 = null;
  String str = null;
  byte[] buff = new byte[1000];
  ByteArrayOutputStream baos = null;
  DataOutputStream dos = null;
  DatagramSocket ds = null;

  public void Laundframe() {
    setTitle("b");
    setLocation(200, 300);
    setSize(400, 400);
    setVisible(true);
    this.setLayout(new GridLayout(2, 1));
    ta = new TextArea("", 8, 30, TextArea.SCROLLBARS_NONE);

    b1 = new Button("发送给a");
    b1.addActionListener(new MonArea(this));
    this.add(ta);
    this.add(b1);

    this.setResizable(false);
    pack();
    this.addWindowListener(new Windowclosed());
  }

  // ////////////////窗口关闭////////////////////
  class Windowclosed extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      setVisible(false);
      System.exit(0);
    }
  }

  // ///////TextArea监听类//////////////////////////
  class MonArea implements ActionListener {
    UDPClient udp = null;

    MonArea(UDPClient udp) {
      this.udp = udp;
    }

    public void actionPerformed(ActionEvent e) {

      str = udp.ta.getText();

      udp.ta.setText("");
      baos = new ByteArrayOutputStream();
      dos = new DataOutputStream(baos);

      try {
        dos.writeUTF(str);

      } catch (IOException e1) {

        e1.printStackTrace();
      }
    }
  }

  public void sconnect() {

    try {
      dp = new DatagramPacket(buff, buff.length, new InetSocketAddress(
          "127.0.0.1", 3333));
    } catch (SocketException e1) {
      e1.printStackTrace();
    }

    try {
      ds = new DatagramSocket(4567);
      ds.send(dp);
      ds.close();
    } catch (SocketException e) {

    } catch (IOException e) {

      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    UDPClient ff = new UDPClient();
    ff.Laundframe();
    ff.sconnect();
  }

}

另一端
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.*;

public class UDPServer extends Frame {

  TextArea ta = null;
  Button b1 = null;
  String str1 = null;

  DatagramPacket dp = null;
  DatagramSocket ds = null;
  ByteArrayInputStream bais = null;
  DataInputStream dis = null;
  byte[] buff = new byte[1000];

  public void Laundframe() {
    setTitle("A");
    setLocation(200, 300);
    setSize(400, 400);
    setVisible(true);
    this.setLayout(new GridLayout(2, 1));
    ta = new TextArea("", 8, 30, TextArea.SCROLLBARS_NONE);

    b1 = new Button("发送给B");
    b1.addActionListener(new MonArea(this));
    this.add(ta);
    this.add(b1);

    this.setResizable(false);
    pack();
    this.addWindowListener(new Windowclosed());
  }

  // ////////////////窗口关闭////////////////////
  class Windowclosed extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      setVisible(false);
      System.exit(0);
    }
  }

  // ///////TextArea监听类//////////////////////////
  class MonArea implements ActionListener {
    UDPServer udp = null;

    MonArea(UDPServer udp) {
      this.udp = udp;
    }

    public void actionPerformed(ActionEvent e) {
      String str = null;
      str = udp.ta.getText();
      System.out.println(str);
      udp.ta.setText("");

    }
  }

  // //网络connect
  public void connect() {

    dp = new DatagramPacket(buff, buff.length);
    try {
      ds = new DatagramSocket(3333);
    } catch (SocketException e) {

      e.printStackTrace();
    }

    while (true) {
      try {
        ds.receive(dp);
        System.out.println("a connect ...");
        bais = new ByteArrayInputStream(buff);
        dis = new DataInputStream(bais);
        ta.setText(dis.readUTF());
        System.out.println(dis.readUTF());

      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      // ta.setText(str1);
      try {
        dis.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  }

  // /////////////////////////////////////////////
  // main

  public static void main(String[] args) {
    UDPServer us = new UDPServer();
    us.Laundframe();
    us.connect();

  }

}


JiafanZhou edited on 2008-11-10 19:11


话题树型展开
人气 标题 作者 字数 发贴时间
9639 udp 聊天程序 为什么发数据不能收到呀 xizhenyin 7890 2008-10-29 15:41
7925 Re:udp 聊天程序 为什么发数据不能收到呀 JiafanZhou 326 2008-11-10 23:10

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923