Unable to connect via socket.

154 views
Skip to first unread message

Robert Winkler

unread,
Aug 7, 2018, 7:08:09 AM8/7/18
to NuttX
I'm trying to achieve TCP connection over sockets on my stm32 F4 discovery board with a computer, but my program stops at connect() function after creating socket.
It's possible to connect to the board via telnet. Ping command works, and a server responds on every packet. I have server application listening on the same port on the computer. The same "client" application works well on Linux.
I'm using ENC28J60 chip as a Ethernet driver.

When I'm using telnet I can see in wireshark that the board is responding, however when I try to connect via socket, there are no messages from board.

I have no clue what's going on... The application is really simple. Maybe somebody has experience with using sockets on NuttX and can help me.

My configuration includes:
CONFIG_NET_TCP=y
CONFIG_NET_TCP_CONNS
=40
CONFIG_NET_MAX_LISTENPORTS
=40
CONFIG_NET_TCP_RECVDELAY
=0

CONFIG_NET_UDP
=y
CONFIG_NET_ICMP
=y

CONFIG_NSOCKET_DESCRIPTORS
=10
CONFIG_NET_NACTIVESOCKETS
=16
CONFIG_NET_SOCKOPTS
=y

CONFIG_NET_PKT
=y
CONFIG_NET_PKT_CONNS
=1

CONFIG_SCHED_WORKQUEUE
=y
# CONFIG_SCHED_HPWORK is not set       <--- Also tested on High priority queue
CONFIG_SCHED_LPWORK
=y

# CONFIG_DISABLE_POLL is not set
CONFIG_NET_IPv4
=y
CONFIG_NET
=y

CONFIG_NSH_TELNET
=y
CONFIG_NETUTILS_PING
=y
CONFIG_SYSTEM_PING
=y

CONFIG_NSH_IPADDR
=0xc0a801c8
CONFIG_NSH_DRIPADDR
=0xc0a80101
CONFIG_NSH_NETMASK
=0xffffff00
CONFIG_NSH_NOMAC
=y
CONFIG_NSH_SWMAC
=y
CONFIG_NSH_MACADDR
=0x00e0deadbeef

CONFIG_NSH_NETINIT
=y

Application code:
#include <nuttx/config.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int tcpconnect_main(int argc, char *argv[])
#endif
{
 
int sockfd;
 
struct sockaddr_in server;

  server
.sin_addr.s_addr = inet_addr("192.168.1.3");  // IP ADRESS!
  server
.sin_family = AF_INET;
  server
.sin_port = htons(502);                       // PORT!

  printf
("Creating socket...\n");
  sockfd
= socket(AF_INET, SOCK_STREAM, 0);
 
if (sockfd == -1)
   
{
      printf
("Could not create socket.\n");
   
}
  printf
("Socket created!\n");

  printf
("Connecting with server...\n");
 
if (connect(sockfd , (struct sockaddr *)&server , sizeof(server)) < 0)
   
{
      printf
("Connect failed. Error.\n");
     
return 1;
   
}
  printf
("Connected!\n");

  printf
("Sending message...\n");
 
if(send(sockfd, "Test\n", 5, 0) < 0)
   
{
      printf
("Send failed.");
     
return 1;
   
}
  printf
("Sended!\n");

  close
(sockfd);
 
return 0;
}

Configuration and application code in attachment.

Best regards,
Robert
tcpconnect.zip
.config

patacongo

unread,
Aug 7, 2018, 8:27:14 AM8/7/18
to NuttX

When I'm using telnet I can see in wireshark that the board is responding, however when I try to connect via socket, there are no messages from board.

Normally you need to call bind() before connect() to bind the socket to a local address.  I don't know what happens when you try to connect an unbound socket.  I suspect that you just answered that question.

You could also turn on network debug output to see if there is anything else unusual going on.

Marco

unread,
Aug 7, 2018, 11:49:15 AM8/7/18
to nu...@googlegroups.com
Does the server listen on port 502? Telnet uses port 23 usually.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Robert Winkler

unread,
Aug 7, 2018, 12:58:01 PM8/7/18
to NuttX

Normally you need to call bind() before connect() to bind the socket to a local address.  I don't know what happens when you try to connect an unbound socket.  I suspect that you just answered that question.

You could also turn on network debug output to see if there is anything else unusual going on.

I added following code after creating socket but it do not work even with binding :/

  printf("Binding socket...\n");
 
if (bind(sockfd, (struct sockaddr*) &server, sizeof(server)))
   
{
      printf
("Could not bind!\n");
   
}
  printf
("Socket binded!\n");

The application stops at connect(). It looks like infinite loop. There are no timeouts, no errors just application stops.



I was trying to debug this and here is the result:

The last breakpoint inside connect() that is specyfic to application (I mean only my application is using that function) is:
Breakpoint 1, psock_tcp_connect (psock=0x10002160, addr=0x10003d28) at tcp/tcp_connect.c:317

after that the control goes to something like these:
psock_tcp_connect (psock=0x10002160, addr=0x10003d28) at tcp/tcp_connect.c:317
|-> 344 ret = net_lockedwait(&state.tc_sem);
 
|-> 353 return net_timedwait(sem, NULL);
   
|-> 316 ret = nxsem_wait(sem);
     
|-> 168 up_block_task(rtcb, TSTATE_WAIT_SEM);
       
|-> 155  up_switchcontext(rtcb->xcp.regs, nexttcb->xcp.regs);
         
|-> Some Assembler



Result looks like infinite loop.

Robert Winkler

unread,
Aug 7, 2018, 1:01:34 PM8/7/18
to NuttX

Does the server listen on port 502? Telnet uses port 23 usually.

Yes.  I try to make Modbus TCP/IP, that is why I'm using this port.


patacongo

unread,
Aug 7, 2018, 1:06:07 PM8/7/18
to NuttX


The last breakpoint inside connect() that is specyfic to application (I mean only my application is using that function) is:
Breakpoint 1, psock_tcp_connect (psock=0x10002160, addr=0x10003d28) at tcp/tcp_connect.c:317

after that the control goes to something like these:
psock_tcp_connect (psock=0x10002160, addr=0x10003d28) at tcp/tcp_connect.c:317
|-> 344 ret = net_lockedwait(&state.tc_sem);
 
|-> 353 return net_timedwait(sem, NULL);
   
|-> 316 ret = nxsem_wait(sem);
     
|-> 168 up_block_task(rtcb, TSTATE_WAIT_SEM);
       
|-> 155  up_switchcontext(rtcb->xcp.regs, nexttcb->xcp.regs);
         
|-> Some Assembler



Result looks like infinite loop.

I don't see any loop.  the part that you have shown above is completely normal.  The assembly language is a context switch.  The task that called connect() has been suspended and will not awaken until the context switch occurs.  If you continue single stepping through the assembly, you will eventually be running a different task.  It would probably be better to put a breakpoint at line 167 just after up_block_task() to see if the task awakens.

patacongo

unread,
Aug 7, 2018, 1:07:41 PM8/7/18
to NuttX

I added following code after creating socket but it do not work even with binding :/

  printf("Binding socket...\n");
 
if (bind(sockfd, (struct sockaddr*) &server, sizeof(server)))
   
{
      printf
("Could not bind!\n");
   
}
  printf
("Socket binded!\n");


This does not look right.  What is 'server'.  You don't bind() to the server.  You bind to a local address, then connect to the server.



Robert Winkler

unread,
Aug 7, 2018, 1:20:47 PM8/7/18
to NuttX
I'm not really an expert with networks, but I found something like this: https://stackoverflow.com/questions/27014955/socket-connect-vs-bind 
Also in all examples that I was looking at, there were no binding in client application. But I will check this!
Reply all
Reply to author
Forward
0 new messages