Android Socket Listener只运行一次

时间:2013-07-18 15:33:35

标签: c# android sockets

我有一个Android应用程序,当前正在使用Socket Listener来侦听来自C#应用程序的传入连接。我能够让C#应用程序将字符串发送到Android应用程序,但Socket Listener似乎只接受/执行必要的代码才能将信息写出一次,但继续接受来自C#应用程序的传入连接请求。

我搜索过Stackoverflow并在Google上花了很多时间,但似乎无法确定此问题的确切原因。以下是我的代码。

Android代码

public class ScoringActivity extends Activity
{

    InputStream is;
    private String ipAddress = "";
    ProgressDialog progress;

    private TextView serverStatus;

    // DEFAULT IP
    public static String SERVERIP = "10.0.2.2";

    // DESIGNATE A PORT
    public static final int SERVERPORT = 6000;

    private Handler handler = new Handler();

    private ServerSocket serverSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scoring);
        Intent intent= getIntent();
        ipAddress = intent.getStringExtra("ipAddress");
        setProgressBarIndeterminateVisibility(false);
        Thread fst = new Thread(new ServerThread());
        fst.start();
    }

    public class ServerThread implements Runnable 
    {
        public void run() 
        {
            try 
            {
                if (SERVERIP != null) 
                {
                    handler.post(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            Log.d("Listening on IP: ", SERVERIP + " " + SERVERPORT);
                        }
                    });
                    serverSocket = new ServerSocket(SERVERPORT);
                    while (true) 
                    {
                        // LISTEN FOR INCOMING CLIENTS
                        Socket client = serverSocket.accept();
                        handler.post(new Runnable() 
                        {
                            @Override
                            public void run() 
                            {
                              Log.d("Connected.","Connected.");
                            }
                        });

                        try 
                        {
                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            String line = null;
                            while ((line = in.readLine()) != null) 
                            {
                                Log.d("ServerActivity", line);
                                handler.post(new Runnable() 
                                {
                                    @Override
                                    public void run() 
                                    {
                                      Log.d("In the run method", "in the run method");
                                    }
                                });
                            }
                            in.close();
                            break;
                        } catch (Exception e) 
                          {
                            handler.post(new Runnable() 
                            {
                                @Override
                                public void run() 
                                {
                                    Log.d("Oops. Connection interrupted. Please reconnect your phones.","Oops. Connection interrupted. Please reconnect your phones.");
                                }
                            });
                            e.printStackTrace();
                          }
                    }
                } else 
                {
                    handler.post(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            Log.d("Couldn't detect internet connection.","Couldn't detect internet connection.");
                        }
                    });
                }
            } catch (Exception e) 
            {
                handler.post(new Runnable() 
                {
                    @Override
                    public void run() 
                    {
                        Log.d("Error","Error");
                    }
                });
                e.printStackTrace();
            }
        }
    }
}

C#代码

//ProcessSqlFiles();
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
    if (!clientSocket.Connected)
        clientSocket.Connect(IPAddress.Parse("127.0.0.1"), 8080);
    clientSocket.Send(Encoding.UTF8.GetBytes("Test Input"));
    clientSocket.Close();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

1 个答案:

答案 0 :(得分:0)

break离开了圈子:

}
                            in.close();
                            break;

删除break声明。我想你可能想要的是continue,假设你想回到无限循环的开始。