gpt4 book ai didi

java - 将android TCP客户端连接到java TCP服务器时出现异常

转载 作者:可可西里 更新时间:2023-11-01 02:56:28 24 4
gpt4 key购买 nike

我在 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 服务器的代码:

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 客户端代码:

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)

最佳答案

127.0.0.1 是尝试连接到您的模拟器,将 127.0.0.1 替换为您的计算机 ip。例如:192.168.1.10

关于java - 将android TCP客户端连接到java TCP服务器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7323100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com