gpt4 book ai didi

java - Android线程刚刚停止

转载 作者:太空狗 更新时间:2023-10-29 14:22:12 25 4
gpt4 key购买 nike

我有一个处理接收和发送 UDP 数据包的线程,它工作正常,直到收到 X 数量的数据包,然后客户端停止尝试接收数据包,并停止发送它们,所有这些catch 函数不显示任何内容,客户端只是停止请求数据包。这是我的客户端运行代码

public void run()
{
if( host == true ) { setUpClient(); server.start(); }
rdyForPlay = true;
boolean run = true;
boolean setupPlayer = false;
while( run )
{
//Tell the server to give position of players
//if( setupPlayer == true )
//{
// setUpClient();
// setupPlayer = false;
//}

if( host == false )
{
try {
if(socket == null)
{
socket = new DatagramSocket( port );
}
byte[] bufer = new byte[256];
//String msg = "position";
String msg = ID +":"+ assets[ID].returnPosX() +":"+ assets[ID].returnPosY();
int msgLength = msg.length();
bufer = msg.getBytes();
InetAddress address;
address = InetAddress.getByName("192.168.1.59");
DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
socket.send( p );

} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
Log.d(TAG, "Error with unknown host");
e2.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
Log.d(TAG, "Error with socket");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d(TAG, "Error with sending/receiving data");
e.printStackTrace();
}

byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket( buf, buf.length );
try
{
socket.receive( packet );
}
catch (IOException e)
{
Log.d(TAG, "Error with receiving data");
e.printStackTrace();
}

String data = new String( buf, 0, packet.getLength() );
//Split the string up
String[] dataArray = data.split("#");
int newTotalPlayers = Integer.parseInt( dataArray[0] );
if( newTotalPlayers != totalPlayers )
{
Log.d(TAG," what is total amount of players:" + newTotalPlayers);
if( newTotalPlayers == 1 )
{
newPlayer( 0 );
totalPlayers = newTotalPlayers;
}
else
{
newPlayer( newTotalPlayers );
totalPlayers = newTotalPlayers;
}
//if( ID == 0 && host == false)
//{
// ID = newTotalPlayers;
// setupPlayer = true;
//}
}
//Do a for loop to go through dataArray
for( int i = 0; i < totalPlayers; i++)
{
String[] pos = dataArray[(i + 1)].split(":");
if( Integer.parseInt( pos[(i*3)] ) == ID )
{
Log.d(TAG, "Do nothing please");
}
else
{
assets[i].setPosition( Integer.parseInt( pos[(i*3) + 1] ), Integer.parseInt( pos[(i*3) + 2] ) );
}
}

}

}
Log.d(TAG, "Error with run value");
}

这工作正常,这真的让我很烦恼,因为一些奇怪的原因,连接在很长时间后断开了,我找不到,因为 LogCat 根本没有打印任何东西

Canvas

附言如果需要可以提供更多代码

这是我的服务器运行方法

public void run() {
InetAddress client = null;
boolean run = true;
String data = "";
DatagramPacket packet = null;
while( run )
{
if( data.equalsIgnoreCase( "" ) )
{
/*Log.d(TAG, "waiting for clients");
String msg = "waiting";
int msgLength = msg.length();
byte[] message = msg.getBytes();
DatagramPacket p = new DatagramPacket( message, msgLength, client, port );
try
{
socket.send( p );
}
catch (IOException e2)
{
Log.d(TAG, "Error with sending");
e2.printStackTrace();
}*/
}

//Send some data
if( data.equalsIgnoreCase( "connect" ) )
{
Log.d(TAG, "ID send :" + packet.getAddress());
address.add( packet.getAddress() );
players += 1;
String msg = String.valueOf( players );
int msgLength = msg.length();
byte[] message = msg.getBytes();
DatagramPacket p = new DatagramPacket( message, msgLength, packet.getAddress(), port );
try
{
socket.send( p );
}
catch (IOException e2)
{
Log.d(TAG, "Error with sending");
e2.printStackTrace();
data = "";
}
}

//if( /*data.equalsIgnoreCase( "position" )*/ address.size() > 0 )
//{
//Create for loop to create the string
String msg = "";
msg = players + "#";
for(int i = 0; i < players; i++)
{
msg += + i + ":" + assets.get(i).returnPosX() + ":" + assets.get(i).returnPosY() + ":";
}
//msg = String.valueOf(
// players + ":" +
// "1:" + assets.get(0).returnPosX() + ":" + assets.get(0).returnPosY() );
int msgLength = msg.length();
byte[] message = msg.getBytes();
for(int i = 0; i < address.size() ; i++)
{
DatagramPacket p = new DatagramPacket( message, msgLength, address.get(i), port );
try
{
socket.send( p );
}
catch (IOException e2)
{
Log.d(TAG, "Error with sending");
e2.printStackTrace();

}
}
//Log.d(TAG, "Data sent is:" + msg);
//}

data = " ";

//Receive some data
byte[] buf = new byte[256];
packet = new DatagramPacket( buf, buf.length );
try
{
socket.receive( packet );
}
catch (IOException e)
{
Log.d(TAG, "Error with receiving data");
e.printStackTrace();
}

data = new String( buf, 0, packet.getLength() );
//Log.d(TAG, "Data received was :" + data);

try
{
this.sleep( 25 );
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
Log.d(TAG, "Error with trying to sleep");
e.printStackTrace();
}
}
Log.d(TAG, "Error with while run value");
}

对评论感到抱歉

最佳答案

似乎发生了死锁情况,两个源代码都卡在 socket.receive

幸运的是,Android 代码和 J2SE UDP 服务器客户端代码是相同的,因此请在您的机器上尝试此代码并进行调试以查看发生了什么。可以放打印语句更清楚。

此外,UDP 是不可靠的。我没有在您的代码中看到数据包接收确认和重新发送机制。这是必需的。您不能假设您发送的数据包会被另一端接收。

关于java - Android线程刚刚停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15678445/

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