我有客户端和服务器应用程序在单独的 android 设备上运行,但客户端无法与服务器通信后我无法找出错误,请检查错误,提前感谢我的客户端类是:
public class MainActivity extends Activity {
EditText et1;
Button bt1;
InetAddress IPAddress;
DatagramPacket dp;
String message;
int SERVERPORT = 11111;
String SERVER_IP = "192.168.1.101";
DatagramSocket clientSocket;
byte[] sendData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.EditText01);
bt1=(Button)findViewById(R.id.myButton);
message=et1.getText().toString();
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new ClientThread()).start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
clientSocket = new DatagramSocket(SERVERPORT);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
IPAddress = InetAddress.getByName(SERVER_IP);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sendData = message.getBytes();
dp = new DatagramPacket(sendData, sendData.length, IPAddress, SERVERPORT);
try {
clientSocket.send(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientSocket.close();
return;
}
}
}
我的服务器是:
public class MainActivity extends Activity {
TextView tv1,tv2;
InetAddress broadcastIP;
private Boolean shouldRestartSocketListen=true;
static String UDP_BROADCAST = "UDPBroadcast";
DatagramSocket serverSocket=null;
byte[] data=null;
DatagramPacket dp=null;
int SERVERPORT = 11111;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
new Thread(new serverThread()).start();
}
class serverThread implements Runnable {
private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception {
byte[] recvBuf = new byte[15000];
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
while (shouldRestartSocketListen) {
if (serverSocket == null || serverSocket.isClosed())
{
serverSocket = new DatagramSocket(port,broadcastIP);
// serverSocket.setBroadcast(true);
}
serverSocket.receive(packet);
String senderIP = packet.getAddress().getHostAddress();
String message = new String(packet.getData());
//Log.e("UDP", "Got UDB broadcast from " + senderIP + ", message: " + message);
tv1.setText(message);
//broadcastIntent(senderIP, message);
serverSocket.close();
}
}
private void broadcastIntent(String senderIP, String message) {
Intent intent = new Intent(MainActivity.UDP_BROADCAST);
intent.putExtra("sender", senderIP);
intent.putExtra("message", message);
sendBroadcast(intent);
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
broadcastIP = InetAddress.getByName("192.168.1.100");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
listenAndWaitAndThrowIntent(broadcastIP, SERVERPORT);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void stopListen() {
shouldRestartSocketListen = false;
serverSocket.close();
}
}
没有日志猫,因为我在真实设备上运行。