当前位置: 代码迷 >> Android >> 小程序,为什么安卓客户端发送数据后pc端没反应
  详细解决方案

小程序,为什么安卓客户端发送数据后pc端没反应

热度:55   发布时间:2016-05-01 13:21:22.0
小程序求助,为什么安卓客户端发送数据后pc端没反应
android客户端代码如下
Java code
public class MainActivity extends Activity {    //声明EditText控件fenShu和Button    private EditText fenshu;    private Button dafen;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //根据控件id来取得控件fenShu        fenshu = (EditText)findViewById(R.id.fenshu);        dafen = (Button)findViewById(R.id.dafen);        dafen.setOnClickListener(new DafenListener());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    class DafenListener implements OnClickListener{        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            String fs = fenshu.getText().toString();            fenshu.setText("");            try {                DatagramSocket socket = new DatagramSocket(8090);                InetAddress serverAddress = InetAddress.getByName("218.66.218.190");                byte data [] = fs.getBytes();                DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,8090);                socket.send(packet);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                    }                }        }


pc客户端的代码如下
Java code
//窗口模块public   class   Xianshiping{     public   static   void   main   (String   args[])         {     ImageFrame   frame   =   new   ImageFrame();             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setVisible(true); //socket模块     try   {     //创建一个DatagramSocket对象,并指定监听的端口号     DatagramSocket   socket   =   new   DatagramSocket(8090);     byte   data   []   =   new   byte[1024];     //创建一个空的DatagramPacket对象     DatagramPacket   packet   =   new   DatagramPacket(data,data.length);     //使用receive方法接收客户端所发送的数据     socket.receive(packet);     //取得packet包内的数据放入一个String对象fenshu中     String   fenshu   =   new   String(packet.getData(),packet.getOffset(),packet.getLength());     ImageFrame   IF=new   ImageFrame(fenshu);     IF.paint(null); }   catch   (Exception   e)   {     //   TODO   Auto-generated   catch   block     e.printStackTrace(); }     }     } class   ImageFrame   extends   JFrame{ //显示文本 String   str   =   null; ImageFrame(String   str) {     super();     this.str=str; } public   void   paint(Graphics   g)   {       super.paint(g);       g.setFont(new   Font( "SansSerif ",Font.PLAIN,500));       g.drawString(str,   30,   550);         } //设置背景图片    public ImageFrame()    {        setTitle("ImageTest");        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);        ImagePanel panel = new ImagePanel();        add(panel);    }    public static final int DEFAULT_WIDTH=500;        public static final int DEFAULT_HEIGHT=500;    }class ImagePanel extends JPanel{    public ImagePanel(){        try{            image = ImageIO.read(new File("img.jpg"));        }            catch (IOException e)    {            e.printStackTrace();        }        }        public void paintComponent(Graphics g)    {        super.paintComponent(g);        if(image==null) return;        int imageWidth=image.getWidth(this);    int imageHeight=image.getHeight(this);    g.drawImage(image,0,0,null);    for(int i=0;i*imageWidth<=getWidth();i++)    for(int j=0;j*imageHeight<=getHeight();j++)    if(i+j>0)        g.copyArea(0,0,imageWidth,imageHeight,i*imageWidth,j*imageHeight);    }        private Image image;    }


为什么我在android客户端中输入数字 PC客户端却没有反应,为什么?
  相关解决方案