c# - TCP 连接阶段失败(从 JAVA 客户端调用 Web 服务到 C# 服务器)

标签 c# java web-services tcp connection

所以我有一个用于调用 Web 服务(C#)的 Android 应用程序,我从一开始就注意到它经常(但并非总是)返回答案的速度非常慢,或者请求完全失败。

起初我认为这与后端代码有关,即服务器处理请求并返回结果的时间太长。我现在排除了这种可能性,因为处理只需要大约 40 毫秒。但我确实注意到有时不会调用 Web 服务代码,或者执行代码需要很长时间。

因此,我启动了 Wireshark 并查看了传输的数据包,发现了我所看到的错误 TCP 连接阶段。下面的第一张图片显示连接错误。

看起来像这样:
客户端:SYN
服务器:SYN ACK
客户端:SYN ACK

如图所示:

enter image description here

但它应该看起来像这样:
客户端:SYN
服务器:SYN、ACK
客户端:ACK

当我再次尝试时,只需从 Android 应用程序再次调用 Webservie(无需重新启动或任何其他操作),我就会得到正确的步骤以及预期的响应。这是一个 Wireshark 图像: enter image description here

我很确定这就是我所认为的“非常慢的网络服务”的问题,因为在收到答案之前通常会有很长的延迟,有时(如第一张图片所示),在全部。 Wireshark 还表明,它可以在响应答案时“停滞” 10-20 秒,这是在实际接收 Web 服务代码之前。

不存在网络问题,因为这一切都在本地 WLAN 上运行(计算机充当热点,没有其他人连接到它)。 Android 设备是运行 2.3.3 的 Samsung S2,Web 服务是运行自托管 ServiceHost 的 .NET 3.5。

有什么想法吗?

--------附加信息------------

以下是我如何从 JAVA 代码调用我的 Web 服务(方法是 GET):

private void executeRequest(HttpUriRequest request, String url)
    {
        HttpClient client = new DefaultHttpClient();

        try 
        {
            httpResponse = client.execute(request);
            responseCode = httpResponse.getStatusLine().getStatusCode();
            message = httpResponse.getStatusLine().getReasonPhrase();

            HttpEntity entity = httpResponse.getEntity();

            if (entity != null) {

                InputStream instream = entity.getContent();
                response = convertStreamToString(instream);

                // Closing the input stream will trigger connection release
                instream.close();
            }

        } catch (ClientProtocolException e)  {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        } catch (IOException e) {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        }
    }

这就是 .NET 中 Web 服务方法的定义方式:

[OperationContract]
[WebGet(UriTemplate = "PutMessage?jsonString={jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat= WebMessageFormat.Json)]
string PutMessage(string jsonString);

最佳答案

如果您仔细查看转储,您实际上会看到客户端没有从服务器获取任何数据包;

Client: SYN
Server: SYN ACK (ACK, never reaches the client)
Server: SYN ACK (Retransmission of the first ACK, also lost. )
Client: SYN (Retransmission, it does not know the server received the first SYN)
Server: SYN ACK (ACK on the SYN from the client, once again lost)
Client: SYN (Retransmission since it *still* does not know the server got the SYN)

换句话说,只要客户端没有收到任何服务器数据包,双方都会进行完全正常的重传。

关于c# - TCP 连接阶段失败(从 JAVA 客户端调用 Web 服务到 C# 服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9557359/

相关文章:

c# - LINQ 按自定义类型分组不起作用

c# - 在 c#.net 中从数据库加载数据时如何将组合框值设置为 null 或空

c# - BindingList.Add() 即使有锁也不能跨线程工作

c# - 如何为复杂类型设置 DataKeyNames?

java - 深度优先搜索错误?

java - 使用 Jackson 将巨大的 JSON 响应反序列化为 POJO 的最有效方法是什么?

iOS - 通过 Web 服务登录

java - 如何使用 Process Builder 在 Java 中运行 NPM 命令

java - 如何在 Java CodeName One 中将文件 POST 到 URL?

apache-flex - 灵活的网络服务客户端