[英]How to debug tcp socket connection in C?
我有一个 C 程序
我想连接到地址为 0xAC101067 端口 3333 (172.16.16.103:3333) 的套接字
但它总是连接失败并且总是得到-1的结果
connect(device_info->cloud_fd, &addr, sizeof(addr))
我从 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;
}
connect
超时(ETIMEDOUT)或者没有人在所述端口目标机器上监听(ECONNREFUSED)。错误描述
**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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.