Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP Server don't get the right data from the client #27174

Closed
shiraez opened this issue Jul 27, 2020 · 9 comments
Closed

TCP Server don't get the right data from the client #27174

shiraez opened this issue Jul 27, 2020 · 9 comments

Comments

@shiraez
Copy link

shiraez commented Jul 27, 2020

When i open tcp server and tcp client with different threads, buffer filled with trash using zsock_recv.

TCP Server:

int
server()
{
struct sockaddr_in addr;
    uint16_t port = 40000;
    char              *dst = "192.0.3.1";
    int               s, new_socket = -1;
    int                ret = -1;
    int addrlen = sizeof(addr);
    int opt = 1;
    char buffer[1024] = {0};
    char* messg = "Hello from server";

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if ((s = zsock_socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        return -1;
    }

    if (zsock_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
    {
        return -1;
    }

    if (zsock_bind(s, (struct sockaddr *)&addr,
                                 sizeof(addr))<0)
    {
        clicon_err(OE_UNIX, errno, "bind");
        return -1;
    }
    if (zsock_listen(s, 3) < 0)
    {
        return -1;
    }
    if ((new_socket = zsock_accept(s, (struct sockaddr *)&addr,
                       (socklen_t*)&addrlen))<0)
    {
        return -1;
    }
    ret = zsock_recv( new_socket , buffer, sizeof(buffer), 0);
    printk("len %d  %s\n",ret, buffer);
   return 0;

}

TCP client:

int
client()
{
    struct sockaddr_in addr;
    uint16_t port = 40000;
    char              *dst = "192.0.3.1";
    int               *s = -1;
    int                ret = -1;
    char* messg = "Hello from client";
    char buffer[1024] = {0};

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);

    if ((s = zsock_socket(addr.sin_family, SOCK_STREAM, 0)) < 0) {
    clicon_err(OE_CFG, errno, "socket");
    return -1;
    }
    if (zsock_inet_pton(addr.sin_family, dst, &addr.sin_addr) != 1)
    return -1;

    if (zsock_connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0){
    zsock_close(s);
    return -1;
    }
    if((ret = zsock_send(s , messg , strlen(messg) , 0 )) < 0)
      return -1;
    done:
    return 0;
}

Environment:
-POSIX

@jukkar
Copy link
Member

jukkar commented Jul 28, 2020

In client you have this which is wrong.

> int *s = -1;

After I removed *, the thing worked just fine.

@jukkar jukkar closed this as completed Jul 28, 2020
@shiraez
Copy link
Author

shiraez commented Jul 28, 2020

Hi, thanks for your answer, but it doesn't work.
(After I removed *)

@timork timork reopened this Jul 28, 2020
@timork
Copy link
Collaborator

timork commented Jul 28, 2020

@shiraez
Please provide better example for the issue

jukkar added a commit to jukkar/zephyr that referenced this issue Jul 28, 2020
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
@jukkar
Copy link
Member

jukkar commented Jul 28, 2020

I used this test program for testing
https://github.com/jukkar/zephyr/tree/bug-27174-client-server-communication
See the code in samples/net/sockets/client-server

@shiraez
Copy link
Author

shiraez commented Jul 28, 2020

I ran your test but this is still print "len 1024" and the buffer is 0.
I'm using an older version of zephyr(114 I think), do you know if this is related to this problem?

@jukkar
Copy link
Member

jukkar commented Jul 29, 2020

When using latest upstream, I get

Booting from ROM..*** Booting Zephyr OS build zephyr-v2.3.0-1351-g2a9a3bc926b6  ***

[00:00:00.000,000] <inf> net_config: Initializing network
[00:00:00.010,000] <inf> net_config: IPv4 address: 192.0.2.1
uart:~$ len 17  Hello from client
[00:00:00.110,000] <inf> net_config: IPv6 address: fe80::5054:ff:fe12:3456
[00:00:00.110,000] <inf> net_config: IPv6 address: fe80::5054:ff:fe12:3456

When running it in 1.14, I get the same print len 17 Hello from client but there is a NULL ptr access in TCP. This with qemu_x86 and native_posix. There is some issue that has been fixed in upstream but is missing in 1.14.
What board are you using?

@shiraez
Copy link
Author

shiraez commented Jul 29, 2020

I use native_posix 64 bit.

@github-actions
Copy link

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

@Anuhya0
Copy link

Anuhya0 commented Aug 27, 2024

can you please help me out in my project on tcp client-server communication using cora z7 and h265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants