服务器没有监听android中UDP套接字中的任何端口号

时间:2013-12-12 17:59:08

标签: java android sockets

我正在创建一个应用程序,其中udp服务器将监听所有客户端,例如。来自pc或iphone或任何其他客户。但是,如果我发送数据包,服务器端没有任何事情发生。 `服务器:

public class Server implements Runnable {

    @Override
    public void run() {
        while(start==false)
        {
        }
        try {
            WifiManager wm=(WifiManager)getSystemService(WIFI_SERVICE);


            updatetrack("nServer: Start connectingn" + Inet4Address.getLocalHost().getHostAddress() );
            DatagramSocket socket=new DatagramSocket(58797);
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            updatetrack("Server: Receivingn");
            byte [] arr =new byte[10000];
            int j=0;
            int x=0;
            while(true){
                packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                //buf=packet.getData();
                x=0;
               /*if (buf.length < 10)
                {
                    String string2=(new String(buf));
              //      break;
                }*/
             String string2=new String(packet.getData(),0,packet.getLength());

                if(string2.equals(":")){
                    Bitmap bd = BitmapFactory.decodeByteArray(arr,0,arr.length);
            //        update_iv(bd);
                    ImageView iv=(ImageView)findViewById(R.id.imageView1);
                    iv.setImageBitmap(bd);
                    this.wait(111111111);
                    break;
                }
                for(int i=j;i<packet.getData().length;i++){
                    arr[i]=packet.getData()[x++];
                    j++;
                }
            }
            updatetrack("Server: Received");


        } catch (Exception e) {
            updatetrack("Server: Error!n"+e.getMessage());
        }
    }
}` 
Client on my pc:
void sendstring(){
      try {

            InetAddress serverAddr = InetAddress.getByName("192.168.1.15");
            System.out.println("Client: Start connectingn");
            DatagramSocket socket = new DatagramSocket();

            byte[] sendData = new byte[1024];
            byte[] buf = new byte[1024];
            String sentence;
            int bytesRead = 0;
            /*while((bytesRead = bis.read(buf))!=-1){


                // sendData = new String(buf, 0, bytesRead);
                //sendData = sentence.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(buf, buf.length,
                        serverAddr, 58797);
                socket.send(sendPacket);





            }*/
            String string =new String("hello world");
            sendData = string.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, string.length(),
                    serverAddr, 58797);
            socket.send(sendPacket);
            string =new String(":");
              sendData = string.getBytes();
             sendPacket = new DatagramPacket(sendData, string.length(),
                    serverAddr, 58797);
            socket.send(sendPacket);

            /*
            if(!input.getText().toString().isEmpty())
            {
                buf=input.getText().toString().getBytes();
            }
            else
            {
                buf = ("Default message").getBytes();
            }
            DatagramPacket packet = new DatagramPacket(buf,


                    buf.length, serverAddr, SERVERPORT);
            updatetrack("Client: Sending ‘" + new String(buf) + "’n");
            socket.send(packet);
            updatetrack("Client: Message sentn");
            updatetrack("Client: Succeed!n");*/

        } catch (Exception e) {
        System.out.println("Client: Error!n"+e.getMessage());
        }
}

0 个答案:

没有答案