简体   繁体   English

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

[英]TCP connection phase fails (calling web service from JAVA client to C#-server)

so I have an app for Android that calls a webservice (C#) and I have from the start noticed that it often, but not all the time, it is extremely slow in returning an answer or that the request fails altogether. 因此我有一个适用于Android的应用程序,该应用程序调用了Web服务(C#),从一开始我就注意到它经常但并非始终如此,返回答案的速度非常慢,或者请求完全失败。

At first I thought it had something to do with the backend code, ie the server takes to long to process the request and return a result. 起初,我认为这与后端代码有关,即服务器需要很长时间来处理请求并返回结果。 I have now excluded that possibility as the processing only takes about 40ms. 我现在排除了这种可能性,因为处理只需要40毫秒。 I did however notice that the webservice code isnt called sometimes, or it takes a long time for the code to be executed. 但是,我确实注意到有时不调用Web服务代码,或者执行代码需要花费很长时间。

So I started up Wireshark and took a look at the packets transferred, and discovered what I see as a faulty TCP connection phase. 因此,我启动了Wireshark,查看了传输的数据包,发现了我认为错误的TCP连接阶段。 The first picture below show a faulty connection. 下面的第一张图片显示了错误的连接。

It looks like this: 看起来像这样:
Client: SYN 客户:SYN
Server: SYN ACK 服务器:SYN ACK
Client: SYN ACK 客户:SYN ACK

as this image shows: 如下图所示:

在此处输入图片说明

But it should look like this: 但是它应该看起来像这样:
Client: SYN 客户:SYN
Server: SYN, ACK 服务器:SYN,ACK
Client: ACK 客户端:ACK

When I try another time, just calling the webservie again from the Android app (no restart or anything) I get the correct steps and also a response as expected. 当我再次尝试时,只要再次从Android应用程序中调用webservie(不重新启动或执行任何操作),我将获得正确的步骤,并得到预期的响应。 Here is a Wireshark-image from that: 这是来自其中的Wireshark图像: 在此处输入图片说明

I am pretty sure that this is the problem with what I see as a "very slow webservice", as there is usually a long delay before receiving an answer and sometimes (as in the first image), there is no response at all. 我非常确定这是我认为“非常缓慢的Web服务”的问题,因为在收到答案之前通常会有很长的延迟,有时(如第一张图片所示)根本没有响应。 Wireshark also shows that it can just "stall" with the answers for 10-20 seconds, and that is before actually reacing the web service code. Wireshark还显示,它只能在10-20秒内“拖延”答案,而这实际上是在重新获得Web服务代码之前。

There is no network problem, as this is all run on a local WLAN (the computer acts as a hotspot, no one else is connected to it). 没有网络问题,因为所有问题都在本地WLAN上运行(计算机充当热点,没有其他人与其连接)。 The Android device is a Samsung S2 running 2.3.3, the webservice is .NET 3.5 running a self-hosted ServiceHost. Android设备是运行2.3.3的Samsung S2,Web服务是运行自托管ServiceHost的.NET 3.5。

Any ideas? 有任何想法吗?

-------- ADDITIONAL INFO ------------ - - - - 附加信息 - - - - - -

Here is how I call my webservice from the JAVA-code (Method is GET): 这是我从JAVA代码(方法是GET)中调用Web服务的方式:

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();
        }
    }

This is how the web service method is defined in .NET: 这是在.NET中定义Web服务方法的方式:

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

If you look at the dump a little closer, what you're actually seeing is the client not getting any packets from the server; 如果您更仔细地查看转储,您实际上看到的是客户端没有从服务器获取任何数据包。

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)

In other words, a perfectly normal retransmission from both sides, provided the client does not receive any server packets. 换句话说,如果客户端未接收到任何服务器数据包,则从双方进行的完全正常的重新传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM