Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android与c#tcp通信_C#_Android_Tcp - Fatal编程技术网

Android与c#tcp通信

Android与c#tcp通信,c#,android,tcp,C#,Android,Tcp,我想通过TCP从C#应用程序向Android仿真器或设备发送消息。我在谷歌上搜索过,Android客户端和C#server的搜索结果最多,但不是我想要的结果 这是我尝试过的;也许我做得不对,但我想要的是通过TCP向Android设备发送消息 发送消息的服务器代码: private static int port = 4444; private static TcpListener listener; private static Thread thread; pr

我想通过TCP从C#应用程序向Android仿真器或设备发送消息。我在谷歌上搜索过,Android客户端和C#server的搜索结果最多,但不是我想要的结果

这是我尝试过的;也许我做得不对,但我想要的是通过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());
            }
        }
    }

问题在于您的
客户机
代码。
在双方之间打开一个套接字,一个充当执行
绑定->侦听->接受的侦听器/服务器,另一个充当连接
的客户端
代码中有两个问题:

  • 您的客户应
    连接
    ,而不是
    接受

  • 客户端和服务器都将尝试从套接字读取数据,然后再写入。如果套接字的读写操作在不同的线程上,则不会有问题。在您的情况下,这将导致死锁。 如果您希望两个操作都在同一个线程上完成,则客户端和服务器应按相反的顺序执行读写操作:
    客户端:
    read->write

    服务器:
    write->read

  • 客户端和服务器都打开服务器套接字并等待连接。您的客户端应该连接到服务器,而不是打开
    ServerSocket