简体   繁体   中英

Exception in connecting android TCP-client to java TCP-server

I wrote code for tcp-server in java, and tested with tcp-client in java and it worked well, but when I have written a tcp-server in android, I got this exception :

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)

Code for 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();

        }
    }
}

code for 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;
            }
        }
    }
}

what is the problem in that, and how to solve it ?

EDIT: new error :

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 is try connect to your emulator, replace 127.0.0.1 by your computer ip. For example: 192.168.1.10

也许缺少INTERNET许可?

如果存在权限问题或权限被拒绝,则打开AndroidManifest.xml文件并添加

<uses-permission android:name="android.permission.INTERNET">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM