我有客户端和服务器应用程序在单独的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();
}
}
没有log cat,因为我在真实设备上运行。
答案 0 :(得分:0)
我不能因为声誉而添加coments但是使用asynctasks ..为send创建一个类,为receive创建另一个类。我把两者放在一起......未经测试。
public class SenderBroadcastAsyncTask extends AsyncTask<Void, Integer, String> {
Context context;
public SenderBroadcastAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
@Override
protected String doInBackground(File... imagen) {
int port =1515;
DatagramSocket socket = null;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
try {
socket.setBroadcast(true);
} catch (SocketException e) {
e.printStackTrace();
}
//////send socket
int eport = 1616;
InetAddress eip = null;
try {
eip = InetAddress.getByName("192.168.1.1"); ////SERVER IP ADDRESS ----
} catch (UnknownHostException e) {
e.printStackTrace();
}
DatagramSocket esocket = null;
try {
esocket = new DatagramSocket(eport);
} catch (SocketException e) {
e.printStackTrace();
}
/////sending
string send = "something";
byte[] sendbyte = send.getBytes
DatagramPacket somedatagrampacket = new Datagrampacket(sendbyte,sendbyte.length,eip,eport);
esocket.send(somedatagrampacket)
//////Start receive
while(true)
{
byte[] message = new byte[60*1024];
DatagramPacket recv_packet = new DatagramPacket(message, message.length);
try {
socket.receive(recv_packet);
} catch (IOException e) {
e.printStackTrace();
}
///Do something whit recv_packet
}
protected void onPostExecute(String result) {
///something to do whit results
}
}