将 Action<String> 从 c# 转换为 Java

我目前正在 C#(主机)和 Java(客户端)之间编写一个非常简单的 TCP 连接


我正在努力用 Java 接收消息,我找到了一个(在我看来)c# 的好解决方案。我将引用传递给构造函数中的方法,该方法将被保存,稍后如果收到消息,我可以从父类调用此方法。


我想在 Java 中做完全相同的事情,但我不知道怎么做!


最后我想要一个易于使用的类,我如何在java中回调。


服务器代码 (c#)


class Server

    {


        private TcpListener serverSocket;

        private TcpClient clientSocket;

        private NetworkStream networkStream;


        private Action<string> outRecievedMethod;

        private Thread recievingRoutineThread;


        public Server(int port, Action<string> outRecievedMethod) {

            serverSocket = new TcpListener(IPAddress.Any,port);

            clientSocket = default(TcpClient);

            serverSocket.Start();

            clientSocket = serverSocket.AcceptTcpClient();

            networkStream = clientSocket.GetStream();

            this.outRecievedMethod = outRecievedMethod;

            recievingRoutineThread = new Thread(this.recievingRoutine);

            recievingRoutineThread.Start();

        }


        private void recievingRoutine()

        {

            while (true)

            {

                String recievingString = null;

                try

                {

                    recievingString = recieve();

                }catch (Exception ex) { }

                recievingString = recievingString.Replace("\0", "");

                if(recievingString != null && recievingString != "")

                    outRecievedMethod((String)recievingString.Clone()); 

            }

        }


        private string recieve()

        {

            byte[] bytesFrom = new byte[65536];

            networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

            string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

            return dataFromClient;

        }


        public void send(string msg)

        {...}


        public void send(byte[] msgBytes)

        {...}

    }


隔江千里
浏览 134回答 1
1回答

一只甜甜圈

您可以使用java.util.function.Consumer。将消费者实例传递给客户端构造函数并accept在接收时调用消费者的方法:public class Client {&nbsp; &nbsp; private final Consumer<String> messageConsumer;&nbsp; &nbsp; public Client( String ip, int port, Consumer<String>&nbsp; messageConsumer) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; this.messageConsumer = messageConsumer;&nbsp; &nbsp; &nbsp; &nbsp; Thread recievingRoutineThread = new Thread( new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String recievingString = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{ recievingString = recieve();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception ex) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(recievingString != null && recievingString != "")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendRecievedMessageOutside(recievingString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp; &nbsp; recievingRoutineThread.start();&nbsp; &nbsp; }&nbsp; &nbsp; private void sendRecievedMessageOutside(String message){&nbsp; &nbsp; &nbsp; &nbsp; messageConsumer.accept( message );&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP