[英]Android client socket. UnknowHostException
I have a working server application in JAVA (multithreaded) and a working client console application. 我在JAVA(多线程)中有一个工作的服务器应用程序,还有一个工作在客户端控制台的应用程序。
Now I'm trying to do an android client, however using Eclipse and the Android emulator I can't connect to my local server .... 现在,我尝试做一个android客户端,但是使用Eclipse和Android模拟器,我无法连接到本地服务器...。
yes, Manifest file is modified with INTERNET permission. 是的,清单文件已使用INTERNET权限修改。
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. 在调试期间,在“日志”窗口中,我会注意到一条System.err消息,即10.0.2.2的UnknowHostException。
尝试创建一个InetAddress并使用采用InetAddress的Socket构造函数 ,这应该避免查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.