BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

Swift bluesocket Can't get long data ?

Open ShineYangGod opened this issue 4 years ago • 18 comments

When I use tcp to connect successfully, I need to get a long data, but nothing is returned

ShineYangGod avatar Aug 02 '21 06:08 ShineYangGod

Could you provide a minimum example to reproduce it? Thanks.

mbarnach avatar Aug 03 '21 08:08 mbarnach

I used the testReadWrite method in dome, normal data can be received, but long data cannot be received

ShineYangGod avatar Aug 04 '21 00:08 ShineYangGod

我的代码.zip 这是我的代码

ShineYangGod avatar Aug 04 '21 00:08 ShineYangGod

Hello, what's the matter with my question? Can it be solved?

ShineYangGod avatar Aug 10 '21 00:08 ShineYangGod

@ShineYangGod I'm not sure what you mean by "long data". I've replaced the line in SocketTests.testReadWriteby:

// let hello = "Hello from client..."
let hello = String(repeating: "Hello from client...", count: 1000)

and everything works fine. If your use case is different, please provide a minimum example that we can look at.

mbarnach avatar Aug 10 '21 07:08 mbarnach

Wait a minute, I will give you a long data example

ShineYangGod avatar Aug 10 '21 07:08 ShineYangGod

MintBlueTCPSocketMangent.swift.zip

This is what I use now, and finally there is an example of the data returned by our gateway

ShineYangGod avatar Aug 10 '21 07:08 ShineYangGod

If I'm using the data (end of your doc) directly in the sample, it works fine. I think the issue should be somewhere else, either in the setup or in the delay induced by your gateway maybe?

mbarnach avatar Aug 10 '21 08:08 mbarnach

If i directly use SocketTests.testReadWrite Is it possible to connect to TCP and send data and receive data?

ShineYangGod avatar Aug 10 '21 08:08 ShineYangGod

截屏2021-08-10 16 31 53 The first line is the data I sent to the gateway, and the last line is the received data. Normally, there is a string of data similar to the example I sent to you.

ShineYangGod avatar Aug 10 '21 08:08 ShineYangGod

I tested it several times, and it seems that the client stopped receiving after receiving a few pieces of data. How do I set the client to continue to receive data?

ShineYangGod avatar Aug 10 '21 09:08 ShineYangGod

After testing, our gateway is no problem. I tried other third-party sockets, and there is no problem. Now, when we use our socket to receive messages, we can’t achieve continuous reception. Our gateway will generally return two messages in a row, and will actively push the message. When I send a message, the gateway is pushing at the same time, and the message will not be received at this time.

ShineYangGod avatar Aug 12 '21 06:08 ShineYangGod

@ShineYangGod This sounds similar to this issue. TCP sockets can operate in a blocking or non-blocking fashion. When using non-blocking (which I think is the default), you are not guaranteed to get any specific number of bytes upon read. So when you run things locally you might see that you get all your data instantaneously, but when you go through any router, your data may be "chunked" in an arbitrary way. You can read more details in this Stack Overflow discussion as well.

dannys42 avatar Aug 28 '21 22:08 dannys42

Can you give a specific example?

ShineYangGod avatar Sep 01 '21 01:09 ShineYangGod

Why is the tcp connection so slow? And the message sent to the server is very slow. I have no problem with other sockets, and I looked at the log and did not receive the message sent.

ShineYangGod avatar Sep 28 '21 12:09 ShineYangGod

Hi @ShineYangGod can you provide a working example? I'm looking at the MintBlueTCPSocketMangent.swift.zip you gave and it looks like your server in mintServerHelper() is accepting new connections but not reading or writing any data. Meanwhile your client in mintTestReadWrite() is attempting to read data as soon as it's connected... I'm not sure if the socket is a blocking or non-blocking socket...

If the client socket is blocking, then the first mintReadAndPrint() on line 125 will just block until timeout before you write.

If the client socket is non-blocking, then socket.write() on line 127 may potentially be incomplete.

So either way I think there are issues with your socket handling.

dannys42 avatar Sep 29 '21 04:09 dannys42

Hi @ShineYangGod can you provide a working example? I'm looking at the MintBlueTCPSocketMangent.swift.zip you gave and it looks like your server in mintServerHelper() is accepting new connections but not reading or writing any data. Meanwhile your client in mintTestReadWrite() is attempting to read data as soon as it's connected... I'm not sure if the socket is a blocking or non-blocking socket...

If the client socket is blocking, then the first mintReadAndPrint() on line 125 will just block until timeout before you write.

If the client socket is non-blocking, then socket.write() on line 127 may potentially be incomplete.

So either way I think there are issues with your socket handling.

dannys42 avatar Sep 29 '21 04:09 dannys42

MintBlueTCPSocketMangent.swift.zip I'm not sure what this method is for? acceptClientConnection, whenever you get to this step, it will jump out, attach my code

ShineYangGod avatar Sep 30 '21 09:09 ShineYangGod