0

我正在制作一个非常简单的应用程序,在其中我从我的 PC 上的 java 程序发送一个文本字符串,我试图从我的模拟器中的 android 应用程序接收相同的字符串。

这是我的活动(服​​务器):

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appstart);
    tv = (TextView) findViewById(R.id.textView1);
    try {
        DatagramSocket clientsocket = new DatagramSocket(9876);
        byte[] receivedata = new byte[1024];
        DatagramPacket recv_packet = new                   DatagramPacket(receivedata, receivedata.length);
        Log.d("UDP", "S: Receiving 1 sec...");
        clientsocket.receive(recv_packet);
        String rec_str = new String(recv_packet.getData());
        tv.setText(rec_str);
        Log.d(" Received String ", rec_str);
        InetAddress ipaddress = recv_packet.getAddress();
        int port = recv_packet.getPort();
        Log.d("IPAddress : ", ipaddress.toString());
        Log.d(" Port : ", Integer.toString(port));                                   clientsocket.close();
    } catch (Exception e) {
        Log.e("UDP", "S: Error", e);
    }
}

这是我的PC客户端java代码:

  public static void main(String args[]) throws Exception
    {
            while(true)
    {           
    BufferedReader inFromUser = new BufferedReader(new       InputStreamReader(System.in));
            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
    System.out.println(IPAddress.getHostName());
            byte[] sendData = new byte[1024];
            byte[] receiveData = new byte[1024];
            String sentence = inFromUser.readLine();
            sendData = sentence.getBytes();
            DatagramPacket sendPacket = new    DatagramPacket(sendData,sendData.length,IPAddress,9876);
            clientSocket.send(sendPacket);
    System.out.println(sendPacket.getPort());
            clientSocket.close();
    }       
   }

onReceive()然而,该应用程序没有收到任何东西,但在 android 应用程序中的函数上抛出异常......

有谁知道为什么?

4

2 回答 2

0

Try this POC. it sends and listens for the packets. split the listener into another thread

package a.first;

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


public class Serv {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

        Listner listner = new Listner();
        Thread thread = new Thread(listner);
        thread.start();


        String messageStr = "Hello msg1";
        int server_port = 2425;
        DatagramSocket s = new DatagramSocket();
        InetAddress local = InetAddress.getByName("172.20.88.223");
        int msg_length = messageStr.length();
        byte[] message = messageStr.getBytes();
        DatagramPacket p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg1");
        s.send(p);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
        messageStr = "Hello msg2";
        msg_length = messageStr.length();
        message = messageStr.getBytes();
        p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg2");
        s.send(p);
}


}
   class Listner implements Runnable
    { 
            @Override
            public void run() {
        String text = null;
        while(true){
            text = null;    
        int server_port = 2425;
        byte[] message = new byte[1500];
        DatagramPacket p = new DatagramPacket(message, message.length);
        DatagramSocket s = null;
        try{
           s = new DatagramSocket(server_port);
        }catch (SocketException e) {
            e.printStackTrace();
            System.out.println("Socket excep");
        }
        try {
        s.receive(p);
       }catch (IOException e) {
            e.printStackTrace();
                System.out.println("IO EXcept");
            }
        text = new String(message, 0, p.getLength());
        System.out.println("message = "+text);
        s.close();

    }
}

}
于 2013-02-22T11:19:42.667 回答
0

你得到 android.os.NetworkOnMainThreadException 因为你在你的主线程上运行网络方法,这将使你的应用程序没有响应。尝试将其放入单独的线程中,或者......在这种情况下,可能是您的 Activity 可以绑定到的 Service。(有关示例,请参阅如何修复 android.os.NetworkOnMainThreadException? )

如果(不推荐)您只需要测试您的 UDP 服务,您可以将您的 API 要求设置为 7 或以下或禁用主线程策略上的网络,您将不会收到异常。但是,您的应用程序将非常无响应。

于 2012-09-25T16:14:14.233 回答