doInBackground卡 [英] doInBackground gets stuck

查看:163
本文介绍了doInBackground卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用多个端口,因此不同的数据和信息可以发送和接收方便的服务器,但在我doInBackground方法,我的code卡上socket.receive,
这里是我的code

I'm trying to create a server that uses multiple ports so different data and information can be sent and received with ease, but in my doInBackground method, my code gets stuck on a socket.receive, Here is my code

while( run )
    {
        //GameServerID
        try
        {
            if(gameServerID == null)
            {
                gameServerID = new DatagramSocket( portID );
            }
            //try to receive data
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            try
            {
                Log.d(TAG, "Wait for something to connect");
                gameServerID.receive( packet ); <--GETS STUCK HERE
            }
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }
            String data = new String( buf, 0, packet.getLength() );
            Log.d(TAG, data);
            //Send out the ID to the client
            byte[] bufer = new byte[256];
            //Send a message "connect" to the host
            String msg = Integer.toString( players );
            players = players + 1;
            bufer = msg.getBytes();
            InetAddress address;
            //Default ip address of the host
            address = packet.getAddress();
            DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, portID );
            //Send packet
            gameServerID.send( p );
            addresses.add( address );
        }
        catch (SocketException e) 
        {
            Log.d(TAG, "Error with socket");
            e.printStackTrace();
        } 
        //Listen for a client to connect
        catch (IOException e) 
        {
            Log.d(TAG, "Error with I/O");
            e.printStackTrace();
        }

        //GameServerPositions
        try
        {
            Log.d(TAG, "Run the gamePositions code");
            if(gamePositions == null)
            {
                gamePositions = new DatagramSocket( portPos );
            }
            //Receive position
            //try to receive data
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            try
            {
                gamePositions.receive( packet );
            }
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }
            String data = new String( buf, 0, packet.getLength() );
            Log.d(TAG, data);
            String[] pos = data.split(":");
            for(int i = 0;i<pos.length;i++)
            {
                Log.d(TAG, pos[i]);
            }
            xValues[ Integer.parseInt( pos[0] ) ] = Integer.parseInt( pos[1] );
            yValues[ Integer.parseInt( pos[0] ) ] = Integer.parseInt( pos[2] );
        }
        catch (SocketException e) 
        {
            Log.d(TAG, "Error with socket");
            e.printStackTrace();
        } 
        //Listen for a client to connect
        catch (IOException e) 
        {
            Log.d(TAG, "Error with I/O");
            e.printStackTrace();
        }

        //GameServerSendPos
        try
        {
            Log.d(TAG, "Run the gamePositionsSend code");
            String data = "";
            if( gameSendPos == null )
            {
                gameSendPos = new DatagramSocket( portSend );
            }

            //create the string ready to be sent out
            for(int i = 0; i < 8; i++)
            {
                if(xValues[i] >= 0)
                {
                    data += i + ":" + xValues[i] + ":" + yValues[i] + ":";
                }
            }

            byte[] bufer = new byte[256];
            bufer = data.getBytes();
            DatagramPacket p = null;

            for(int i = 0;i < addresses.size(); i++)
            {
                if( addresses.get(i) != null )
                {
                    p = new DatagramPacket( bufer, bufer.length , addresses.get(i), portSend );
                    gameSendPos.send( p );
                }
            }

        }
        catch (SocketException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

我能做些什么,从卡住停止,或经过这么漫长的等待,只跳过它?只是为了更多的信息,当第一个客户端连接时,code正常工作,直到它再次到达顶部,然后socket.received只是堵塞了......

What can I do to stop it from getting stuck, or after so long waiting, just to skip it? Just for more information, when the first client connects, the code works fine, until it reaches the top again and then the socket.received just jams up...

画布

更新

我已经改变了我的code为3个不同的类别,从我做起他们在我的服务器,同时循环像这样

I have changed my code into 3 different classes, I start them in my servers while loop like so

while( run )
    {
        if(start == true)
        {
            gsID.doInBackground( );
            addresses = gsID.addresses;
            gsPos.addresses = addresses;
            gsPos.doInBackground( );
            gsSendPos.addresses = addresses;
            gsSendPos.positions = gsPos.positions;
            gsSendPos.doInBackground( );
            start = false;
        }
        else
        {
            addresses = gsID.addresses;
            gsPos.addresses = addresses;
            gsSendPos.addresses = addresses;
            gsSendPos.positions = gsPos.positions;
        }
    }

但它仍然再次被卡住的gameServerID接收方法。

But it still once again gets stuck on the gameServerID receive method.

我只是改变了我所有的类线程,它工作得更好,但是在Android 4.0以上版本的线程的更高版本无法从UI使用,但我不知道我是如何开始他们不是UI ?

I just changed all my classes to thread, and it works better, but in the later versions of android 4.0+ threads can't be used from the UI, but i'm not sure how I start them from not the UI?

推荐答案

从的Javadoc <一个href=\"http://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html#receive%28java.net.DatagramPacket%29\"相对=nofollow> DatagramSocket.receive :

From the Javadoc for DatagramSocket.receive:

此方法一直阻塞数据报收到

This method blocks until a datagram is received

所以你的code为'坚持',因为它正在等待数据包到达。这意味着,连接到该服务器/端口的客户端还没有发送一个数据包使插座被挡住的设计

So your code is 'stuck' because it is waiting for data packets to arrive. This means the client that is connecting to this server/port hasn't yet sent another packet so the socket is blocking by design.

传统上,如果你要听上在同一时间多个端口,你有两种选择:

Traditionally if you want to listen on multiple ports at the same time you have two options:


  1. 在不同的线程打开每个阻挡套接字(这不会很好地扩展,如果你将有大量的客户机同时由于每个线程一个客户端,但对于少数的连接工作正常)

  2. 使用NIO是非阻塞IO的Java

这篇关于doInBackground卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆