开发者

Android client socket. UnknowHostException

I have a working server application in JAVA (multithreaded) and a working client console application.

Now I'm trying to do an android client, however using Eclipse and the Android emulator I can't connect to my local server ....

  • yes, I use 10.0.2.2 as my server ip.
  • yes, Manifest file is modified with INTERNET permission.

            import java.io.BufferedReader;
            import java.io.BufferedWriter;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.io.OutputStreamWriter;
            import java.io.PrintWriter;
            import java.net.Socket;
            import java.net.UnknownHostException;
    
            import android.app.Activity;
            import android.os.Bundle;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.widget.Button;
            import android.widget.ImageButton;
            import android.widget.TextView;
            import android.widget.Toast;
    
            public class RealApp extends Activity implements OnClickListener {
    
                private PrintWriter pw;
                private BufferedReader in;
                private boolean connected = false;
    
                public void onCreate(Bundle savedInstanceState) 
                {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
    
                    if (!connected) 
                    {
                        Thread cThread = new Thread(new ClientThread());
                        cThread.start();
                    }
    
    
                }
    
    
    
                public class ClientThread implements Runnable {
    
                    public void run() {
    
                        {
    
                            Socket skt = null;
                            try {
                                skt = new Socket("l0.0.2.2", 1337);
                            } catch (UnknownHostException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
    
                            String s = "";
                            String inString = "";
    
                            BufferedReader in = null;
                            try {
                                in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                            } catch (IOException e2) {
                                // TODO Auto-generated catch block
                                e2.printStackTrace();
                       开发者_运维问答     }
                            PrintWriter pw = null;
                            try {
                                pw = new PrintWriter(skt.getOutputStream(), true);
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
    
    
                            do
                            {
                                try {
                                    inString = in.readLine();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
    
    
                            }
                            while(!s.equals("quit"));
    
    
                            pw.close();
                            try {
                                skt.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
    
    
    
                        }
                    }
                }}
    

During a debug, in the Log window I can notice a System.err message, an UnknowHostException for 10.0.2.2.


Try creating an InetAddress and using the Socket constructor which takes an InetAddress, this should avoid the lookup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜