试图制作一个Android TCP服务器应用程序

时间:2015-06-01 14:41:17

标签: java android sockets tcp

我正在尝试制作一个Android TCP服务器应用程序,它可以通过wifi-shield从我的arduino板上获取消息。

我是java编码的新手,所以我从http://www.roman10.net/android-tcp-client-and-server-communication-programmingillustrated-with-example/

下载了代码

但似乎它不起作用。它没有任何错误,但是当我运行它时,它只是没有响应。

任何人都可以帮助我吗? package com.example.androidserversocket;

package roman10.tutorial.tcpcommserver;
package roman10.tutorial.tcpcommserver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TcpServer extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textDisplay = (TextView) this.findViewById(R.id.text1);
        textDisplay.setText("");
        runTcpServer();
    }
    private TextView textDisplay;
    private static final int TCP_SERVER_PORT = 5000;
    private void runTcpServer() {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(TCP_SERVER_PORT);
            //ss.setSoTimeout(10000);
            //accept connections
            Socket s = ss.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            //receive a message
            String incomingMsg = in.readLine() + System.getProperty("line.separator");
            Log.i("TcpServer", "received: " + incomingMsg);
            textDisplay.append("received: " + incomingMsg);
            //send a message
            String outgoingMsg = "goodbye from port " + TCP_SERVER_PORT + System.getProperty("line.separator");
            out.write(outgoingMsg);
            out.flush();
            Log.i("TcpServer", "sent: " + outgoingMsg);
            textDisplay.append("sent: " + outgoingMsg);
            //SystemClock.sleep(5000);
            s.close();
        } catch (InterruptedIOException e) {
            //if timeout occurs
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
            }

1 个答案:

答案 0 :(得分:0)

就我而言,将在UI线程上调用onCreate()方法。不再可以在UI线程上运行网络操作。因此你应该替换

runTcpServer();

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        runTcpServer();
    }
});
t.start();

您还需要将以下行添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

如果你还没有这样做的话 您可以找到有关问题here

的更多信息