android UDP连接,没有接收任何数据

时间:2012-04-16 08:13:13

标签: android sockets connection udp packet

我是这里的新手,所以如果我提出愚蠢的问题,请原谅我。

我试图在Eclipse的PC仿真器和DSP之间建立UDP连接。一个Android手机
(或两个Android手机设备之间)。

我有路由器,手机通过路由器的wifi网络连接到互联网。 PC也在同一网络上(直接有线路由器 - PC连接)。我试图从服务器线程发送一些文本数据到客户端线程,但没有收到/发送任何东西。 :(

服务器 java类(重新编辑,服务器从客户端接收消息):

public class server implements Runnable 
{
    // the Server's Port
    public static final int SERVERPORT  = 6000;

    // running Server thread.
    public void run() 
    {
        Log.d("redwing","server thread started.");
        DatagramSocket serverSocket = null;

        try
        {
            // Open Server Port
            serverSocket = new DatagramSocket(server.SERVERPORT);
            byte[] receiveData  = new byte[32];
            byte[] sendData     = new byte[32];
            // loop until "server_finished"  becomes False.
            while(createserver.server_finished)
            {
                if(renderer.gGL!=null)  // ignore me, just a null pointer checker
                {
                    // waiting for the incoming client's message packet
                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                    serverSocket.receive(receivePacket);
                    renderer.recv = new String(receivePacket.getData());
                    Log.d("server","packet received from client, ETA " + timing.getNow() + " " + renderer.recv); // timing getNow - just returns current system minute & second.

                    // server is replying to the client back with a message.
                    InetAddress IPAddress = receivePacket.getAddress();
                    int port = receivePacket.getPort();

                    sendData = new String("server msg").getBytes();
                    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                    serverSocket.send(sendPacket);
                    renderer.sent = new String(sendData, 0, sendData.length);
                    Log.d("server","packet sent to client, ETA " + timing.getNow() + " " + renderer.sent);     // timing getNow - just returns current system minute & second.
                }
            }
            // close the socket
            if(serverSocket!=null) serverSocket.close();
            serverSocket = null;
        }
        catch (Exception e) 
        {
            Log.e("server", "Error", e);
            if(serverSocket!=null) serverSocket.close();
            serverSocket = null;
        }
        finally 
        {
            if(serverSocket!=null) serverSocket.close();
            serverSocket = null;
        }
        Log.d("redwing","server thread terminated.");
    }
}

客户端 java类(重新编辑,客户端不接收来自服务器的消息):

public class client implements Runnable 
{
    public static final int CLIENTPORT  = 5000;
    // running Client thread.
    public void run() 
    {
        Log.d("redwing","client thread started.");
        DatagramSocket clientSocket = null;

        try
        {
            // getting local address 
            clientSocket = new DatagramSocket(server.SERVERPORT);
            InetAddress IPAddress = InetAddress.getByName("192.168.0.100");  

            // displaying IP & hostname.
            Log.d("client", "IP: " + IPAddress.getHostAddress() + " Name: " + IPAddress.getHostName());
            byte[] sendData     = new byte[32];
            byte[] receiveData  = new byte[32];
            // Loop until client_finished becomes False.
            while(createclient.client_finished)
            {
                if(renderer.gGL!=null)  // ignore me, just a null pointer checker
                {
                    // sending a message to the server
                    sendData = timing.getNow().getBytes();
                    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, client.CLIENTPORT);
                    clientSocket.send(sendPacket);
                    renderer.sent = new String(sendData,0,sendData.length);;
                    Log.d("client","packet sent to server, ETA " + timing.getNow());  

                    // waiting for the server packet message.
                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                    clientSocket.receive(receivePacket);
                    renderer.recv = new String(receivePacket.getData());
                    Log.d("client","packet received from server, ETA " + timing.getNow());
                }
            }
            // close the socket
            if(clientSocket!=null) clientSocket.close();
            clientSocket = null;
        }
        catch (Exception e) 
        {
            Log.e("client", "Error", e);
            if(clientSocket!=null) clientSocket.close();
            clientSocket = null;
        }
        finally 
        {
            if(clientSocket!=null) clientSocket.close();
            clientSocket = null;
        } 
        Log.d("redwing","client thread terminated.");
    }
}

清单中启用了Permisions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<user-permission android:name="android.permission.NETWORK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

我在Android PC模拟器上运行服务器,在我的Android手机上运行客户端。

客户和客户服务器线程运行得很好,但是......问题是我不能在它们之间交换任何数据。 客户端没有收到任何内容,服务器也没有收到任何内容。数据包已发送但未收到任何内容。

我做错了什么?

请帮帮我 提前谢谢。

1 个答案:

答案 0 :(得分:1)

运行模拟器后,在命令提示符下键入它 - “telnet localhost”,然后键入“redir add udp:5000:6000”。使用端口号5000连接客户端并使用端口号6000打开udp服务器。然后,您可以从udp服务器接收客户端消息。

Take a look for details

clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("<pc ip>"); // instead of "localhost"


public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!  
public static final int SERVERPORT = 6000;   

/* Retrieve the ServerName */  
InetAddress serverAddr = InetAddress.getByName(SERVERIP);  
DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);