gpt4 book ai didi

java - doInBackground 卡住了

转载 作者:行者123 更新时间:2023-12-02 07:08:37 26 4
gpt4 key购买 nike

我正在尝试创建一个使用多个端口的服务器,以便可以轻松发送和接收不同的数据和信息,但在我的 doInBackground 方法中,我的代码卡在了 socket.receive 上,这是我的代码

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();
}
}

我能做些什么来阻止它被卡住,或者在等了这么久之后,只是跳过它?只是为了了解更多信息,当第一个客户端连接时,代码工作正常,直到它再次到达顶部,然后 socket.received 就会卡住......

Canvas

更新

我已将代码更改为 3 个不同的类,我在服务器中启动它们,就像这样循环

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 接收方法上。

我刚刚将所有类更改为线程,并且效果更好,但是在 Android 4.0+ 的更高版本中,无法从 UI 使用线程,但我不确定如何从 UI 启动它们?

最佳答案

来自 DatagramSocket.receive 的 Javadoc :

This method blocks until a datagram is received

因此您的代码“卡住”了,因为它正在等待数据包到达。这意味着连接到此服务器/端口的客户端尚未发送另一个数据包,因此套接字在设计上处于阻塞状态。

传统上,如果您想同时监听多个端口,您有两个选择:

  1. 在不同的线程上打开每个阻塞套接字(如果您同时拥有大量客户端,由于每个线程一个客户端,这将无法很好地扩展,但对于少量连接来说可以正常工作)
  2. 使用 NIO,这是 Java 的非阻塞 IO

关于java - doInBackground 卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com