从android到PC的TCP无法使用java工作

时间:2016-09-03 10:53:27

标签: java android tcp

我正试图通过TCP从我的Android手机(使用java应用程序)向我的计算机发送一条简单的消息。我有一个正在我的计算机上监听的套接字,但是一旦我运行这个应用程序,它就会崩溃。我是Android新手,所以请耐心等待......

这是我的Java代码:

package com.scorekeep.clienttcp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    EditText textOut;


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

        Socket socket = null;
        try {
            socket = new Socket("10.0.0.10",5000);
            DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
            DOS.writeUTF("HELLO_WORLD");
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您无法在ui线程中执行套接字连接等后台任务。你应该使用AsyncTask。

示例

import android.app.*;
import android.os.*;
import android.util.*;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new connection().execute();
    }
}

class connection extends AsyncTask<String,String,String> {

    @Override
    protected String doInBackground(String... message) {

        try
        {
            InetAddress serverAddr = InetAddress.getByName("ip here");
            Socket socket = new Socket(serverAddr, 5000); //port here
    // send the message to the server
        PrintWriter out = new PrintWriter(new BufferedWriter(
                              new OutputStreamWriter(socket.getOutputStream())), true);
        out.println("hi");
        out.flush(); //optional
        socket.close();
        }
        catch (Exception e)
        {}

        return null;
    }
}

注意未经测试。可能包含错误。

答案 1 :(得分:1)

根据要求提供了一个扩展示例:

import android.app.*;
import android.os.*;
import android.util.*;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity 
{
    @Override
     protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new connection().execute();
    }
}

 class connection extends AsyncTask<String,String,String> {
public static PrintWriter out;
BufferedReader in;
public static boolean running = true;
    @Override
    protected String doInBackground(String... message) {

        try
        {
            InetAddress serverAddr = InetAddress.getByName("localhost");
            Socket socket = new Socket(serverAddr, 8008);
            // send the message to the server
            out = new PrintWriter(new BufferedWriter(
                              new OutputStreamWriter(socket.getOutputStream())), true);

            in = new BufferedReader(new InputStreamReader(
                                    socket.getInputStream()));

        while(running) {
            String msgfromserver = in.readLine();
        }
        }
        catch (Exception e)
        {}

        return null;
    }

    public static void sendmsg(String msg){
        if(out!=null){
            out.println(msg);
            out.flush();
        }
    }
}

<强>用法: 从按钮

的OnClick方法调用connection.sendmsg("some text");

并设置connection.running = false; onbackpress。 (或在完成活动之前)

答案 2 :(得分:1)

代码中有两处更改。在AsyncTask类的开头<{1}}和公共public static void sendmsg