您的位置:首页 > 其它

已知端口ip 基于UDP协议 通过局域网连接接收信息

2016-12-30 00:26 501 查看
package com.example.test1;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class TestUDP {    

    private byte[] buffer = new byte[1024];    

    

    private DatagramSocket ds = null;    

    

    /**  

     * 构造函数,创建UDP客户端  

     * @throws Exception  

     */    

    public TestUDP() throws Exception {    

        ds = new DatagramSocket();    

    }    

        

    /**  

     * 设置超时时间,该方法必须在bind方法之后使用.  

     * @param timeout 超时时间  

     * @throws Exception  

     */    

    public final void setSoTimeout(final int timeout) throws Exception {    

        ds.setSoTimeout(timeout);    

    }    

    

    /**  

     * 获得超时时间.  

     * @return 返回超时时间  

     * @throws Exception  

     */    

    public final int getSoTimeout() throws Exception {    

        return ds.getSoTimeout();    

    }    

    

    public final DatagramSocket getSocket() {    

        return ds;    

    }    

    

    /**  

     * 向指定的服务端发送数据信息.  

     * @param host 服务器主机地址  

     * @param port 服务端端口  

     * @param bytes 发送的数据信息  

     * @return 返回构造后俄数据报  

     * @throws IOException  

     */    

    public final DatagramPacket send(final String host, final int port,    

            final byte[] bytes) throws IOException {    

        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress    

                .getByName(host), port);    

        ds.send(dp);    

        return dp;    

    }    

    

    /**  

     * 接收从指定的服务端发回的数据.  

     * @param lhost 服务端主机  

     * @param lport 服务端端口  

     * @return 返回从指定的服务端发回的数据.  

     * @throws Exception  

     */    

    public final String receive(final String lhost, final int lport)    

            throws Exception {    

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);    

        ds.receive(dp);    

        String info = new String(dp.getData(), 0, dp.getLength());    

        return info;    

    }    

    

    /**  

     * 关闭udp连接.  

     */    

    public final void close() {    

        try {    

            ds.close();    

        } catch (Exception ex) {    

            ex.printStackTrace();    

        }    

    }    

    

   }    

}   

package com.example.test1;

import java.io.IOException;

import java.io.InterruptedIOException;

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.Handler;

import android.util.Log;

import android.widget.Toast;

public class UDPConectTest extends Thread {

private static final int TIMEOUT = 3000;

private static final int MAXTRIES = 5;

public Context mContext;

public Handler mHandler;

public UDPConectTest(Context context, Handler handler) {

// TODO Auto-generated constructor stub

mContext = context;

mHandler = handler;

}

@Override

public void run() {

Log.i("UDP", "开始");

// TODO Auto-generated method stub

super.run();

try {

TestUDP client;

client = new TestUDP();

        String serverHost = "192.168.201.254";    

        int serverPort = 5190;

        client.setSoTimeout(TIMEOUT);

        client.send(serverHost, serverPort, ("wi-drivec=192.168.201.100").getBytes());

        String info = client.receive(serverHost, serverPort);   

        Log.i("UDP", "服务端回应数据:" + info);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}     

}

}


请问为什么无法得到服务器返还得数据呢 我是新人 渴望得到大家的帮助 谢谢

这个是接口的example:

Client: 

wi-drivec=192.168.201.101

Server return: 

wi-drives=192.168.201.254 model='mlw2' version='2.0.0.0' ssid="MLWG2-0004"

Server: SD_CARD or USB is connected/disconnected UDP packet send to broadcast(192.168.201.255).

Example: Server send: wi-drives=192.168.201.254 SD_CARD1 = connected 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐