Android套接字客户端没有发送和关闭自己

时间:2012-12-28 19:27:16

标签: android sockets client

我对android和java编程很新,我需要你的帮助。我想在我的PC上创建一个Android客户端和一台服务器(Windows 7)。我检查了putty(一个模仿客户端的程序(没有编程错误^^))我的服务器是否编程没有错误。从而我认识到我的服务器编程正确。

在这里,您可以看到我运行良好的服务器:

  public class MyServer {
  public static void main (String[] args) throws IOException {

  ServerSocket serverSocket = null;

  try {
      serverSocket = new ServerSocket(4449);
      System.out.println("Listening on port: 4449");
  } catch (IOException e){
      System.err.println("Could not listen on port 4449.");
      System.exit(1);
  }

  Socket clientSocket = null;

  try {
      clientSocket = serverSocket.accept();
   System.out.println("Got connection.");
  } catch (IOException e) {
      System.err.println("Accept failed: 4449.");
      System.exit(1);
  }

  BufferedReader in = null;
  PrintWriter out = null;
  try {
      in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

      out = new PrintWriter(clientSocket.getOutputStream(), true);
  } catch (IOException e) {
      System.err.println("Read failed");
      e.printStackTrace();
  }


  try {

      System.out.println("message: " + in.readLine());
      out.println("hab was bekommen!");
  } catch (IOException e) {
      System.err.println("Can't get a message from Client.");
      e.printStackTrace();

  }
  }
  }

我也尝试为这台服务器创建一个Android客户端,但我没有管理它。

这是我的主要活动:

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;
TextView problems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_android_client);

    textOut = (EditText)findViewById(R.id.textout);
    Button buttonSend = (Button)findViewById(R.id.send);
    textIn = (TextView)findViewById(R.id.textin);
    problems = (TextView)findViewById(R.id.problems);
    buttonSend.setOnClickListener(buttonSendOnClickListener);
}

Button.OnClickListener buttonSendOnClickListener 
= new Button.OnClickListener() {


    @Override
    public void onClick(View arg0) {
        //TODO Auto-generated method stub
        Socket client = null;


        BufferedReader in = null;
        PrintWriter out = null;

        try {
            client = new Socket("192.168.2.107", 4449);
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (UnknownHostException e) {
            problems.setText("Unknown host: 192.168.2.107");
        } catch (IOException e) {
            // System.out.println("No Input/Output.");
            problems.setText("No Input/Output.");
        }

        try {
            out.println("Hallo.");
            textIn.setText(in.readLine());
        } catch (IOException e) {
            problems.setText("Can't send/ get message.");
        }
    }   
};



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_android_client, menu);
    return true;
}
    }

到目前为止一切顺利。当我在Android智能手机上启动我的应用程序时,应用程序启动没有任何问题。然后我在EditText字段'textOut'中键入内容并按下'Send'按钮。然后我的屏幕变黑,应用程序自行关闭。我还粘贴了AndroidManifest.xml中的权限,以进入互联网并使用访问Wifi。

我希望有人可以帮助我,因为我正在解决这个问题2天并且还没有找到任何解决方案。我主要使用这一方(http://android-er.blogspot.de/2011/01/simple-communication-using.html),当然还有其他一些方面,但我得到了大部分信息。此外,我的源代码中没有显示错误。

亲切的问候,

Lukas5060

编辑:这是我的LogCat:

12-28 20:21:55.929:I / dalvikvm(698):threadid = 3:对信号3作出反应 12-28 20:21:56.039:I / dalvikvm(698):将堆栈跟踪写入'/data/anr/traces.txt' 12-28 20:21:56.259:I / dalvikvm(698):threadid = 3:对信号3作出反应 12-28 20:21:56.299:I / dalvikvm(698):将堆栈跟踪写入'/data/anr/traces.txt' 12-28 20:21:56.741:D / gralloc_goldfish(698):未检测到GPU仿真的仿真器。 12-28 20:21:56.771:I / dalvikvm(698):threadid = 3:对信号3作出反应 12-28 20:21:56.789:I / dalvikvm(698):将堆栈跟踪写入'/data/anr/traces.txt' 12-28 20:23:58.430:D / AndroidRuntime(698):关闭VM 12-28 20:23:58.430:W / dalvikvm(698):threadid = 1:线程退出时未捕获异常(group = 0x409c01f8) 12-28 20:23:58.470:E / AndroidRuntime(698):致命异常:主要 12-28 20:23:58.470:E / AndroidRuntime(698):android.os.NetworkOnMainThreadException 12-28 20:23:58.470:E / AndroidRuntime(698):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 12-28 20:23:58.470:E / AndroidRuntime(698):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 12-28 20:23:58.470:E / AndroidRuntime(698):at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 12-28 20:23:58.470:E / AndroidRuntime(698):at libcore.io.IoBridge.connect(IoBridge.java:112) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.Socket.startupSocket(Socket.java:566) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.Socket.tryAllAddresses(Socket.java:127) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.Socket。(Socket.java:177) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.net.Socket。(Socket.java:149) 12-28 20:23:58.470:E / AndroidRuntime(698):at net.ibasic.AndroidClient $ 1.onClick(AndroidClient.java:50) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.view.View.performClick(View.java:3511) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.view.View $ PerformClick.run(View.java:14105) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.os.Handler.handleCallback(Handler.java:605) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.os.Handler.dispatchMessage(Handler.java:92) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.os.Looper.loop(Looper.java:137) 12-28 20:23:58.470:E / AndroidRuntime(698):在android.app.ActivityThread.main(ActivityThread.java:4424) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.lang.reflect.Method.invokeNative(Native Method) 12-28 20:23:58.470:E / AndroidRuntime(698):at java.lang.reflect.Method.invoke(Method.java:511) 12-28 20:23:58.470:E / AndroidRuntime(698):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 12-28 20:23:58.470:E / AndroidRuntime(698):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-28 20:23:58.470:E / AndroidRuntime(698):at dalvik.system.NativeStart.main(Native Method) 12-28 20:23:59.070:I / dalvikvm(698):threadid = 3:对信号3作出反应 12-28 20:23:59.100:I / dalvikvm(698):将堆栈跟踪写入'/data/anr/traces.txt' 12-28 20:24:02.420:I / Process(698):发送信号。 PID:698 SIG:9

1 个答案:

答案 0 :(得分:1)

好吧我的猜测是问题是你在主ui线程中这样做是远远不推荐的。相反,你应该做的是checkout AsyncTask,这是进行这种操作的首选方式(从文件或网络和其他一些东西中读取)。

这比听起来更容易你需要做的就是这样(注意这只是为了让你感觉它看起来如何):

private class CreateSocketTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        //Do the socket stuff here...
    }

    @Override
    protected void onPostExecute(String result) {
        //This is called when doInBackground has finished
        //From here you'd call a method in the main ui class.
    }
}

继承人android doc

网上有很多教程,因此您可以立即启动并运行。