android java udp socket类封装

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

import android.content.Context;
import android.os.Message;
import android.util.Log;

public class WMGGUDPSocket {
    DatagramSocket mDatagramSocket = null;
    InetAddress    mDestAddress    = null;
    protected Thread mRecieveDataThread = null;
    
    byte[]         mCacheBuffer;
    String         mRecieveData;
    String         mSendData;
    int            iDstIP;
    
    public WMGGUDPSocket(Context pContext)
    {
        try {
            mDatagramSocket = new DatagramSocket(Constants.UDP_PORT);
        }catch (SocketException e){
            e.printStackTrace();
        }
    }
    
    public byte[] IPTobytes(int iIP)
    {
        byte[] IP = new byte[4];
        IP[0] = (byte)(iIP & 0xFF);  
        IP[1] = (byte)((iIP >> 8) & 0xFF);  
        IP[2] = (byte)((iIP >> 16) & 0xFF);  
        IP[3] = (byte)((iIP >> 24) & 0xFF);

        return IP;
    }
    
    public int onSendData(String mData, int iDestIP)
    {
        int iRetValue = 0;
        mSendData = mData;
        iDstIP = iDestIP;
        new Thread(new SendDataThread()).start();
        return iRetValue;
    }
    
    public int SendData(String mData, int iDestIP)
    {
        int iRetValue = 0;
        DatagramPacket mDatagramPacket = null;
        mCacheBuffer =  mData.getBytes();
        try {
            mDestAddress = InetAddress.getByAddress(IPTobytes(iDestIP));
        }catch (UnknownHostException e){
            e.printStackTrace();
        }
        
        mDatagramPacket = new DatagramPacket(mCacheBuffer, mCacheBuffer.length, mDestAddress, Constants.UDP_PORT);
        
        try {
            mDatagramSocket.send(mDatagramPacket);
        }catch (IOException e){
            e.printStackTrace();
        }
        
        return iRetValue;
    }
    
    class SendDataThread implements Runnable {

        public void run() {
        
            SendData(mSendData, iDstIP);
        }

    }
    
    
    class RecieveDataThread implements Runnable {
        byte[] mCacheBuff;
        DatagramPacket mRecieveDP = null;
        

        public RecieveDataThread() {
            
            mCacheBuff = new byte[Constants.RECIEVE_BUFF_LEN];
            mRecieveDP = new DatagramPacket(mCacheBuff,0, mCacheBuff.length);
        }

        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    mDatagramSocket.receive(mRecieveDP);
                    mRecieveData = new String(mRecieveDP.getData(), 0, mRecieveDP.getLength());
                    
                    Log.d("WMGGUDPSocket", mRecieveData);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

    }
    
    public void StopRecieveDataThread()
    {
        try{
            mDatagramSocket.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        
        try {
            mRecieveDataThread.interrupt();
        }catch (Exception e){
            e.printStackTrace();
        }
        
    }
    
    
    public void RecieveData()
    {
        mRecieveDataThread = new Thread(new RecieveDataThread());
        mRecieveDataThread.start();
    }
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值