tcp客户端调用bind函数绑定IP地址出错。

caisam 2014-11-17 08:01:15
你我都知道客户端没有绑定IP地址的必要,内核会自动分配一个。但是我现在需要绑定一个IP地址。
主机1(客户端):网卡1的IP地址:192.168.152.131;网卡2的192.168.152.133
主机2(服务端):IP地址:192.168.152.130
(当然是能够ping通的)
程序功能很简单:服务端像客户端返回时间。客户端显示时间,服务端显示客户端的IP地址。
------------------------------------客户端代码-----------------------------------------------------------
//timecli.c
#include<stdio.h>
#include<linux/socket.h>
#include<linux/types.h>
#include<arpa/inet.h>
#include<fcnlt.h>
#include<unistd.h>
#include<netinet/in.h>
#include<time.h>

//#define MAXLINE 1024
#define err_sys( m ) { perror( m ); exit( 1 ); }

int
main( int argc, char *argv[] )
{
if( argc != 2 )
{
printf( "Usage: %s <IP address>", argv[0] );
exit( 1 );
}
int sockfd;
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
if( sockfd < 0 )
err_sys( "socket error" );
struct sockaddr_in ser_add;
struct sockaddr_in client_add;

/* Client bind IP and port */

bzero( ( void * )&client_add, sizeof( client_add ) );
char clien_IP[] = "192.169.152.131";
client_add.sin_family = AF_INET;
client_add.sin_port = htons( 51670 );
if( ( inet_pton( AF_INET, clien_IP,( struct sockaddr * )&client_add.sin_addr ) ) <= 0 )
err_sys( "inet_pton error" );
if( bind( sockfd, ( struct sockaddr * )&client_add, sizeof( client_add ) ) < 0 )
err_sys( "bind error");



/* server add and connect */
bzero( ( void * )&ser_add, sizeof( ser_add ) );
ser_add.sin_family = AF_INET;
ser_add.sin_port = htons( 13 );
if( ( inet_pton( AF_INET, argv[1], ( struct sockaddr * )&ser_add.sin_addr ) ) <= 0 )
err_sys( "inet_pton error" );
if( connect( sockfd, ( struct sockaddr * )&ser_add, sizeof( ser_add ) ) < 0 )
err_sys( "connect error" );
int n;
char recvline[MAXLINE];
while( ( n = read( sockfd, recvline, MAXLINE ) ) > 0 )
{
if( fputs( recvline, stdout ) < 0 )
err_sys( "recvline error" );
}
if( n < 0 )
err_sys( "read error" );
return 0;
}
---------------------服务端-----------------------------
//timeser.c
#include<stdio.h>
#include<linux/socket.h>
#include<linux/types.h>
#include<arpa/inet.h>
#include<fcnlt.h>
#include<unistd.h>
#include<netinet/in.h>
#include<time.h>
//#include<unp.h>

#define MAXLINE 1024
#define err_sys( m ) { perror( m ); exit( 1 ); }

int
main()
{
int sockfd;
if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
err_sys( "socket error" );
struct sockaddr_in seradd, client_addr;
bzero( ( void * )&seradd, sizeof( seradd ) );
seradd.sin_family = AF_INET;
seradd.sin_port = htons( 13 );
//seradd.sin_addr.s_addr = htonl( INADDR_ANY );
/*为了验证:当服务器端绑定了ip地址时,是不是只接受以这个ip地址为目的地的
* 客户端链接。当bind_name[]="127.0.0.1"时,发现客户端不能和服务端建立链接*/
char bind_name[] = "192.168.152.130";
if( ( inet_pton( AF_INET, bind_name, &seradd.sin_addr ) ) < 0 )
err_sys( "inet_pton error" );
if( bind( sockfd, ( struct sockaddr *)&seradd, sizeof( seradd ) ) < 0 )
err_sys( "bind error" );
listen( sockfd, 5 );
while( 1 )
{
int connect_fd;
int len;
char buff[MAXLINE];
len = sizeof( client_addr );
connect_fd = accept( sockfd, ( struct sockaddr * )&client_addr, &len );
if( connect_fd < 0 )
err_sys( "connect connect_fd" );
printf( " connect from %s, port:%d\n",
inet_ntop( AF_INET, &client_addr.sin_addr, buff, sizeof( buff ) ),
ntohs( client_addr.sin_port ) );
time_t thetime;
thetime = time( NULL );
snprintf( buff, sizeof( buff ), "%.24s\r\n", ctime( &thetime ) );
write( connect_fd, buff, strlen( buff ) );
}
close( sockfd );
return 0;
}
--------------------------问题描述--------------------------
当我在客户端调用bind函数绑定IP地址时,出现了can't assign requested address
就像上面客户端代码里面所示(红字)。但是注释掉那段代码就没问题。
该怎么办呢?如果非要绑定一个IP地址的话。
...全文
551 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
caisam 2014-11-18
  • 打赏
  • 举报
回复
没人么?等等等等等

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧