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 に何も出力されていないため、奇妙な理由で接続が切断されるのが本当に気になります。
キャンバス
PS 必要に応じてさらにコードを指定できます
これが私のサーバー実行方法です
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");
}
コメント失礼します