应该连接到我的PC上本地运行的服务器的代码不起作用。这是一项似乎适合其他人的作业的一部分。我检查了防火墙,正确的地址和端口检测,端口转发等内容。在仿真器上安装Telnet应用程序证明,使用10.0.2.2从模拟器我可以到达我的PC上运行的Python服务器127.0.0.1。 / p>
这是代码,我尝试了各种方法来解决潜在的InetAddress或Socket问题:
package uk.ac.man.cs.COMP28512.lab4;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Created by leeming
* Code snippets from http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/
*/
public class ServerConnect extends Thread{
public static final int BUFFER_SIZE = 2048;
private Socket socket;
private static final int SERVERPORT = 9999; //This is the port that we are connecting to
//Remember the channel simulator is 9998
private static final String SERVERIP = "10.0.2.2"; //This address is magically mapped to the host's loopback.
private static final String LOGTAG = "SocketTester";
boolean terminated = false;
private PrintWriter out = null;
private BufferedReader in = null;
Activity parentref;
/**
*
* @param parentRef Expects a reference to the calling activity, e.g. new ServerConnect(this);
*/
public ServerConnect(Activity parentRef)
{
parentref=parentRef;
}
/**
* Sends commands to the server. Called from UI thread via a button press
* @param cmd
*/
public void send(String cmd)
{
try
{
Log.i(LOGTAG,"Sending command: "+cmd);
out.println(cmd);
}
catch(Exception e)
{
Log.e(LOGTAG,"Failed to send command : "+e);
}
}
/**
* Main thread loop that grabs incoming messages
*/
public void run()
{
Log.i(LOGTAG,"Running socket thread");
try
{
// InetAddress svrAddr = InetAddress.getByName(SERVERIP);
byte[] Addr = new byte[]{10,0,2,2};
InetAddress svrAddr = InetAddress.getByAddress(Addr);
System.out.println("ADDRESS: " + svrAddr.toString());
/*
String IP = svrAddr.toString().substring(1);
InetAddress realAddr = InetAddress.getByName(IP);
System.out.println(IP);
System.out.println(realAddr.getAddress().toString());
*/
socket = new Socket(svrAddr, SERVERPORT);
// if (realAddr.isReachable(1000))
// System.out.println("Successfully reached");
//Setup i/o streams
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Keep listening for messages from server
while(!terminated)
{
final String message = in.readLine();
if(message != null && message != "")
{
Log.i(LOGTAG,"MSG recv : "+message);
//Update GUI with any server responses
final TextView txtv = (TextView) parentref.findViewById(R.id.txtServerResponse);
parentref.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
*
*
* This is where the the UI gets updated by the socket
*
*
*/
txtv.setText(message+"\n"+txtv.getText());
}
});
}
}
}
catch (UnknownHostException uhe)
{
Log.e(LOGTAG,"Unknownhost\n"+uhe.getStackTrace().toString());
}
catch (Exception e) {
Log.e(LOGTAG, "Socket failed\n"+e.getMessage());
e.printStackTrace();
}
disconnect();
Log.i(LOGTAG,"Thread now closing");
}
/**
* Disconnect from the server as well as closing i/o streams
*/
public void disconnect()
{
Log.i(LOGTAG, "Disconnecting from server");
try
{
in.close();
out.close();
}
catch(Exception e)
{/*do nothing*/}
try
{
socket.close();
}
catch(Exception e)
{/*do nothing*/}
}
}
这就是我得到的日志输出:
03-30 21:08:39.846 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/MainPAge﹕ onCreate entered
03-30 21:08:39.852 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/SocketTester﹕ Running socket thread
03-30 21:08:39.861 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/System.out﹕ ADDRESS: /10.0.2.2
03-30 21:08:39.867 1115-1115/uk.ac.man.cs.COMP28512.lab4 E/SocketTester﹕ Socket failed
null
03-30 21:08:39.868 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ android.os.NetworkOnMainThreadException
03-30 21:08:39.898 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
03-30 21:08:39.898 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
03-30 21:08:39.898 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
03-30 21:08:39.898 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
03-30 21:08:39.899 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
03-30 21:08:39.899 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
03-30 21:08:39.899 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.net.Socket.startupSocket(Socket.java:590)
03-30 21:08:39.899 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.net.Socket.<init>(Socket.java:226)
03-30 21:08:39.912 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at uk.ac.man.cs.COMP28512.lab4.ServerConnect.run(ServerConnect.java:85)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at uk.ac.man.cs.COMP28512.lab4.MainActivity.onCreate(MainActivity.java:33)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5933)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-30 21:08:39.913 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-30 21:08:39.914 1115-1115/uk.ac.man.cs.COMP28512.lab4 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-30 21:08:39.915 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/SocketTester﹕ Disconnecting from server
03-30 21:08:39.918 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/SocketTester﹕ Thread now closing
03-30 21:08:39.982 1115-1132/uk.ac.man.cs.COMP28512.lab4 D/OpenGLRenderer﹕ Render dirty regions requested: true
03-30 21:08:40.002 1115-1115/uk.ac.man.cs.COMP28512.lab4 D/﹕ HostConnection::get() New Host Connection established 0xaeef7a20, tid 1115
03-30 21:08:40.086 1115-1115/uk.ac.man.cs.COMP28512.lab4 D/Atlas﹕ Validating map...
03-30 21:08:40.437 1115-1132/uk.ac.man.cs.COMP28512.lab4 D/﹕ HostConnection::get() New Host Connection established 0xaeef74a0, tid 1132
03-30 21:08:40.508 1115-1132/uk.ac.man.cs.COMP28512.lab4 I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-30 21:08:40.605 1115-1132/uk.ac.man.cs.COMP28512.lab4 D/OpenGLRenderer﹕ Enabling debug mode 0
03-30 21:08:40.677 1115-1132/uk.ac.man.cs.COMP28512.lab4 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-30 21:08:40.678 1115-1132/uk.ac.man.cs.COMP28512.lab4 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa7a3b2e0, error=EGL_SUCCESS
03-30 21:08:40.812 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/Choreographer﹕ Skipped 39 frames! The application may be doing too much work on its main thread.
03-30 21:08:41.773 1115-1115/uk.ac.man.cs.COMP28512.lab4 I/Choreographer﹕ Skipped 57 frames! The application may be doing too much work on its main thread.
查看错误并跟踪它们我无法理解可能导致Socket无法创建的原因。
答案 0 :(得分:2)
请参阅What's the difference between Thread start() and Runnable run()
看起来你直接调用run()而不是通过start()。