通过使用 java Sockets,我的小应用程序遇到了一个小问题。
如果我在 pc 上启动我的 Sockert-Server 并且我使用(实习生)网络 IP 通过同一网络中的 wifi 与我的手机连接,我可以将一些东西发送到我的服务器。
但是,如果我尝试通过移动互联网连接发送消息,它不起作用..也没有错误或其他问题:/
这是我的android类:
public class Sockets extends Activity {
EditText textOut;
EditText ipAddress;
TextView textIn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_socket);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
ipAddress = (EditText)findViewById(R.id.ipAddress);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new DatagrammClient(ipAddress.getText().toString(),textOut.getText().toString()).execute("");
textOut.setText("");
}};
private static class DatagrammClient extends android.os.AsyncTask<String, Void, String> {
final static String LOGIN = "LOGIN";
final static String LOGOUT = "LOGOUT";
static int port = 1234;
static int length = 1024; // Länge eines Pakets
static DatagramSocket socket = null;
static InetAddress ia = null;
static DatagramPacket packet;
String hostname = "";
String msg ="";
public DatagrammClient(String hostname,String msg) {
this.hostname = hostname;
this.msg = msg;
}
/**
* Send the login package and open socket
*
* @param servername
* @throws IOException,UnknownHostEception
*/
private static void connectToServer(String servername) throws IOException,UnknownHostException{
packet = null;
byte[] ba = LOGIN.getBytes();
try {
socket = new DatagramSocket();
ia = InetAddress.getByName(servername);
packet = new DatagramPacket(ba, ba.length, ia, port);
Log.d("servername",servername);
Log.d("Internetaddress",ia.toString());
Log.d("SOCKET",socket.toString());
// sende Anmeldung
socket.send(packet);
} catch (SocketException se) {
Log.d("SocketException",se.toString());
}
catch (UnknownHostException he) {
Log.d("UnknownHost: ",he.toString());
}
catch (IOException e) {
Log.d("IOException: ",e.toString());
}
}
public static void sendMessage(String message, String hostname) throws UnknownHostException, IOException{
//if (socket == null){
DatagrammClient.connectToServer(hostname);
//}
DatagrammClient.sendMessageToServer(message);
DatagrammClient.readMessageFromServer();
//Close connection -> Send logout Package?
}
private static void readMessageFromServer(){
// Lesen der empfangenen Pakete erfolgt in eigenem Thread
LeseThread lt = new LeseThread( socket );
}
private static void sendMessageToServer(String message){
byte[] ba = null;
try {
if (!message.equals(LOGOUT)) {
// message = br.readLine();
ba = message.getBytes();
packet.setData(ba, 0, ba.length);
socket.send(packet);
Log.d("Message:",packet.toString());
} else {
ba = LOGOUT.getBytes();
packet.setData(ba, 0, ba.length);
socket.send(packet);
// Exit the system -> do we need to close the socket clientside?
//socket.close();
System.exit(0);
}
} catch (IOException e) {
System.err.println("Ausnahmefehler: " + e);
Log.d("IOEXCEPTION_MESSAGE_TO_SERVER:",e.toString());
}
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
DatagrammClient.sendMessage(msg, hostname);
}
catch(Exception e){
Log.d("Error: ",e.toString());
}
return null;
}
}
}
使用 INTERNET 的权限在 Manifest.. 中设置。