Android tcp服务器用按钮发送到客户端的消息

时间:2015-02-03 10:16:55

标签: java android sockets tcp

如何使用按钮将数据(字符串)服务器发送到客户端? 我做了几次尝试,但我是Android编程的新手。因此 等待你的帮助..我的期望:只是一个小样本.. 我的目标是将数据从服务器发送到客户端。

enter code here  public class MainActivity extends ActionBarActivity {
private TextView tvClientMsg,tvServerIP,tvServerPort;
private final int SERVER_PORT = 13003; //Define the server port
@Override

protected void onCreate(Bundle saveenter code heredInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvClientMsg = (TextView) findViewById(R.id.textViewClientMessage);
    tvServerIP = (TextView) findViewById(R.id.textViewServerIP);
    tvServerPort = (TextView) findViewById(R.id.textViewServerPort);
    tvServerPort.setText(Integer.toString(SERVER_PORT));


    getDeviceIpAddress();

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {

                ServerSocket socServer = new ServerSocket(SERVER_PORT);

                Socket socClient = null;

                while (true) {

                    socClient = socServer.accept();

                    ServerAsyncTask serverAsyncTask = new ServerAsyncTask();


                    serverAsyncTask.execute(new Socket[] {socClient});
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public void getDeviceIpAddress() {
    try {

        for (Enumeration<NetworkInterface> enumeration = NetworkInterface
                .getNetworkInterfaces(); enumeration.hasMoreElements();) {
            NetworkInterface networkInterface = enumeration.nextElement();

            for (Enumeration<InetAddress> enumerationIpAddr = networkInterface.getInetAddresses(); enumerationIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumerationIpAddr.nextElement();

                if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {

                    tvServerIP.setText(inetAddress.getHostAddress());
                }
            }
        }
    } catch (SocketException e) {
        Log.e("ERROR:", e.toString());
    }
}


public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

class ServerAsyncTask extends AsyncTask<Socket, Void, String> {


    protected String doInBackground(Socket... params) {
        String result = null;

        Socket mySocket = params[0];
        try {

            InputStream is = mySocket.getInputStream();

            PrintWriter out = new PrintWriter(
                    mySocket.getOutputStream(), true);

            out.println("Hello from server");

            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));

            result = br.readLine();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;




    }


    protected void onPostExecute(String s) {

        tvClientMsg.setText(s);
    }







}
}

0 个答案:

没有答案