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

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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值