【问题标题】:TCPClient - Send & Receive ResponseTCPClient - 发送和接收响应
【发布时间】:2014-12-30 14:38:36
【问题描述】:

我正在连接到一个远程 TCP 侦听器,该侦听器接收一个字符串,并以一个响应进行响应。

从我的 Windows 8 Phone 应用程序转到 Java Jar。 JarIS收到消息,但 Windows 8 Phone 应用没有收到响应。

C#代码

outputClient.Connect (/IP ADDRESS/, /Port/);

        using (Socket sock = outputClient.Client) {
            sock.Send (UTF8Encoding.ASCII.GetBytes (broadcastMessage));

            var response = new byte[100];

            sock.Receive (response);
            var str = Encoding.ASCII.GetString (response).Replace ("\0", "");
            Console.WriteLine ("[RECV] {0}", str);
        } <-- JAVA CODE DOESN'T GET HIT UNTIL THIS LINE IS COMPLETED

Java 代码

String clientSentence;
        ServerSocket socketServer = new ServerSocket(/* PORT */);

        while (true)
        {
            Socket connectionSocket = socketServer.accept();
            connectionSocket.setKeepAlive(true);

            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            clientSentence = inFromClient.readLine();

            BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));

            if (clientSentence != null)
            {
                try
                {
                    JsonObject json = new JsonParser().parse(clientSentence).getAsJsonObject();
                    String un = json.get("Username").toString();
                    String uuid = "2c9c79a096ef4d869fb1d1e07469bb41".replaceAll(                                            
                            "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",                            
                            "$1-$2-$3-$4-$5"); 
                    var val = /* Get val */

                    String response = gson.toJson(val);
                    outToClient.write(response);
                   outToClient.newLine();
                   outToClient.flush();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    outToClient.write(response);
                    outToClient.newLine();
                    outToClient.flush();
                }
            }

            connectionSocket.close();
        }

再解释一下:JAVA CODE DOESN'T GET HIT UNTIL THIS LINE IS COMPLETED 表示在 使用 (Socket sock = outputClient.Client) 不再被使用。

【问题讨论】:

  • 这是什么意思:“在这条线完成之前,JAVA 代码不会受到攻击”?如果此时没有执行 Java 代码,您的 C# 程序如何接收任何数据?为什么您的问题似乎表明 C# 接收没有完成 not ?这个问题需要大量清理,这样才有意义。就发布的 C# 代码而言,您犯的一个主要错误是未检查接收的字节数。更重要的是,如果您使用的是 TcpClient(看起来就是这样),为什么不从其中获取 NetworkStream 并包装在 StreamReader 中,就像在 Java 代码中一样?
  • 我做了一个快速编辑以进一步解释它。
  • 对不起,这没有帮助。除非您将套接字设置为非阻塞,否则在接收到一些数据之前,您无法到达 using 块的右括号,而在发送了一些数据之前不会发生这种情况。 Java 端不可能在到达using 语句的结束块之前不发送。
  • 事情就是这样——如果我删除sock.Receive (response);,它会正常发送,如果不是,它就会挂起。
  • 那么当sock.Receive(response)在那里时,接收到了什么数据?

标签: java c# tcp tcpclient tcplistener


【解决方案1】:

我将 C# 代码替换为:

using (TcpClient client = new TcpClient (/IP ADDRESS/, /PORT/))
        using (NetworkStream stream = client.GetStream ())
        using (StreamReader reader = new StreamReader (stream))
        using (StreamWriter writer = new StreamWriter (stream)) {
            writer.AutoFlush = true;

            foreach (string lineToSend in linesToSend) {
                Console.WriteLine ("Sending to server: {0}", lineToSend);
                writer.WriteLine (lineToSend);
                string lineWeRead = reader.ReadLine ();
                Console.WriteLine ("Received from server: {0}", lineWeRead);
                Thread.Sleep (2000); // just for effect
            }
            Console.WriteLine ("Client is disconnecting from server");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多