免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10994 | 回复: 9
打印 上一主题 下一主题

socket的connect返回1,代表什么意思? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-27 13:33 |只看该作者 |倒序浏览
如题。书上所说,-1是出错。正常是0.但我的代码会有返回1.
#include "unp.h"
#define MAXPORT 1000
int main(int argc,char **argv){
        int sockfd,i,ret;
        struct sockaddr_in      servaddr;
        if (argc != 2)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_quit("usage: a.out <IPaddress>");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_sys("socket error");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bzero(&servaddr, sizeof(servaddr));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servaddr.sin_family = AF_INET;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_quit("inet_pton error for %s", argv[1]);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i<MAXPORT;i++){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("conneting to port %d \n",i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servaddr.sin_port   = htons(i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ret=connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) >= 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("port %d is open \n",i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("return %d\n",ret);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}
~

open的端口返回的都是1,而未开放的端口返回的是0。
...
conneting to port 12
return 0
conneting to port 13
port 13 is open
return 1
.....

我的是ubuntu7.10

论坛徽章:
0
2 [报告]
发表于 2007-11-27 13:39 |只看该作者
扫描端口?

论坛徽章:
0
3 [报告]
发表于 2007-11-27 14:41 |只看该作者
哪位大哥知道的,指点一下啊。

论坛徽章:
0
4 [报告]
发表于 2007-11-27 14:53 |只看该作者

你可以把errorno在控制台上输出看看阿!

Technical Reference: Communications, Volume 2

connect Subroutine

Purpose

       Connects two sockets.

Library

       Standard C Library (libc.a

Syntax

       #include <sys/socket.h>

       int connect ( Socket, Name, NameLength)

       int Socket;

       const struct sockaddr *Name;

       socklen_t NameLength;

Description

       The connect subroutine requests a connection between two sockets. The kernel sets up the communication link between the
       sockets; both sockets must use the same address format and protocol.

       If a connect subroutine is issued on an unbound socket, the system automatically binds the socket. The connect subroutine
       can be used to connect a socket to itself. This can be done, for example, by binding a socket to a local port (using bind)
       and then connecting it to the same port with a local IP address (using connect).

       The connect subroutine performs a different action for each of the following two types of initiating sockets:
       *    If the initiating socket is SOCK_DGRAM, the connect subroutine establishes the peer address. The peer address
            identifies the socket where all datagrams are sent on subsequent send subroutines. No connections are made by this
            connect subroutine.
       *    If the initiating socket is SOCK_STREAM or SOCK_CONN_DGRAM, the connect subroutine attempts to make a connection to
            the socket specified by the Name parameter. Each communication space interprets the Name parameter differently. For
            SOCK_CONN_DGRAM socket type and ATM protocol, some of the ATM parameters may have been modified by the remote station,
            applications may query new values of ATM parameters using the appropriate socket options.
       *    In the case of a UNIX(R) domain socket, a connect call only succeeds if the process that calls connect has read and
            write permissions on the socket file created by the bind call. Permissions are determined by the umask< value of the
            process that created the file.

Implementation Specifics

Parameters

       Socket
            Specifies the unique name of the socket.
       Name
            Specifies the address of target socket that will form the other end of the communication line
       NameLength
            Specifies the length of the address structure.

Return Values

       Upon successful completion, the connect subroutine returns a value of 0.

       If the connect subroutine is unsuccessful, the system handler performs the following functions:
       *    Returns a value of -1 to the calling program.
       *    Moves an error code, indicating the specific error, into the errno global variable.

Error Codes

       The connect subroutine is unsuccessful if any of the following errors occurs:
       Value
            Description
       EADDRINUSE
            The specified address is already in use. This error will also occur if the SO_REUSEADDR socket option was set and the
            local address (whether specified or selected by the system) is already in use.
       EADDRNOTAVAIL
            The specified address is not available from the local machine.
       EAFNOSUPPORT
            The addresses in the specified address family cannot be used with this socket.
       EALREADY
            The socket is specified with O_NONBLOCK or O_NDLAY, and a previous connecttion attempt has not yet completed.
       EINTR
            The attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection will be
            established asynchronously.
       EACCESS
            Search permission is denied on a component of the path prefix or write access to the named socket is denied.
       ENOBUFS
            The system ran out of memory for an internal data structure.
       EOPNOTSUPP
            The socket referenced by Socket parameter does not support connect.
       EWOULDBLOCK
            The range allocated for TCP/UDP ephemeral ports has been exhausted.
       EBADF
            The Socket parameter is not valid.
       ECONNREFUSED
            The attempt to connect was rejected.
       EFAULT
            The Address parameter is not in a writable part of the user address space.
       EINPROGRESS
            The socket is marked as nonblocking. The connection cannot be immediately completed. The application program can
            select the socket for writing during the connection process.
       EINVAL
            The specified path name contains a character with the high-order bit set.
       EISCONN
            The socket is already connected.
       ENETDOWN
            The specified physical network is down.
       ENETUNREACH
            No route to the network or host is present.
       ENOSPC
            There is no space left on a device or system table.
       ENOTCONN
            The socket could not be connected.
       ENOTSOCK
            The Socket parameter refers to a file, not a socket.
       ETIMEDOUT
            The establishment of a connection timed out before a connection was made.

Examples

       The following program fragment illustrates the use of the connect subroutine by a client to initiate a connection to a
       server's socket.

       struct sockaddr_un server;
       .
       .
       .
       connect(s,(struct sockaddr*)&server, sun_len(&server));

Related Information

       The accept subroutine, bind subroutine, getsockname subroutine, send subroutine, socket, subroutine, socks5tcp_connect
       subroutine.

       Initiating UNIX Stream Connections Example Program, Sockets Overview, and Understanding Socket Connections in AIX 5L
       Version 5.3 Communications Programming Concepts.

论坛徽章:
0
5 [报告]
发表于 2007-11-27 15:55 |只看该作者
#include "unp.h"
#define MAXPORT 1000
extern int errno;
int main(int argc,char **argv){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int sockfd,i,ret;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct sockaddr_in      servaddr;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (argc != 2)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_quit("usage: a.out <IPaddress>");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_sys("socket error");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bzero(&servaddr, sizeof(servaddr));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servaddr.sin_family = AF_INET;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_quit("inet_pton error for %s", argv[1]);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i<MAXPORT;i++){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("conneting to port %d \n",i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servaddr.sin_port   = htons(i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ret=connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) >= 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("port %d is open \n",i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("return %d\n",ret);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("errno %d,msg %s",errno,strerror(errno));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}


返回结果:
conneting to port 1
return 0
errno 111,msg Connection refusedconneting to port 2
return 0
errno 111,msg Connection refusedconneting to port 3
return 0
errno 111,msg Connection refusedconneting to port 4
return 0
errno 111,msg Connection refusedconneting to port 5
return 0
errno 111,msg Connection refusedconneting to port 6
return 0
errno 111,msg Connection refusedconneting to port 7
return 0
errno 111,msg Connection refusedconneting to port 8
return 0
errno 111,msg Connection refusedconneting to port 9
return 0
errno 111,msg Connection refusedconneting to port 10
return 0
errno 111,msg Connection refusedconneting to port 11
return 0
errno 111,msg Connection refusedconneting to port 12
return 0
errno 111,msg Connection refusedconneting to port 13
port 13 is open
return 1
errno 111,msg Connection refusedconneting to port 14
return 0
errno 106,msg Transport endpoint is already connectedconneting to port 15
return 0
errno 106,msg Transport endpoint is already connectedconneting to port 16
return 0
errno 106,msg Transport endpoint is already connectedconneting to port 17
return 0
errno 106,msg Transport endpoint is already connectedconneting to port 18
return 0
。奇怪的是它连接失败为什么不返回-1,而返回0.而成功返回的不是0,却是1.这和unp书上的意思相左啊。网上其他的也都是提到返回-1为出错。0为正常。

论坛徽章:
0
6 [报告]
发表于 2007-11-28 10:01 |只看该作者
顶啊。还没人出来解答啊

论坛徽章:
0
7 [报告]
发表于 2007-11-28 10:56 |只看该作者
你的if 句少写了一个()
正确的是
  if( (ret=connect(sockfd, (SA *) &servaddr, sizeof(servaddr)))>=0)
你再试一下

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
8 [报告]
发表于 2007-11-28 11:05 |只看该作者
if (ret=connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) >= 0)
=的优先级低,所以........

论坛徽章:
0
9 [报告]
发表于 2007-11-28 13:04 |只看该作者
果然是这问题。多谢兄弟们

论坛徽章:
0
10 [报告]
发表于 2007-11-28 13:14 |只看该作者
是因为你没有设置你的socket可以重复利用你设置的IP地址,因为当你连续connect同一个IP地址是错误的操作,所以你应该在connect之前加一条setsockopt函数来设置这个SO_REUSEADDR选项
if(setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&reuseaddr,sizeof(int)) == -1){
                fprintf(stderr,"set sock option error(SO_REUSEADDR)\n");
                exit(-1);
        }
//int reuseaddr=1;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP