| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 576 人关注过本帖
标题:java中接收不到汉字最后一个字符以后的字符,
取消只看楼主 加入收藏
yantian001
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-3-29
收藏
 问题点数:0 回复次数:0 
java中接收不到汉字最后一个字符以后的字符,
我用java写的一个udp程序,为什么接收不到输入的汉字最后一个字符以后的字符,比如输入"你好aaaa"接收到的只有"你 "后面的没了 什么问题,知道的说下!
public class Chat extends Frame
{
    List lst = new List(6);
    TextField tfIP        =     new TextField(15);
    TextField tfInfo    =     new TextField(20);
    DatagramSocket  ds  = null;
    public Chat()
    {
        try
        {
            ds = new DatagramSocket(3000);
        }
        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(tfInfo,"East");

            new Thread (new Runnable()
                {
                    public void run()
                    {   
                        byte [] buff = new byte[2048];
                        DatagramPacket dp = new DatagramPacket(buff,2048);
                        while(true)
                            {
                                try
                                {
                                    ds.receive(dp);
                                    //System.out.println(dp.getData());
                                    String str = new String(dp.getData(),0,dp.getLength()) +"length="+dp.getLength()
                                        +" from "+dp.getAddress().getHostAddress() +":"+dp.getPort();
                                       
                                    //String str = new String (info.getBytes("iso-8859-1"),"GB2312");
                                    lst.add(str,0);
                                }
                                catch (Exception e)
                                {
                                    if(!ds.isClosed())
                                        {
                                        e.printStackTrace();
                                        }
                                }
                                
                            }
                    }
                }).start();

        tfInfo.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    byte [] buff = tfInfo.getText().getBytes();
                    try
                    {
                    DatagramPacket dp = new DatagramPacket(buff,tfInfo.getText().length(),InetAddress.getByName(tfIP.getText()),3000);
                    System.out.println(tfInfo.getText());
                    ds.send(dp);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                    tfInfo.setText("");   
                }
            });
                addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                        {
                            ds.close();
                            dispose();
                            System.exit(0);
                        }
                });
    }
搜索更多相关主题的帖子: 汉字 字符 java 
2009-09-27 10:44
快速回复:java中接收不到汉字最后一个字符以后的字符,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018559 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved