我在java中编写了tcp-server的代码,并在java中使用tcp-client进行了测试并且运行良好,但是当我在android中编写了一个tcp-server时,我得到了这个例外:
09-06 15:15:37.792: ERROR/ClientActivity(444): java.net.SocketException: Permission denied
09-06 15:15:37.792: ERROR/ClientActivity(444): at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
09-06 15:15:37.792: ERROR/ClientActivity(444): at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
09-06 15:15:37.792: ERROR/ClientActivity(444): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
09-06 15:15:37.792: ERROR/ClientActivity(444): at java.net.Socket.startupSocket(Socket.java:698)
09-06 15:15:37.792: ERROR/ClientActivity(444): at java.net.Socket.tryAllAddresses(Socket.java:150)
09-06 15:15:37.792: ERROR/ClientActivity(444): at java.net.Socket.<init>(Socket.java:209)
09-06 15:15:37.792: ERROR/ClientActivity(444): at java.net.Socket.<init>(Socket.java:176)
09-06 15:15:37.792: ERROR/ClientActivity(444): at socket.android.ClientActivity$ClientThread.run(ClientActivity.java:52)
09-06 15:15:37.792: ERROR/ClientActivity(444): at java.lang.Thread.run(Thread.java:1019)
java TCP-server的代码:
package messenger.classes;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import messenger.handlers.ClientHandler;
public class Server{
public static final int PORT=8111;
public static void main(String args[]){
final Map<String, ClientHandler> clients =new ConcurrentHashMap<String, ClientHandler>();
try {
ServerSocket ss=new ServerSocket(PORT);
System.out.println("Server starts listening on PORT "+PORT);
for(Socket socket=ss.accept();socket!=null;socket=ss.accept()){
Runnable handler = new ClientHandler(socket,clients);
new Thread(handler).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
android TCP-client的代码:
package socket.android;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ClientActivity extends Activity {
private Button connectPhones;
private String serverIpAddress = "";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
connectPhones = (Button) findViewById(R.id.btnConnect);
connectPhones.setOnClickListener(connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket("127.0.0.1", 8111);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
问题是什么,以及如何解决?
编辑:新错误:
09-06 15:33:57.533: ERROR/ClientActivity(557): java.net.ConnectException: /127.0.0.1:8014 - Connection refused
09-06 15:33:57.533: ERROR/ClientActivity(557): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
09-06 15:33:57.533: ERROR/ClientActivity(557): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-06 15:33:57.533: ERROR/ClientActivity(557): at java.net.Socket.startupSocket(Socket.java:705)
09-06 15:33:57.533: ERROR/ClientActivity(557): at java.net.Socket.tryAllAddresses(Socket.java:150)
09-06 15:33:57.533: ERROR/ClientActivity(557): at java.net.Socket.<init>(Socket.java:209)
09-06 15:33:57.533: ERROR/ClientActivity(557): at java.net.Socket.<init>(Socket.java:176)
09-06 15:33:57.533: ERROR/ClientActivity(557): at socket.android.ClientActivity$ClientThread.run(ClientActivity.java:48)
09-06 15:33:57.533: ERROR/ClientActivity(557): at java.lang.Thread.run(Thread.java:1019)
答案 0 :(得分:2)
127.0.0.1尝试连接到您的模拟器,用您的计算机ip替换127.0.0.1。例如:192.168.1.10
答案 1 :(得分:1)
也许缺少INTERNET权限?
答案 2 :(得分:1)
如果存在权限问题或权限被拒绝,请打开AndroidManifest.xml
文件并添加
<uses-permission android:name="android.permission.INTERNET">