我只能用我的 Client.class 发送数据包,但我不能用我的 Server.class 接收数据包。客户端和服务器在每种情况下都是两个线程。要更新MainActivity
我使用处理程序。处理程序有效(我对其进行了测试)但不是 Server.class。
public class Client implements Runnable {
//@Override
public void run() {
do{
try {
InetAddress serverAddr = InetAddress.getByName(Server.CLIENTIP);
DatagramSocket socket = new DatagramSocket();
byte[] buf = ("Test").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length,serverAddr,Server.CLIENTPORT);
socket.send(packet);
}
catch (Exception e) {}
}
while(open == true);
}
}
public class Server implements Runnable {
public static String CLIENTIP = "192.168.0.13";//send-ip
public static String SERVERIP = "10.0.0.18";//receive-ip
public static final int CLIENTPORT = 5000; //send-port
public static final int SERVERPORT = 5001; //receive-port
//@Override
public void run() {
do
{
try {
DatagramSocket socket = new DatagramSocket(SERVERPORT);
byte[] buf = new byte[256];
DatagramPacket rpacket = new DatagramPacket(buf, buf.length);
socket.receive(rpacket);
String x = new String(rpacket.getData());
MainActivity.glText = x;
MainActivity.mHandler.post(MainActivity.mTimer);
}
catch (Exception e) {}
}
while(open == true);
}
}
有没有人解决这个问题?感谢您的回答!;)