vb.net套接字与android套接字通信

uqxowvwt  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(201)

我正在尝试使用套接字在android应用程序和vb.net应用程序之间进行通信。vb应用程序连接到socket还可以,但我面临的问题是vb.net应用程序在调用stream.dispose时只将数据发送到连接的android socket。
下面是我的android和vb.net代码
安卓:

class Thread1 implements Runnable {

    @Override
    public void run() {
        Socket socket;
        try {
            serverSocket = new ServerSocket(SERVER_PORT);

            try {
                socket = serverSocket.accept();
                input = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtviewIP.setText("Connected");
                    }
                });
                new Thread(new Thread2()).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class Thread2 implements Runnable {
        boolean bKeepRunning = true;

    @Override
    public void run() {
        while (bKeepRunning) {
            try {
                final String message = input.readLine();
                if (message != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            Log.d("Message", "Command Received: " + message);
                            if (message.equalsIgnoreCase("A")) {
                                DoSomething();
                                bKeepRunning = false;
                            }
                        }
                    });
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            serverSocket.close();

            Thread1 = new Thread(new Thread1());
            Thread1.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

vb.net版本:

Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim ipAddress As String
Dim port As String = "9877"
Dim serverStream As NetworkStream
Dim bConnected As Boolean = False

Public Function Connect() As Boolean

    Try
        clientSocket.Connect(ipAddress, port)

        serverStream = clientSocket.GetStream()
        bConnected = True
    Catch ex As Exception
        bConnected = False

    Finally

    End Try

    Connect = bConnected

End Function

Public Sub SendToServer(text As String)
    'Dim serverStream As NetworkStream = clientSocket.GetStream()
    Dim outStream As Byte() = System.Text.Encoding.UTF8.GetBytes(text)
    serverStream.Write(outStream, 0, outStream.Length)
    serverStream.Flush()

    'till this point no data is sent.
    'as soon as I call dispose or close data is sent 
    serverStream.Close() OR serverStream.Dispose()

    'Have also tried below code but this also do not work and behaves in the same way as above
    Dim stream As StreamWriter = New StreamWriter(serverStream, System.Text.Encoding.UTF8)
    stream.AutoFlush = True
    stream.Write(text)
    stream.Flush()
    'Data is sent as soon as the below line is executed
    Stream.Close()
End Sub

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题