Write & read socket on IOS 10.3.2

39 views
Skip to first unread message

shop.servic...@gmail.com

unread,
Jul 11, 2017, 10:13:34 PM7/11/17
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA NetBeans 8.2
Desktop OS Windows 10
Simulator  3.7
Device IPhone 6 Plus IOS 10.3.2

Note: This works flawlessly on Android SCH-1545 OS Ver 5.0.1

Here is a code fragment 

import ca.weblite.codename1.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;


mySocket = new Socket(host, port, socketTimeOut);
OutputStream os = mySocket.getOutputStream();
os.write(frame(CMD.getBytes()));
is = mySocket.getInputStream();

Note: Monitoring The Server, 
Command is received and properly responded.

On the IOS Client, have read zero bytes, subsequent (4) retries resulted in zero bytes available / read
On Android, response is received in 10ms

Note: WiFi is local and 659mbps 
Very responsive.

Add build hint ios.add_libs=CFNetwork.framework (if deploying to iOS)
Please see attached hints settings.

Thoughts?

CodenameoneHints.PNG

Shai Almog

unread,
Jul 12, 2017, 12:54:49 AM7/12/17
to CodenameOne Discussions, shop.servic...@gmail.com
The build hint you added is:

ios.add libs=...

Instead of ios.add_libs with an underscore and no equals sign

shop.servic...@gmail.com

unread,
Jul 12, 2017, 9:19:32 AM7/12/17
to CodenameOne Discussions, shop.servic...@gmail.com
Thanks. (See new attached pic of hints)
Have corrected this and subsequent builds produce the following
Command received by Server, Server promptly responds
IOS client see's and reads only one byte and fails the call

Subsequent writes fail to read any data after successful writes
Note:
I close socket and streams after each command is written/read
CodenameoneHints.PNG

Shai Almog

unread,
Jul 13, 2017, 12:30:56 AM7/13/17
to CodenameOne Discussions, shop.servic...@gmail.com
I don't know, I only use our builtin socket api and not the lib.
Maybe Steve knows.

Closing a socket after every message will make your connection very slow. You might as well use HTTPS which is generally better. I strongly recommend avoiding sockets.

shop.servic...@gmail.com

unread,
Jul 13, 2017, 7:45:00 AM7/13/17
to CodenameOne Discussions, shop.servic...@gmail.com
My app opens a socket, sets a timeout does some transaction, (typically request data from the server) closes the connection and processed the request
I don't see an exposed method to set timeout in the builtin socket api

The lib version (https://github.com/shannah/CN1Sockets) works very well for my needs.

Please advise

Steve Hannah

unread,
Jul 13, 2017, 11:05:45 AM7/13/17
to codenameone...@googlegroups.com, shop.servic...@gmail.com
Can you share a little bit more of your client source code.  It's hard to comment without seeing more.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/52b4daf2-fa2d-4d86-94de-4cd53c1b0711%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

shop.servic...@gmail.com

unread,
Jul 14, 2017, 3:53:32 PM7/14/17
to CodenameOne Discussions, shop.servic...@gmail.com

 

import java.io.InputStream;
import java.io.OutputStream;

public static int socketReadRetries = 3;
public static int socketTimeOut = 3000;
public static Socket mySocket = null;

String CMD = "pingFromMobile~END~";
Object obj = null;
mySocket = new Socket(host, port, socketTimeOut);
OutputStream os = mySocket.getOutputStream();
os.write(frame(CMD.getBytes()));
          

for (int a = 0; a < socketReadRetries; a++) {

                is = mySocket.getInputStream();

                byte[] response = new byte[is.available()];

                if (response.length == 0) {

                    Thread.sleep(200);

                    continue;

                }

                try {

                    for (int b = 0; b < response.length; b++) {

                        response[b] = (byte) is.read();

                    }

                    obj = response;

                } catch (Exception h) {

                    h.printStackTrace();

                    if (mySocket != null) {

                        mySocket.close();

                    }

                    return null;

                }

                break;

            }

            if (is != null) {

                is.close();

            }

            if (mySocket != null && !mySocket.isClosed()) {

                mySocket.close();

Steve Hannah

unread,
Jul 14, 2017, 3:58:48 PM7/14/17
to codenameone...@googlegroups.com, shop.servic...@gmail.com
You shouldn't do this:

byte[] response = new byte[is.available()];

From the javadocs for InputStream:


Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Steve


To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

shop.servic...@gmail.com

unread,
Jul 14, 2017, 9:50:55 PM7/14/17
to CodenameOne Discussions, shop.servic...@gmail.com
Steve, Thanks I didn't know that, I modified the approach and it works correctly.

You have a great company and appreciate your support!
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/af805466-72b8-456f-b05f-94441161cc47%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages