如何在C中调试tcp套接字连接?

时间:2015-02-04 04:11:37

标签: c sockets tcp embedded

我有一个C程序

我想连接到地址为0xAC101067的端口3333(172.16.16.103:3333)

但它始终连接失败并始终获得结果connect(device_info->cloud_fd, &addr, sizeof(addr))

的-1

我从API中知道它说0是成功而-1是失败,

那么如何找出这个程序中的问题?

 if( device_info->cloud_fd == -1 && (u32)cloud_ip_addr>0){
    device_info->cloud_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(device_info->cloud_fd,0,SO_BLOCKMODE,&opt,4);
    cloud_ip_addr=0xAC101067;
    addr.s_ip = cloud_ip_addr; 
    addr.s_port = 3333;//device_info->conf.server_port;
        printf("device_info->cloud_fd=%d\r\n",device_info->cloud_fd);
    if (connect(device_info->cloud_fd, &addr, sizeof(addr))!=0) 
      goto cloud_error; 
  }

2 个答案:

答案 0 :(得分:1)

  1. 如答案所阐明的那样,取出适当的错误 @LPs。问题可能不在代码中,而在于外部。
  2. 在客户端和服务器之间获取tcpdump。看什么 电线蒸发。此捕获对于调试客户端无法连接的原因是必不可少的。可能是服务器无法访问且connect超时(ETIMEDOUT)或者没有人监听所述端口目标计算机(ECONNREFUSED)。

答案 1 :(得分:0)

错误描述

   **errno - number of last error**

<强>概要

   #include <errno.h>

<强>描述

   The <errno.h> header file defines the integer variable errno, which
   is set by system calls and some library functions in the event of an
   error to indicate what went wrong.  Its value is significant only
   when the return value of the call indicated an error (i.e., -1 from
   most system calls; -1 or NULL from most library functions); a
   function that succeeds is allowed to change errno.

   Valid error numbers are all nonzero; errno is never set to zero by
   any system call or library function.

为了您的功能

返回值

   If the connection or binding succeeds, zero is returned.  On error,
   -1 is returned, and errno is set appropriately.

<强>错误

   The following are general socket errors only.  There may be other
   domain-specific error codes.

   EACCES For UNIX domain sockets, which are identified by pathname:
          Write permission is denied on the socket file, or search
          permission is denied for one of the directories in the path
          prefix.  (See also path_resolution(7).)

   EACCES, EPERM
          The user tried to connect to a broadcast address without
          having the socket broadcast flag enabled or the connection
          request failed because of a local firewall rule.

   EADDRINUSE
          Local address is already in use.

   EADDRNOTAVAIL
          (Internet domain sockets) The socket referred to by sockfd had
          not previously been bound to an address and, upon attempting
          to bind it to an ephemeral port, it was determined that all
          port numbers in the ephemeral port range are currently in use.
          See the discussion of /proc/sys/net/ipv4/ip_local_port_range
          in ip(7).

   EAFNOSUPPORT
          The passed address didn't have the correct address family in
          its sa_family field.

   EAGAIN Insufficient entries in the routing cache.

   EALREADY
          The socket is nonblocking and a previous connection attempt
          has not yet been completed.

   EBADF  The file descriptor is not a valid index in the descriptor
          table.

   ECONNREFUSED
          No-one listening on the remote address.

   EFAULT The socket structure address is outside the user's address
          space.

   EINPROGRESS
          The socket is nonblocking and the connection cannot be
          completed immediately.  It is possible to select(2) or poll(2)
          for completion by selecting the socket for writing.  After
          select(2) indicates writability, use getsockopt(2) to read the
          SO_ERROR option at level SOL_SOCKET to determine whether
          connect() completed successfully (SO_ERROR is zero) or
          unsuccessfully (SO_ERROR is one of the usual error codes
          listed here, explaining the reason for the failure).

   EINTR  The system call was interrupted by a signal that was caught;
          see signal(7).

   EISCONN
          The socket is already connected.

   ENETUNREACH
          Network is unreachable.

   ENOTSOCK
          The file descriptor is not associated with a socket.

   EPROTOTYPE
          The socket type does not support the requested communications
          protocol.  This error can occur, for example, on an attempt to
          connect a UNIX domain datagram socket to a stream socket.

   ETIMEDOUT
          Timeout while attempting connection.  The server may be too
          busy to accept new connections.  Note that for IP sockets the
          timeout may be very long when syncookies are enabled on the
          server.