Java 多个UDP客户端向一个主机发送数据包';行不通
我在一台设备上运行了一个简单的UDP服务器,当一个客户端想要连接时,它向服务器发送一条消息“connect”,然后服务器获取地址,并开始向该地址发送数据,现在当另一个客户端想要加入时,该客户端发送消息“connect”,但有时什么也不会发生,这就像希望加入消息的客户端从未被服务器接收到一样,我该如何解决这个问题?这里还有服务器的代码Java 多个UDP客户端向一个主机发送数据包';行不通,java,android,networking,udp,Java,Android,Networking,Udp,我在一台设备上运行了一个简单的UDP服务器,当一个客户端想要连接时,它向服务器发送一条消息“connect”,然后服务器获取地址,并开始向该地址发送数据,现在当另一个客户端想要加入时,该客户端发送消息“connect”,但有时什么也不会发生,这就像希望加入消息的客户端从未被服务器接收到一样,我该如何解决这个问题?这里还有服务器的代码 protected String doInBackground(String... params) { boolean run = true; S
protected String doInBackground(String... params) {
boolean run = true;
String data = "";
DatagramPacket packet = null;
boolean position = false;
while( run )
{
if( data.equalsIgnoreCase( "" ) )
{
}
//Send some data
if( data.equalsIgnoreCase( "connect" ) && wait == true )
{
Log.d(TAG, "Someone wants to connect");
//Increase the total players by 1
players = players + 1;
//Notify to the host (client) something has change
//notify client
//Send a message to the client with the ID
byte[] bufer = new byte[256];
//Send a message "connect" to the host
String msg = Integer.toString( players );
int msgLength = msg.length();
bufer = msg.getBytes();
InetAddress address;
//Default ip address of the host
//Take the address from the packet
addresses.add( packet.getAddress() );
Log.d(TAG, "Address is " + addresses.get( addresses.size() - 1 ) );
address = addresses.get( addresses.size() - 1 );
DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
//Send packet
try
{
socket.send( p );
}
catch (IOException e)
{
e.printStackTrace();
}
wait = false;
}
if( wait == true && position == true )
{
position = false;
wait = false;
}
for(int i = 0;i < positions.length; i++)
{
if(positions[i] != null)
{
//Log.d(TAG, "X and Y position of asset:"+i+", is:"+ positions[i]);
}
}
//Needs to try and reteive data...
if( wait == false )
{
//Log.d(TAG, "Waiting to retreive data");
byte[] buf = new byte[256];
packet = new DatagramPacket( buf, buf.length );
try
{
socket.receive( packet );
wait = true;
}
catch (IOException e)
{
Log.d(TAG, "Error with receiving data");
e.printStackTrace();
}
data = new String( buf, 0, packet.getLength() );
//Log.d(TAG, "Data received from :" + packet.getAddress() + ", holds this value: " + data);
String[] dataStrings = data.split(":");
if( dataStrings[0].equalsIgnoreCase( "position" ) )
{
position = true;
}
}
//Log.d(TAG, "Data received was :" + data);
/*try
{
Thread.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");
return "finished";
}
public void connectToServer()
{
//Send a connect message to the server
try {
//Create a socket
socket = new DatagramSocket( port );
byte[] bufer = new byte[256];
//Send a message "connect" to the host
String msg = "connect";
int msgLength = msg.length();
bufer = msg.getBytes();
InetAddress address;
//Default ip address of the host
address = InetAddress.getByName("192.168.1.59");
DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
//Send packet
socket.send( p );
} catch (UnknownHostException e2) {
Log.d(TAG, "Unknown host");
e2.printStackTrace();
} catch (SocketException e) {
Log.d(TAG, "Socket problem");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "I/O problem");
e.printStackTrace();
}
//Receive the message back
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket( buf, buf.length );
//Try to receive a packet from the server
try
{
Log.d(TAG, "Waiting for data");
socket.receive( packet );
}
//Error
catch (IOException e)
{
Log.d(TAG, "Error with receiving data");
e.printStackTrace();
}
//Convert the packet to a string
String data = new String( buf, 0, packet.getLength() );
//Use the string to find out what ID this client is
ID = Integer.parseInt( data );
//Setup the client game
setUpClient();
}
如果出现问题,我是否应该尝试让connectToServer方法继续尝试
Canvas在第一个“连接”字符串之后,客户端不会向服务器发送任何内容。因此,服务器将永远在该行等待:
socket.receive( packet ); // <- will never return
wait = true;
socket.receive(数据包);//我现在正在做这项工作,我也在使用AsyncTask,因为它在Android设备上运行,其中一些在ver4及以上版本上运行,线程不能通过GUI使用,一旦一切正常,我当然会检查代码,只想让东西正常工作:)哦,客户端在连接后会继续向服务器发送数据,我没有显示代码,因为这不是问题所在,所以发生的情况是,已连接的客户端使套接字接收忙,而新客户端无法发送连接,因为它忙。