swiftsocket / SwiftSocket

The easy way to use sockets on Apple platforms
BSD 3-Clause "New" or "Revised" License
1.68k stars 400 forks source link

rpi python server, ios client sends Receives only -1 #106

Open panayiotis95 opened 7 years ago

panayiotis95 commented 7 years ago

I configure raspberry pi as tcp server that waits for connection gets the data and then send it back. On swift I copy and paste the example client code in readme file of swiftsocket. The connection is established raspberry receives the data send but ios only shows -1 in xcode terminal

softknife commented 7 years ago

@panayiotis95 I have the same problem and i find the reason of showing -1 . If you read the file ytcpsocket.c , you will notice the following function :

int ytcpsocket_pull(int socketfd, char *data, int len, int timeout_sec) {
    int readlen = 0;
    int datalen = 0;
    if (timeout_sec > 0) {
        fd_set fdset;
        struct timeval timeout;
        timeout.tv_usec = 0;
        timeout.tv_sec = timeout_sec;
        FD_ZERO(&fdset);
        FD_SET(socketfd, &fdset);
        int ret = select(socketfd + 1, &fdset, NULL, NULL, &timeout);
        if (ret <= 0) {
            return ret; // select-call failed or timeout occurred (before anything was sent)
        }
    }
    // use loop to make sure receive all data
    do {
        readlen = (int)read(socketfd, data + datalen, len - datalen);
        printf("%d\n",readlen);   //  -1 is generated from here ,  an read error occured
        if (readlen > 0) {
            datalen += readlen;
        }
    } while (readlen > 0);

    return datalen;
}

But i don't know how to solve it. @danshevluk help us ,please

danshevluk commented 7 years ago

Hello there! Which version of SwiftSocket are you using?

softknife commented 7 years ago

@danshevluk version 2.0.2

-> SwiftSocket (2.0.2)
   A cool framework to work with TCP and UDP sockets
   pod 'SwiftSocket', '~> 2.0.2'
   - Homepage: https://github.com/swiftsocket/SwiftSocket
   - Source:   https://github.com/swiftsocket/SwiftSocket.git
   - Versions: 2.0.2, 2.0.1, 2.0, 1.2, 1.1 [master repo]

Some one description

softknife commented 7 years ago

any update?

nathjwhite commented 7 years ago

I believe this issue occurs only in version v2.0.2. The iOS example runs as expected in all previous versions.

gl-lei commented 6 years ago

I have the same problem and I resolve the issue by specifying the timeout parameter. guard let MsgBytes = tcpClient.read(4, timeout: 5) else { return; }

softknife commented 6 years ago

danshevluk is disappear or go to Hawaii for travelling ? 😄

fukemy commented 6 years ago

still not working

fukemy commented 6 years ago

how to install early version?