将Android客户端连接到TCP C#服务器

时间:2016-02-16 13:04:30

标签: java c# android tcp server

我创建了一个简单的服务器代码,它接受多连接并从客户端读取一个字符串(命令),其中我的客户端是一个简单的Android应用程序,它应该发送一个类似“GetListFoods”的请求类型

我已成功建立连接,但进程在服务器应该读取请求的部分(服务器端)停止,无法完成其工作。

我非常确定我的服务器端代码,但我是新的android程序员。

这是我的服务器端代码:

public class Server
    {
        private int _Port;
        private TcpListener _Listener;
        private bool _Listening = false;

        public Server()
        {
            _Port = 5050;
        }

        public void Start()
        {
            _Listener = new TcpListener(IPAddress.Any, _Port);

            _Listener.Start();

            _Listening = true;

            _Listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _Listener);
        }

        private void HandleAcceptTcpClient(IAsyncResult result)
        {
            if(_Listening)
            {
                _Listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _Listener);
                TcpClient client = _Listener.EndAcceptTcpClient(result);

                ClientConnection connection = new ClientConnection(client);
                ThreadPool.QueueUserWorkItem(connection.HandleConnection, client);
            }
        }
    }

连接类只是一个处理客户端请求的简单类:

 class ClientConnection
    {
        internal TcpClient _controlClient;
        private NetworkStream _controlStream;
        private StreamReader _controlReader;
        private StreamWriter _controlWriter;

        public ClientConnection(TcpClient client)
        {
            _controlClient = client;
            _controlStream = _controlClient.GetStream();
            _controlReader = new StreamReader(_controlStream);
            _controlWriter = new StreamWriter(_controlStream);
        }

        public void HandleConnection(object obj)
        {
            try
            {
                string line;
                while(!string.IsNullOrEmpty(line = _controlReader.ReadLine()))
                {
                    //Make Some work here
                }
            }
            catch (Exception ex)
            {

            }
        }
    }

我需要从我的Android应用程序发出一个简单的请求, 这是我的代码:

public class Main extends AppCompatActivity {

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

        Button start = (Button)findViewById(R.id.start);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConnectToServer server = new ConnectToServer();
                server.execute("GetList");
            }
        });
    }

    public class ConnectToServer extends AsyncTask<String, String, Void> {
        PrintWriter out;
        BufferedReader in;
        Socket client;

        public ConnectToServer() {
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                //byte[] message = params[0].getBytes();
                client = new Socket();
                client.connect(new InetSocketAddress("X.X.X.X", 5050));
            } catch (UnknownHostException host) {
                host.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            try {

                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                out.write(params[0]);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    }
}

提前感谢:)

0 个答案:

没有答案