1

我想通过 TCP 将消息从 C# 应用程序发送到 Android 模拟器或设备。我在 Google 上搜索并获得了 Android 客户端和 C# 服务器的最大结果,但不是我想要实现的。

这是我尝试过的;也许我走错了路,但我想要的是通过 TCP 向 Android 设备发送消息。

发送消息的服务器代码:

    private static int port = 4444;
    private static TcpListener listener;
    private static Thread thread;
    private static int clientId = 0;

  listener = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);
        thread = new Thread(new ThreadStart(Listen));
        thread.Start();

private static void Listen()
    {
        listener.Start();
        MessageBox.Show("Listening on: " + port.ToString());

        while (true)
        {
            MessageBox.Show("Waiting for connection....");
            MessageBox.Show("Client No: " + clientId);
            TcpClient client = listener.AcceptTcpClient();
            Thread listenThread = new Thread(new ParameterizedThreadStart(ListenThread));
            listenThread.Start(client);
        }
    }

    private static void ListenThread(Object client)
    {

        NetworkStream netstream = ((TcpClient)client).GetStream();
        MessageBox.Show("Request made");
        clientId = clientId + 1;
        // String message = "Hello world";
        byte[] resMessage = Encoding.ASCII.GetBytes(clientId.ToString());
        netstream.Write(resMessage, 0, resMessage.Length);
        netstream.Close();
    }

客户代码

private TextView textDisplay;
Socket socket;
private static final int TCP_SERVER_PORT = 4444;
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.d("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.d("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) {
                Log.d("Error",e.toString());
            }
        }
    }
4

1 回答 1

0

问题在于您的Client代码。在两方之间打开一个套接字,一个扮演监听器/服务器的角色,bind->listen->accept一个客户端connect
在你的代码中有两个问题:

  1. 您的客户应该connect和不应该accept
  2. 您的客户端和服务器都将尝试从套接字读取并在之后写入。如果您从套接字进行的读取和写入操作在不同的线程上,则不会有问题。在您的情况下,这将导致死锁。如果您希望这两个操作在同一个线程上按顺序完成,那么您的客户端和服务器应该以相反的顺序执行读取和写入:
    客户端:read -> write
    服务器: write-> read
于 2020-07-24T07:03:18.433 回答