0
我遇到問題,我不知道是否有可能。從服務器(Java應用程序)到Android應用程序的套接字
我有一個Android應用程序是客戶端套接字。
這是爲了在Android應用程序將消息發送到服務器的代碼:
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
System.out.println("envia mensaje");
client = new Socket("ip of server", 4444); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
在同一時間,在Android應用程序,一個插座在4445端口創建:
private class SocketServerThread extends Thread {
static final int SocketServerPORT = 4445;
int count = 0;
@Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
SlimpleTextClientActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println("I'm waiting here: "
+ serverSocket.getLocalPort());
}
});
while (true) {
Socket socket = serverSocket.accept();
count++;
message += "#" + count + " from " + socket.getInetAddress()
+ ":" + socket.getPort() + "\n";
SlimpleTextClientActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println(message);
}
});
/*SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
socket, count);
socketServerReplyThread.run();*/
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
而且這是服務器代碼,它在4444端口創建一個套接字:
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println("Dirección entrante"+clientSocket.getInetAddress());
//System.out.println("Dirección entrante2"+clientSocket.get());
System.out.println(message);
ip_a = clientSocket.getInetAddress().toString().substring(clientSocket.getInetAddress().toString().indexOf("/")+1, clientSocket.getInetAddress().toString().length());
//message.substring(message.indexOf(":")+1, message.length()).trim();
System.out.println("realiza conexión a ip:#"+ip_a+"#");
messsage = "enviado desde el servidor"; // get the text message on the text field
SendMessage(ip_a);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
這個工作正常,但我想發送一個mes從服務器到通過套接字所具有的IP的Android設備。
我嘗試使用clientSocket.getInetAddress()和clientSocket.getRemoteSocketAddress(),但無法正常工作。我在4445端口的Android應用程序中創建了一個套接字,但服務器無法連接到Android應用程序的套接字。
你能幫我嗎?
在此先感謝
你爲什麼要創建兩臺服務器?請解釋爲你的整個設置和目標對我來說不清楚。 – greenapps 2014-11-06 12:18:45
我希望2個應用程序可以通過服務器java應用程序進行通信。 – tiomelenas 2014-11-06 14:45:22
好的。但請花時間來解釋整個場景。我需要從代碼中發現嗎? – greenapps 2014-11-06 14:50:17