- 论坛徽章:
- 0
|
在工作过程中,因某些原因想在客户端绑定127.0.0.1的IP发送给服务器并异步接收服务器返回(服务器在本机。本机有另外一个局域网的IP)
程序大体是这样的
sock2 = TcpServInitWithIP( "127.0.0.1", "7099", 5 );
...
sock1 = TcpConnectUseLocalIP( "127.0.0.1", "22.16.16.46", "7990", 10 );
...(发送数据)
TcpClose(sock1);
...(下略)
(accept等,接收服务器返回)
这样可以正常工作。但是如果将TcpServInitWithIP放在TcpConnectUseLocalIP后面调用的时候,就会出错。
错误码因时间过久忘记了,貌似是说资源忙吧。因现在环境不在了,没法重试。
如果不绑定IP,就没有此问题。
请达人看看是怎么回事
下面是源码(操作系统是AIX4.3)
int TcpServInit( const char *sPortName, int backLog )
{
struct sockaddr_in serverAddr;
in_port_t port;
int sockFd;
int opt;
if( backLog < 5 )
backLog = 5;
port = GetTcpPort( sPortName );
if( port == INPORT_NONE )
return -1;
sockFd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( sockFd < 0 )
{
RptLibErr( errno );
return -1;
}
opt = 1;
setsockopt( sockFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = port;
if( bind( sockFd, (SA *)&serverAddr, sizeof( serverAddr ) ) < 0 )
{
RptLibErr( errno );
TcpClose( sockFd );
return -1;
}
if( listen( sockFd, backLog ) < 0 )
{
RptLibErr( errno );
TcpClose( sockFd );
return -1;
}
return sockFd;
}
int TcpServInitWithIP( const char* sIPName, const char *sPortName, int backLog )
{
struct sockaddr_in serverAddr;
in_port_t port;
in_addr_t addr;
int sockFd;
int opt;
if( backLog < 5 )
backLog = 5;
port = GetTcpPort( sPortName );
if( port == INPORT_NONE )
return -1;
addr = GetTcpAddr( sIPName );
if( addr == INADDR_NONE )
return -1;
sockFd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( sockFd < 0 )
{
RptLibErr( errno );
return -1;
}
opt = 1;
setsockopt( sockFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = addr;
serverAddr.sin_port = port;
if( bind( sockFd, (SA *)&serverAddr, sizeof( serverAddr ) ) < 0 )
{
RptLibErr( errno );
TcpClose( sockFd );
return -1;
}
if( listen( sockFd, backLog ) < 0 )
{
RptLibErr( errno );
TcpClose( sockFd );
return -1;
}
return sockFd;
}
int TcpConnect( const char *sIPName, const char *sPortName, int nTimeOut )
{
struct sockaddr_in serverAddr;
int sock;
in_port_t port;
in_addr_t addr;
int ret;
Sigfunc *OldAction;
Assert( sIPName != NULL && *sIPName != 0 );
Assert( sPortName != NULL && *sPortName != 0 );
if( ( port = GetTcpPort( sPortName ) ) == INPORT_NONE )
{
RptLibErr( TCP_ILL_PORT );
return -1;
}
if( ( addr = GetTcpAddr( sIPName ) ) == INADDR_NONE )
{
RptLibErr( TCP_ILL_IP );
return -1;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = addr;
serverAddr.sin_port = port;
sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( sock < 0 )
{
RptLibErr( errno );
return -1;
}
if( nTimeOut == TIME_INFINITE )
{
ret = connect( sock, (struct sockaddr *)&serverAddr, sizeof( serverAddr ) );
}
else
{
OldAction = SetAlrm( nTimeOut, NULL );
ret = connect( sock, (struct sockaddr *)&serverAddr, sizeof( serverAddr ) );
UnSetAlrm();
}
if( ret == 0 )
return sock;
TcpClose( sock );
RptLibErr( errno );
return -1;
}
int TcpConnectUseLocalIP( const char *sLocalIP, const char *sIPName, const char *sPortName, int nTimeOut )
{
struct sockaddr_in serverAddr;
int sock;
in_port_t port;
in_addr_t addr;
in_addr_t addr1;
int ret;
int opt;
Sigfunc *OldAction;
Assert( sIPName != NULL && *sIPName != 0 );
Assert( sPortName != NULL && *sPortName != 0 );
if( ( port = GetTcpPort( sPortName ) ) == INPORT_NONE )
{
RptLibErr( TCP_ILL_PORT );
return -1;
}
if( ( addr1 = GetTcpAddr( sLocalIP ) ) == INADDR_NONE )
{
RptLibErr( TCP_ILL_IP );
return -1;
}
if( ( addr = GetTcpAddr( sIPName ) ) == INADDR_NONE )
{
RptLibErr( TCP_ILL_IP );
return -1;
}
sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( sock < 0 )
{
RptLibErr( errno );
return -1;
}
opt = 1; /*标志打开*/
setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = addr1;
serverAddr.sin_port = 0;
if( bind( sock, (SA *)&serverAddr, sizeof( serverAddr ) ) < 0 )
{
RptLibErr( errno );
TcpClose(sock);
return -1;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = addr;
serverAddr.sin_port = port;
if( nTimeOut == TIME_INFINITE )
{
ret = connect( sock, (struct sockaddr *)&serverAddr, sizeof( serverAddr ) );
}
else
{
OldAction = SetAlrm( nTimeOut, NULL );
ret = connect( sock, (struct sockaddr *)&serverAddr, sizeof( serverAddr ) );
UnSetAlrm();
}
if( ret == 0 )
return sock;
TcpClose( sock );
RptLibErr( errno );
return -1;
}
[ 本帖最后由 xiao_cui2000 于 2006-4-22 12:11 编辑 ] |
|