android编程3:socket编程之udp发送

系统 1842 0

本文博客链接: http://blog.csdn.net/jdh99 ,作者:jdh,转载请注明.

需要实现的功能:采用udp下的socket编程,当按下确认键,模拟器发送文本框数据,pc机上的网络调试助手接收


环境:win7 + eclipse + sdk

代码:

    package test.soket;

//import com.test_button.R;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class test_socket extends Activity {
	public static TextView show;
	public static Button press;
	public static boolean flag;
    
    private static final int MAX_DATA_PACKET_LENGTH = 40;
    private byte[] buffer = new byte[MAX_DATA_PACKET_LENGTH];
    private DatagramPacket dataPacket;
    private DatagramSocket udpSocket;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //开辟控件空间
        show = (TextView)findViewById(R.id.editText1);
        press = (Button)findViewById(R.id.button1);
        flag = false;
        //soket_send thread = new soket_send();
        //thread.init();
        //thread.start();
        
        try
        {
        	udpSocket = new DatagramSocket(5554);
        }
        catch (SocketException e)
        {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        }
        dataPacket = new DatagramPacket(buffer, MAX_DATA_PACKET_LENGTH);
        String str = "hello,jdh";  //这是要传输的数据
		byte out [] = str.getBytes();  //把传输内容分解成字节
        dataPacket.setData(out);
        dataPacket.setLength(out.length);
        dataPacket.setPort(5554);
        try
        {
                
        	InetAddress broadcastAddr = InetAddress.getByName("192.168.0.248");
        	dataPacket.setAddress(broadcastAddr);
        	udpSocket.send(dataPacket);
        }
        catch (IOException e)
        {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        }
        
	       
        press.setOnClickListener(new Button.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		flag = true;
        		/*
                String str = "hello,jdh";  //这是要传输的数据
        		byte out [] = str.getBytes();  //把传输内容分解成字节
                dataPacket.setData(out);
                dataPacket.setLength(out.length);
                */
                
                //获得输入框文本
        		CharSequence str =test_socket.show.getText();
                byte out[] = str.toString().getBytes();
                dataPacket.setData(out);
                dataPacket.setLength(out.length);
                try
                {
                        
                	InetAddress broadcastAddr = InetAddress.getByName("192.168.0.248");
                	dataPacket.setAddress(broadcastAddr);
                	udpSocket.send(dataPacket);
                }
                catch (IOException e)
                {
                	// TODO Auto-generated catch block
                	e.printStackTrace();
                }
        	}
        });
    }
}
  
界面:

android编程3:socket编程之udp发送

注意:在模拟器IP为本机IP,端口为模拟器名称

android编程3:socket编程之udp发送


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论